我有一个发布到“http://jont.byethost3.com/gcmtest/gcm.php?shareRegId=true”的Android应用。访问后,应用程序会向脚本发送一个id密钥,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;
}
我的清单以防万一
<?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服务器在本地使用此代码,当我将其上传到此主机时问题仍然存在。
答案 0 :(得分:0)
要使用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
,