Dfference between using URLCOnnection Object and Httppost in android client

I am using a URLConnection object to send data from my android client to the 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 as follows.

 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 difference between the two. How do you get the data in the latter case in the servlet. give a brief description of this HttpPost. On the Internet, all I find is code. PLS gives a step-by-step explanation of HttpPost and its methods and how they should receive data in the servlet. Links will be fine.

+4
source share
1 answer

This blog post pretty well explains the difference between the two (well, actually, HttpURLConnection, but it's just a subclass of URLConnection). Some highlights of the article:

  • HttpURLConnection easily enables gzip encoding
  • HttpURLConnection can provide simple caching of results.
  • HttpURLConnection is newer and actively developing, so it will only be faster and better
  • HttpURLConnection has some annoying bugs on the foryo and pre-froyo platforms.
  • HttpClient checked and right. It has been a long time and it works.
  • HttpClient is largely not developed because it is so old and the API is completely blocked. There are not many Android developers to make them better.

At the end of the article, it is recommended to use HttpURLConnection on all platforms above froyo, I personally like to use HttpClient no matter what. It is just easier to use for me and makes more sense. But if you are already using HttpURLConnection, you should use it fully. He's going to get a lot of love from Android developers from here.

+9
source

Source: https://habr.com/ru/post/1397699/


All Articles