2016-04-14 64 views
1

我有一個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服務器在本地工作,但問題仍然存在時,我將其上傳到此主機。

+0

你是如何在這裏傳遞參數?通過追加到URL? –

+0

您是通過追加到URL並通過郵件將數據傳遞到URL?這就是我想的問題 –

+0

你好,謝謝你的回覆。我在問題中編輯了我的代碼。我通過url – JianYA

回答

0

爲了通過post數據使用AsyncAndroidHttp庫,你可以使用提供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,

+0

我已經添加了我的PHP腳本。它應該使用GET來檢查shareRegId是否爲空,然後從POST獲取數據。到目前爲止,它似乎並沒有聯繫該網站。 – JianYA

+0

問題是,我不知道client.post是否能夠發送任何信息,除了在PHP端創建的文本文件。目前我所有的權限都是正確的,但沒有生成文本文件 – JianYA

+0

做了一件事,暫時將'post'改爲'get',並在php腳本中使用'$ _GET'來獲取這兩個參數並看看會發生什麼 –

相關問題