2013-05-22 37 views
2

我嘗試了很多使用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發送。

+0

[看我這個答案](http://stackoverflow.com/questions/13134019/http-post-method-passing-null-values-to-the-server/13134287#13134287) –

+0

你得到什麼問題與所有這些方法發送json字符串到服務器? –

+0

感謝chintan,我看到了你的答案,我需要結合你的第一種和第二種方式,但是如何?我沒有得到。 – Deven

回答

3

試過很多方式使用HttpPost發送數據,但沒有成功

=>是它作爲你的web服務的URL明顯的是以下一個GET方法,而不是POST。

http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[ {.....}]

所以我建議你嘗試HTTPGet方法,而不是HTTPPost

檢查這個答案對adding parameters to a HTTP GET request in Android

+0

謝謝。是的,我也嘗試過使用url的HttpGet:String serviceUrl =「http://xxxxx/xxx/abc.php?id = xx&name = xxx&data =」+ jsonArr.toString();但不工作。 – Deven

+0

感謝Paresh現在使用「URLEncodedUtils」工作,但它使用HttpPost以及爲什麼...? – Deven

相關問題