2011-05-17 145 views
33

我寫的代碼是應該採取數據的Android應用程序,將其打包爲JSON並將其發佈到Web服務器,這反過來應該用JSON迴應。使用的HttpClient和HttpPost在Android中使用POST參數

使用GET請求工作正常,但使用POST所有數據由於某種原因似乎得到剝離和服務器沒有收到任何東西。

這裏的代碼片段:

HttpParams params = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(params, 5000); 
HttpConnectionParams.setSoTimeout(params, 5000);   
DefaultHttpClient httpClient = new DefaultHttpClient(params); 
BasicCookieStore cookieStore = new BasicCookieStore(); 
httpClient.setCookieStore(cookieStore); 

String uri = JSON_ADDRESS; 
String result = ""; 
String username = "user"; 
String apikey = "something"; 
String contentType = "application/json"; 

JSONObject jsonObj = new JSONObject(); 

try { 
    jsonObj.put("username", username); 
    jsonObj.put("apikey", apikey); 
} catch (JSONException e) { 
    Log.e(TAG, "JSONException: " + e); 
} 

HttpPost httpPost = new HttpPost(uri); 
List<NameValuePair> postParams = new ArrayList<NameValuePair>(); 
postParams.add(new BasicNameValuePair("json", jsonObj.toString())); 
HttpGet httpGet = null; 
try { 
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams); 
    entity.setContentEncoding(HTTP.UTF_8); 
    entity.setContentType("application/json"); 
    httpPost.setEntity(entity); 

    httpPost.setHeader("Content-Type", contentType); 
    httpPost.setHeader("Accept", contentType); 
} catch (UnsupportedEncodingException e) { 
    Log.e(TAG, "UnsupportedEncodingException: " + e); 
} 

try { 
    HttpResponse httpResponse = httpClient.execute(httpPost); 
    HttpEntity httpEntity = httpResponse.getEntity(); 

    if (httpEntity != null) { 
     InputStream is = httpEntity.getContent(); 
     result = StringUtils.convertStreamToString(is); 
     Log.i(TAG, "Result: " + result); 
    } 
} catch (ClientProtocolException e) { 
    Log.e(TAG, "ClientProtocolException: " + e); 
} catch (IOException e) { 
    Log.e(TAG, "IOException: " + e); 
} 

return result; 

我想我已經按照如何創建參數,並張貼他們的一般準則,但顯然不是。

任何幫助或指針在那裏我能找到一個解決方案,都非常歡迎在這一點上(花幾個小時實現無後的數據後,曾發送)。真正的服務器在Tomcat上運行Wicket,但我也在一個簡單的PHP頁面上測試過,沒有任何區別。

+2

**更新與解決方案_(在這種特殊情況下的工作至少對我來說)_:**我刪除了所有方法調用集'Content-Type',現在它可以工作。在這種情況下唯一允許的內容類型方法調用是'httpPost.setHeader(「Accept」,contentType);'。 – Patrick 2011-05-17 14:17:45

+0

這是因爲UrlEncodedFormEntity將內容類型設置爲application/x-www-form-urlencoded。如果您將內容類型更改爲其他內容(例如application/json),服務器將無法解析您的請求參數。 – redochka 2011-11-19 09:43:19

回答

28

你嘗試過這樣做沒有JSON對象並通過了兩項basicnamevaluepairs? 還,它可能是與你的serversettings

更新: 這是一段代碼我使用:

InputStream is = null; 
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); 

try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(connection); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     Log.d("HTTP", "HTTP: OK"); 
    } catch (Exception e) { 
     Log.e("HTTP", "Error in http connection " + e.toString()); 
    } 
+1

我試着只傳遞兩個'BasicNameValuePairs',但仍然沒有數據作爲POST發送。請問是否可以將請求作爲get發送,因此服務器不處理髮布數據? HttpPost應該確保沒有發生,不是嗎? – Patrick 2011-05-17 14:04:04

+0

@Patrick - 更新後的代碼 – Andreas 2011-05-17 15:25:07

+1

沒錯,這很像我結束了太多,刪除內容類型設置後的代碼(我用了太多明顯),正如我在原來的職位發表評論時提及。謝謝您的幫助。 – Patrick 2011-05-18 04:24:51

0

我剛剛檢查,我有相同的代碼,你和它工作perferctly。 唯一的區別就是我如何度過我的名單中PARAMS:

我使用的是:ArrayList<BasicNameValuePair> params

,並填寫這樣說:

params.add(new BasicNameValuePair("apikey", apikey); 

我不使用任何的JSONObject發送PARAMS到web服務。

你有義務使用JSONObject嗎?

+1

由於我正在與Iphone開發人員並行工作,他的應用程序實際上發佈了數據,所以除了將數據提交爲JSON之外,我沒有其他選擇。我試着只發送兩個值對,但沒有區別。 – Patrick 2011-05-17 14:05:38

44

實際上,你可以把它作爲JSON方式如下:

// Build the JSON object to pass parameters 
JSONObject jsonObj = new JSONObject(); 
jsonObj.put("username", username); 
jsonObj.put("apikey", apikey); 
// Create the POST object and add the parameters 
HttpPost httpPost = new HttpPost(url); 
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); 
entity.setContentType("application/json"); 
httpPost.setEntity(entity); 
HttpClient client = new DefaultHttpClient(); 
HttpResponse response = client.execute(httpPost); 
+2

謝謝!使用namevaluepairs不適合我,但這是正確的。 – htafoya 2013-07-08 15:17:30

+0

UrlEncodedFormEntity和StringEntity有什麼區別。 服務器是否需要不同的設置? 何時使用UrlEncodedFormEntity以及何時使用StringEntity? – Pankaj 2015-02-16 05:22:29