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.

4
  • see my this answer Commented May 22, 2013 at 5:55
  • what problem u are getting with all these methods for sending json string to server? Commented May 22, 2013 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 . Commented May 22, 2013 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. Commented May 22, 2013 at 6:39

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. yes i also tried HttpGet with url : String serviceUrl = "xxxxx/xxx/abc.php ?id=xx&name=xxx&data="+jsonArr.toString(); but not working.
Thanks Paresh now its working for me using "URLEncodedUtils", But it works using HttpPost as well why...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.