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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With