Executing a HTTP POST Request with HttpClient

19 votes · 14 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

14 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.