我嘗試了很多使用HttpPost
發送數據的方法,但是我沒有成功,如果有人知道這個請幫忙嗎?在Android中使用HttpPost發佈JsonArray和字符串參數
我的服務網址爲:
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]
其工作正常,當我從瀏覽器但是從我的代碼data=[{}]
不發送擊中。
我最後的嘗試是:
1 --->
String serviceUrl = "http://xxxxx/xxx/abc.php";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("id", "xx");
httpPost.setHeader("name", "xxx");
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
2 --->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx"));
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
nameValuePairs.add(new BasicNameValuePair("data", jsonArr.toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(nameValuePairs.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
3 --->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx"));
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
輸出: 獲取響應相同我對瀏覽器的URL 'http://xxxxx/xxx/abc.php?id=xx&name=xxx'
(無數據參數),得到 我事情就意味着數據= [{}]不與來自我的代碼simple_string_parameters發送。
[看我這個答案](http://stackoverflow.com/questions/13134019/http-post-method-passing-null-values-to-the-server/13134287#13134287) –
你得到什麼問題與所有這些方法發送json字符串到服務器? –
感謝chintan,我看到了你的答案,我需要結合你的第一種和第二種方式,但是如何?我沒有得到。 – Deven