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

Android and Web Request (GET)

Android 2.2, Galaxy Tab, API Version 8

It’s easy with apache http client, see below code

//getResponse from any URL
private String getResponse(uRL) throws ClientProtocolException, IOException
{
String strRepsonse = “”;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(uRL));
StatusLine statusLine = response.getStatusLine();
//response.getEntity().getContent().re
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;
}

I hope this is to the point 🙂

Thanks,
Riz

Eclipse And Galaxy Tab

When developing applications with Android you normally use the the AVDs. You can also hook to Galaxy Tab by following:

  1. On Galaxy Tab, From Home go to “Settings”
  2. Click on “Applications”
  3. Then Click on “Development”
  4. You will get options for USB Debugging, Stay Awake, and Allow mock locations.
  5. Turn on USB Debugging, this will trigger the USB Driver installation at your system.
  6. Now, the Your Galaxy Tab will be avaiable for deploying applications directly through eclipse.
  7. IMPORTANT – Turn Off Automatic Select at AVD Manager when you want to use galaxy tab for developnent.

Notice, this method works FAST, as compared to AVD.

I hope this will people who are wondering how to do it.

Thanks,