我想將JSON文本發送到Web服務並讀取響應。我怎麼能從android做到這一點?哪些步驟,如創建請求對象,設置內容頭部等如何通過Android的HttpClient請求發送JSON對象?
我的代碼是在這裏
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i("tag", temp);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
我做了什麼錯誤PLZ糾正我,因爲它顯示了我一個壞請求錯誤 但是當我也張貼在海報它表明我的地位,全成200 OK
嘗試設置此類型'entity.setContentType(「application/json; charset = UTF-8」); \t \t \t \t entity.setContentEncoding(新BasicHeader(HTTP.CONTENT_TYPE, \t \t \t \t \t \t 「應用/ JSON的;字符集= UTF-8」));' – MKJParekh 2012-02-22 06:21:20
@Soni沒有它不適合我,我工作的罰款嘗試它,但它顯示我錯誤的請求錯誤 – 2012-02-22 06:29:18
@Soni唯一的錯誤做的是我沒有在下面的帖子中設置像這樣的後端httppost.setHeader(「Content-type」,「application/json」); – 2012-02-22 06:31:27