2014-08-29 85 views
1

我必須爲我的應用程序提出一些客戶端 - 服務器請求。它的工作原理,但我不能用PHP讀取我的服務器中發佈的參數。沒有錯誤或例外。我嘗試了很多不同的方式,但它仍然不起作用。HttpPost參數不起作用

private class NetworkProvider extends AsyncTask<NetworkingParams, String, String> { 

     @Override 
     protected String doInBackground(NetworkingParams... networkingParams) { 
      String url = networkingParams[0].getUrl(); 
      String methodName = networkingParams[0].getMethodName(); 
      JSONObject params = networkingParams[0].getParamJSON(); 
      InputStream inputStream; 
      HttpPost httpPost; 
      HttpResponse httpResponse; 
      String result = "inputStream is null"; 

      HttpClient httpClient = new DefaultHttpClient(); 

      try { 
       params.put("method", methodName); 
       params.put("uNameDecoded", uNameDecoded); 
       params.put("uPassDecoded", uPassDecoded); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      httpPost = createPostForJSONObject(params, url); 

      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Content-type", "application/json"); 

      try { 
       httpResponse = httpClient.execute(httpPost); 
       inputStream = httpResponse.getEntity().getContent(); 
       if (inputStream != null) 
        result = convertInputStreamToString(inputStream); 
       return result; 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     private HttpPost createPostForJSONObject(
       JSONObject params, String url) { 
      HttpPost post = new HttpPost(url); 
      post.setEntity(createStringEntity(params)); 
      return post; 
     } 

     private String convertInputStreamToString(InputStream inputStream) throws IOException{ 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      String line = ""; 
      String result = ""; 
      while((line = bufferedReader.readLine()) != null) 
       result += line; 

      inputStream.close(); 
      return result; 
     } 

     private HttpEntity createStringEntity(JSONObject params) { 
      StringEntity se = null; 
      try { 
       se = new StringEntity(params.toString()); 
      } catch (UnsupportedEncodingException e) { 
       System.out.println("error creating entity"); 
      } 
      return se; 
     } 
    } 
+1

您是否檢查參數是否作爲請求正文的一部分發送?你有沒有檢查參數是否被使用PHP的服務器所接受?你可以記錄任何(包括錯誤的)POST請求給你的PHP應用程序嗎? – 2014-08-29 20:48:34

+0

參數在httpClient內。服務器沒有得到任何參數。我能夠記錄一個請求。服務器響應有效,但錯誤的數據又回來了。 – 2014-08-29 21:04:27

+0

在創建'HttpPost'元素之前,在'se = new StringEntity(params.toString());'處添加一個斷點或記錄'params.toString()'的內容,以檢查是否正在發送數據以正確的格式。此外,再次通過代理或其他方法檢查請求是如何完成的。如果這*符合預期*,那麼問題可能出現在您的PHP應用程序中。使用Chrome等DHC的RESTful服務測試程序應用程序來測試您的PHP服務。 – 2014-08-29 21:06:49

回答

0

問題是,數據以JSON格式發送到服務器,這對於PHP的$ POST而言並不常見。現在我不使用'POST',而是使用'HTTP_RAW_POST_DATA'。完美的作品。 這是我調試的PHP POST請求的身體:

$entityBody = file_get_contents('php://input'); 
echo $entityBody; 

爲PHP $ POST結構應該是這樣的:「一個=價值」。不是「{」a「:」值「}」。 Java代碼是正確的。