STEPS
1
DECLARE INTERNET PERMISSIONS IN THE MANIFEST BY ADDING THE FOLLOWING LINE TO ANDROIDMANIFEST.XML. This allows your application to use any Internet connections.2
CREATE YOUR HTTPCLIENT AND HTTPPOST OBJECTS TO EXECUTE THE POST REQUEST. The address object is a String representing your POST's destination, such as a PHP page.HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
3
SET YOUR POST DATA. This is done by creating and setting a list of NameValuePairs as your HttpPost's entity. Be sure to catch the UnsupportedEncodingException thrown by HttpPost.setEntity().List pairs = new ArrayList();
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. Be sure to catch the ClientProtocolException and IOException thrown.HttpResponse response = client.execute(post);
THINGS YOU\'LL NEED
Java IDE
Android development tools (SDK, emulator, etc.)
Android developer's phone or other Android powered device (optional for testing)
Internet connection (for testing)
ARTICLE INFO
Categories: Programming | Android
Recent edits by: Hinni, Maluniu, Monica
In other languages:
Español: Cómo ejecutar solicitudes HTTP POST en Android
Discuss
No comments:
Post a Comment