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
}
}
25 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.
Nice, thank you very much. http://www.fashionnewstore.com/
Nice, thank you very much.
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
Do I just receive it like a form data as we do in php or like an array...
What will Android send and How do I receive it in PHP.....plz guys need help...this is urgent
Nice, thanks.
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
nice info. for aplication, please http://id-harga.blogspot.com/
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:
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???
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";
` 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 ......//