Right way to manage HTTP connection in Android

时间秒杀一切 提交于 2019-12-24 14:15:29

问题


I have written two programs which handle the HTTP request. I wanted to know if one is better than other -

Program 1 (Using HttpURLConnection)

          URL url = new URL("https://www.google.com/");

          HttpURLConnection connection = (HttpURLConnection) url.openConnection();

          connection.setRequestMethod("GET");

          connection.setDoOutput(false);

          connection.connect();

          reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

          stringBuilder = new StringBuilder();

Program 2 (Using HttpPost)

         DefaultHttpClient httpClient = new DefaultHttpClient();

         HttpPost httpPost = new HttpPost("https://test.com");

         HttpResponse httpResponse = httpClient.execute(httpPost);

         InputStream inputStream = httpResponse.getEntity().getContent();

         InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

Also in program 2, I use a singleton to get the connection object. But in program 1 there is no global connection object and I need to recreate the HttpURLConnection object everytime I make a request. Please let me know if I am on the right track.

Thank You


回答1:


I would like to suggest you to use Android Asynchronous Http Client library. Then you can avoid these basic stuffs. The one things I like most is HTTP requests happen outside the UI thread.




回答2:


Also in program 2, I use a singleton to get the connection object. But in program 1 there is no global connection object and I need to recreate the HttpURLConnection object everytime I make a request.

Method 2 looks like simpler, but it's so old :

Apache HTTP Client - HTTPPost

DefaultHttpClient and its sibling AndroidHttpClient are extensible HTTP clients suitable for web browsers. They have large and flexible APIs. Their implementation is stable and they have few bugs. But the large size of this API makes it difficult for us to improve it without breaking compatibility. The Android team is not actively working on Apache HTTP Client.

HttpURLConnection

HttpURLConnection is a general-purpose, lightweight HTTP client suitable for most applications. This class has humble beginnings, but its focused API has made it easy for us to improve steadily.

Prior to Froyo, HttpURLConnection had some frustrating bugs.

We should choose method 1 when :

For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.

And method 2 when :

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

Thanks,



来源:https://stackoverflow.com/questions/20062061/right-way-to-manage-http-connection-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!