Executing a HTTP POST Request with HttpClient

32 votes · 29 comments

This code executes a HTTP POST request with org.apache.http.client.HttpClient. Could be used in combination with "Non-Blocking Web Requests".

raw ·
copy
· download
public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } }
Add a comment

29 Comments

It looks really easy!

Reply · May 6, 2009, 10:07 p.m.

Nice, thanks!

Reply · July 10, 2009, 11:44 p.m.

Does this work for https hosts as well ?

Reply · Aug. 13, 2009, 8:29 p.m.

how do i read the response? is there a way to get the returning url?

Reply · Aug. 20, 2009, 1:14 a.m.

You can read the content (which is an InputStream) with this snippet: http://www.androidsnippets.com/get-the-content-from-a-httpresponse-or-any-inputstream-as-a-string

Reply · March 8, 2011, 11:08 p.m.

Thanks you for this example ! finaly it's easy to implement http request into android application.

Reply · March 14, 2011, 8:52 a.m.

Be sure to execute this snippets from a async task (http://developer.android.com/reference/android/os/AsyncTask.html)

Reply · March 25, 2011, 8:18 p.m.

Im a real beginner so please help me. I tried to use this code (and yes I imported the httpclient library) but it says that nearly all variables "CANNOT BE RESOLVED BY A TYPE" (httpost, namevaluepairs, HttpResponse, etc)

Reply · April 13, 2011, 5:55 p.m.

I guess you are missing the required includes. Try Adding:

import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair;

on Netbeans its simply done by hitting ctrl+shift+i

Reply · April 30, 2011, 1 p.m.

How does HttpClient differ from AndroidHttpClient?

Reply · May 31, 2011, 4:08 a.m.

I really want to test this codes, Can you also publish the codes for script.php?

Reply · June 19, 2011, 5:18 a.m.

This may seem obvious, but it took me awhile to figure out why this snippet was causing my program to crash. You have to remember to declare permission to use the Internet in AndroidManifest.xml:

Reply · June 19, 2011, 7:03 a.m.

I tested this code with Android 2.1 and works file, but in Android 2.3.3 it says not responding and crash the program. Please help me. How can I get this done in Android 2.3.3 ?

Reply · July 3, 2011, 11:15 a.m.

thx. great solution

Reply · Oct. 21, 2011, 5:49 p.m.

This code works, but is unusable for me because for some reason it is taking too long for the execute to run. I'm trying to log data once per second but execute is taking on average 2-3 seconds. I'm not sure why this is, because I've an AppInventor app that uses the exact same server and executes the POST nearly instantly (I give haptic feedback when the POST is complete and I've never been able to discern any delay between when I touch the screen and get the feedback).

I found this thread on stackoverflow: http://stackoverflow.com/questions/3046424/http-post-requests-using-httpclient-take-2-seconds-why but unfortunately none of the suggestions work for me, and I am unable to reply to answers to comment that this is the case for me.

Reply · Oct. 30, 2011, 4:21 a.m.

Nice, thank you very much. http://www.fashionnewstore.com/

Reply · March 3, 2012, 3 a.m.
Reply · March 3, 2012, 3 a.m.

Hi Guys, awesome tutorial but I just need to know what do I put in script.php ..... i don't know much about web services because I am a game programmer so please can someone just help

Reply · March 10, 2012, 1:19 p.m.

Do I just receive it like a form data as we do in php or like an array...

Reply · March 10, 2012, 1:20 p.m.

What will Android send and How do I receive it in PHP.....plz guys need help...this is urgent

Reply · March 10, 2012, 5 p.m.

Nice, thanks.

Reply · March 12, 2012, 2:57 a.m.

Hi it works for me if the service is having request params....i would like to know if the service is having request body then how to build it...can u pls help me to do... my service is like this @RequestMapping(value = "/emp/update/", method = RequestMethod.POST) @ResponseBody public Service updateEmpInfo(@RequestBody Employee emp) how can i pass emp object as request body

Reply · May 30, 2012, 8:48 a.m.

nice info. for aplication, please http://id-harga.blogspot.com/

Reply · June 9, 2012, 12:14 a.m.

Not working for me, i need to make a request to external server (www.example.com). The problem is that I need to send POST parameters and i've been searching some info and i found something about cross-domain or something like that. Could someone help me to set to the request and get answer? the problem is that the parameters are not coming to the url (www.example.com)

I tried to do the following code but not working:

Reply · Aug. 6, 2012, 2:04 p.m.

Have you solved this problem? I am having the same problem. The query paramaters are not being passed as part of the post when I run httpclient.execute(httppost);

However when I run EntityUtils.toString(httppost.getEntity()); I do get the parameters as expected but they are not being passed???

Reply · May 4, 2013, 9:10 p.m.

I'm using google gcm for sending notification,And I'm using this piece of code for HTTP POST Request with HttpClient : here is my code :

`List nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("employeeid", getempid)); nameValuePairs.add(new BasicNameValuePair("password", getpsssword)); // String URL_STRING="http://192.168.0.151:8080/map/jsp/passwordget.jsp"; String URL_STRING="http://192.168.1.111:8080/gcmserver/HomeServlet.java";

        HttpClient hc = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL_STRING);
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
        HttpResponse rp = hc.execute(post);

` Now I exactly want to retrive the value from this page whatever is assigning to it (here:"employeeid","password").

I'm also posting this point in stackoverflow,here is the Link

Is their any way to achieve these values from this page ......//

Reply · Sept. 26, 2012, 12:43 p.m.

i get null pointer exception but i write correctly

Reply · Jan. 9, 2014, 4:42 p.m.

How I can change the url assigned to the httpPost method as a paramater on a button click?

Reply · May 6, 2014, 8:43 a.m.

Hey all, quick question from a noob, is there something noobish I don't know about running this correctly? I have this function execute on a click of a button and it crashes my app, I get that "Unfortunately blah blah stop responding" flash and go and I don't even know where to look for errors and such...? Any help greatly appreciated... I am soooo close to finishing this app.... :(

Reply · July 3, 2014, 4:32 p.m.

If my web method not needs any parameter, what i can send on "setEntity" ?

Reply · July 15, 2014, 6:19 p.m.