Android and HTTP POST

Hi Guyz,

In Last Post Code Snipped was for the GET Request Method from any provided URL You can find it here.

Now, Let’s POST something to some address, below is code for it

private String postData(String URL, String xml) throws ClientProtocolException, IOException
String strRepsonse = “”;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL); //url to post information or any one you have
ByteArrayEntity bytes = new ByteArrayEntity(xml.getBytes());
bytes.setContentType(“application/xml”); // set approporiate content type
httppost.setEntity(bytes); //set entity type to post
HttpResponse response = httpclient.execute(httppost); //import line of code
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
BufferedReader rd = new BufferedReader(new
String line = “”;
while ((line = rd.readLine()) != null) {
strRepsonse += line;
return strRepsonse;

If you’re using local address than you need to use IP Address of the server like


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.