2

I tried lots of ways to send data using HttpPost but I haven't succeeded, if anybody knows about this please help?

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

its working fine when I hit from browser but from my code data=[{}] are not sending.

My last attempts are :
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);

Output : Getting response same as that i got with url 'http://xxxxx/xxx/abc.php?id=xx&name=xxx' (without data parameter) on browser, i thing it means data=[{}] are not sending with simple_string_parameters from my code.

Deven
  • 3,078
  • 1
  • 32
  • 34
  • [see my this answer](http://stackoverflow.com/questions/13134019/http-post-method-passing-null-values-to-the-server/13134287#13134287) – Chintan Khetiya May 22 '13 at 05:55
  • what problem u are getting with all these methods for sending json string to server? – ρяσѕρєя K May 22 '13 at 05:55
  • thanks chintan, i saw your answer, i thing i need to combine your 1st and 2nd way but how ? i'm not getting . – Deven May 22 '13 at 06:32
  • thanks ρяσѕρєя K, problem is : i am able to post either simple strings or json encoded data to server but unable to post both as in my service url. – Deven May 22 '13 at 06:39

1 Answers1

3

Tried lot of way to send data using HttpPost but not succeed

=> Yes its obvious as your webservice URL is following a GET method, not a POST.

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

So I would suggest you to try HTTPGet method instead of HTTPPost.

Check this answer for adding parameters to a HTTP GET request in Android?

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • thanks. yes i also tried HttpGet with url : String serviceUrl = "http://xxxxx/xxx/abc.php ?id=xx&name=xxx&data="+jsonArr.toString(); but not working. – Deven May 22 '13 at 06:18
  • Thanks Paresh now its working for me using "URLEncodedUtils", But it works using HttpPost as well why...? – Deven May 22 '13 at 09:26