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. http://wp.me/p1vGA-L

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 www.contoso.com 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
InputStreamReader(response.getEntity().getContent()));
String line = “”;
while ((line = rd.readLine()) != null) {
strRepsonse += line;
}
}
return strRepsonse;
}

NOTE:
If you’re using local address than you need to use IP Address of the server like http://192.168.10.1/YourService.svc

Thanks,
Riz