java - Android 应用程序无法将详细信息发布到 url

标签 java php android restful-url

我有一个 Android 应用程序,可以发布到“http://jont.byethost3.com/gcmtest/gcm.php?shareRegId=true ”。一旦被访问,应用程序就会向脚本发布一个 id key ,并且 php 文件将其保存到我的文件服务器上的文本文件中。 我已在浏览器中手动输入网址,由于没有收到任何 POST 数据,它成功生成了一个空文本文件,但当我在我的应用程序上测试它时,它不起作用。

我使用Asynchttp来调用并传递数据,如下

private void backgroundRegister(final String username){
    new AsyncTask<Void, Void, String>(){

        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try{

                if(gcmObj==null){
                    gcmObj = GoogleCloudMessaging.getInstance(getApplicationContext());
                }
                registerID = gcmObj.register(AppConstants.PROJ_ID);
                msg = "Registration ID : " + registerID;
                Log.d("REGOID",registerID);

            }catch(IOException e){
                Log.d("IOEXCEPTION",e.toString());
            }
            return msg;
        }
        @Override
        protected void onPostExecute(String s) {
            if(!TextUtils.isEmpty(registerID)){
                savetoSharedPrefs(getApplicationContext(),registerID,username);
                Toast.makeText(getApplicationContext(),"Registered with GCM",Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),"Registration failed",Toast.LENGTH_LONG).show();
            }
        }
    }.execute(null, null, null);
}

private void savetoSharedPrefs(Context context, String registerID,String userName){
    SharedPreferences prefs = getSharedPreferences("userDetails", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(regID, registerID);
    editor.putString(userNAME, userName);
    editor.commit();
    storeREG();
}

//store in the file server (PHP)
private void storeREG(){

    pg.show();
    params.put("regID", registerID);
    //Make RESTful webservice call

    AsyncHttpClient client = new AsyncHttpClient();
    client.post(AppConstants.SERVER_URL,params,new AsyncHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
            Intent home = new Intent(getApplicationContext(),HomeActivity.class);
            home.putExtra("regID",registerID);
            Log.d("REGID",registerID);
            startActivity(home);
            finish();
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            pg.hide();
            if(pg!=null){
                pg.dismiss();
            }
            if(statusCode==404){
                Toast.makeText(getApplicationContext(),"Requested resource not found",Toast.LENGTH_LONG).show();
            }else if(statusCode==500){
                Toast.makeText(getApplicationContext(),"Something went wrong at the server",Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),"Unexpected error occurred",Toast.LENGTH_LONG).show();
            }
        }


    });

}

我想要传递的数据只是明文。 我希望有人能帮助我解决这个问题。

编辑

我的PHP脚本

if(!empty($_GET["shareRegId"])) {
    $gcmRegID  = $_POST["regID"]; 
    //echo $gcmRegID;
    file_put_contents("./GCMRegId.txt",$gcmRegID);
    echo "Done!";
    exit;
    }

我的 list 以防万一

 <?xml version="1.0" encoding="utf-8"?>

<!-- GCM Permissions - Start here  -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />


<permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />


<uses-permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.VIBRATE" />



<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomeActivity"
        android:label="@string/title_activity_home" >
    </activity>

    <receiver
        android:name=".GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <action android:name="com.example.amosang.pushtest" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMNotificationIntentService" />
</application>

更新: 我已设法使用我的 mamp 服务器使此代码在本地工作,但当我将其上传到此主机时问题仍然存在。

最佳答案

为了使用 AsyncAndroidHttp 库传递 Post params,您可以使用提供的 RequestParams 类并将其传递给您的 Post API 调用,这是我的意思的一个小演示:

AsyncHttpClient client = new AsyncHttpClient();
RequestParams req = new RequestParams();
req.add("shareRegId","true");

client.post(AppConstants.SERVER_URL,req,new AsyncHttpResponseHandler(){
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        pg.hide();
        if(pg!=null){
            pg.dismiss();
        }
        Toast.makeText(getApplicationContext(),"ID sharing successful",Toast.LENGTH_LONG).show();
        Intent home = new Intent(getApplicationContext(),HomeActivity.class);
        home.putExtra("regID",registerID);
        Log.d("REGID",registerID);
        startActivity(home);
        finish();
    }

这将帮助您传递Post Params

关于java - Android 应用程序无法将详细信息发布到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36613846/

相关文章:

java - 访问 ArrayList 中对象的属性

php - 如何检查带参数的函数是否返回true

php并行支付-交易费

android - 如何在android中使用加速度计传感器计算运行速度

Firebase 数据上的 Android Searchview

java - 收到错误 10 后如何让 Google-Sign 正常工作?

java - Spring 集成错误 - 调度程序没有 channel 订阅者

java连接与mysql错误

php - 托管站点不使用 CSS/JQuery

android - Crosswalk 仅构建一个 APK 文件