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
}
}
14 Comments
It looks really easy!
Nice, thanks!
Does this work for https hosts as well ?
how do i read the response? is there a way to get the returning url?
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-stringThanks you for this example ! finaly it's easy to implement http request into android application.
Be sure to execute this snippets from a async task (http://developer.android.com/reference/android/os/AsyncTask.html)
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)
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
How does HttpClient differ from AndroidHttpClient?
I really want to test this codes, Can you also publish the codes for script.php?
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:
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 ?
thx. great solution
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.