HTTP Post Request in Android Example

HTTP post request are executed in android in a very simple way. You need just 4 steps.

1. Declare Internet permissions in the manifest by adding the following line to AndroidManifest.xml.


<uses-permission android:name="android.permission.INTERNET" />


2. Create your HttpClient and HttpPost objects to execute the POST request.


HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);


String Object like String address = "www.google.com";

3. Set your POST data

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
post.setEntity(new UrlEncodedFormEntity(pairs));


4. Execute the POST request

This returns an HttpResponse object, whose data can be extracted and parsed.

HttpResponse response = client.execute(post);




No comments:

Post a Comment