Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
see my this answer –  chintan khetiya May 22 at 5:55
 
what problem u are getting with all these methods for sending json string to server? –  ρяσѕρєя K May 22 at 5:55
 
thanks chintan, i saw your answer, i thing i need to combine your 1st and 2nd way but how ? i'm not getting . –  kramk May 22 at 6: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. –  kramk May 22 at 6:39

1 Answer

up vote 3 down vote accepted

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?

share|improve this answer
 
thanks. yes i also tried HttpGet with url : String serviceUrl = "xxxxx/xxx/abc.php ?id=xx&name=xxx&data="+jsonArr.toString(); but not working. –  kramk May 22 at 6:18
 
Thanks Paresh now its working for me using "URLEncodedUtils", But it works using HttpPost as well why...? –  kramk May 22 at 9:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.