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


Android app unable to post details to url

我有一个Android应用程序,可以发布到"http://jont.byethost3.com/gcmtest/gcm.php?shareRegId=true".访问后,应用程序会向脚本发布一个id密钥,php文件会将其保存到我的文件服务器上的文本文件中。我已经在浏览器中手动输入了url,由于没有收到任何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;
    }

我的舱单以防万一

 <?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参数,您可以使用提供的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