我有一個Android應用發佈到「http://jont.byethost3.com/gcmtest/gcm.php?shareRegId=true」。一旦訪問了該應用程序,該應用程序就會向該腳本發佈一個id密鑰,並且該php文件會將其保存到我的文件服務器上的文本文件中。 我已經手動輸入到我的瀏覽器的url,它成功地生成一個空的文本文件,因爲沒有收到任何POST數據,但是當我在我的應用程序上測試它時,它不起作用。Android應用無法發佈詳細信息到網址
我用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服務器在本地工作,但問題仍然存在時,我將其上傳到此主機。
你是如何在這裏傳遞參數?通過追加到URL? –
您是通過追加到URL並通過郵件將數據傳遞到URL?這就是我想的問題 –
你好,謝謝你的回覆。我在問題中編輯了我的代碼。我通過url – JianYA