Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dfference between using URLCOnnection Object and Httppost in android client

I am using URLConnection Object to send data from my android client to server.

URL url = new URL("http://10.0.2.2:8080/hello");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
ObjectOutputStream out=new ObjectOutputStream(connection.getOutputStream());
String s="check"+","+susername;
out.writeObject(s);
out.flush();
out.close();

But I have seen many android programs sending data using httppost in the following way.

HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(LOGIN_ADDRESS);
List pairs=new  ArrayList();
String strUsername=username.getText().toString();
String strPassword=password.getText().toString();
pairs.add(new BasicNameValuePair("username", strUsername));
pairs.add(new BasicNameValuePair("password", strPassword));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response= client.execute(httpPost);

please explain the differnce between the two. How would you receive the data in the later case in a servlet. please give a brief explanation on this HttpPost. In the internet all I find is code. pls give a step by step explanation on HttpPost and its methods and how should the data be received in the servlet. Links will do fine.

like image 853
Ashwin Avatar asked Jan 18 '23 10:01

Ashwin


1 Answers

This blog post does a pretty good job of explaining the difference between the two of them (well actually HttpURLConnection, but that's just a subclass of URLConnection). Some highlights from the article are:

  • HttpURLConnection easily allows gzip encoding
  • HttpURLConnection can allow for easy caching of results
  • HttpURLConnection is newer and being actively developed on so it's only going to get faster and better
  • HttpURLConnection has some annoying bugs on foryo and pre-froyo platforms
  • HttpClient is tried and true. It's been around for a long time and it works
  • HttpClient is pretty much not being developed on because it's so old and the API is completely locked down. There isn't much more the android developers can do in terms of making it better.

While the end of the article recommends use of HttpURLConnection on all platforms above froyo, I personally like using HttpClient no matter what. It's just easier to use for me and makes more sense. But if you're already using HttpURLConnection, you should totally keep using it. It's going to be receiving lot's of love from the android developers from here-on-out.

like image 165
Kurtis Nusbaum Avatar answered Feb 12 '23 16:02

Kurtis Nusbaum