管理Android HTTP连接正道 [英] Right way to manage HTTP connection in Android

查看:144
本文介绍了管理Android HTTP连接正道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了两个程序,处理HTTP请求。我想知道,如果一个优于其他 -

程序1(使用HttpURLConnection类)

 网​​址URL =新的URL(https://www.google.com/);          HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();          connection.setRequestMethod(GET);          connection.setDoOutput(假);          connection.connect();          读者=新的BufferedReader(新的InputStreamReader(connection.getInputStream()));          StringBuilder的=新的StringBuilder();

计划2(使用HttpPost)

  DefaultHttpClient的HttpClient =新DefaultHttpClient();         HttpPost httpPost =新HttpPost(https://test.com);         HTT presponse HTT presponse = httpClient.execute(httpPost);         为InputStream的InputStream = HTT presponse.getEntity()的getContent()。         InputStreamReader的InputStreamReader的=新的InputStreamReader(InputStream的);

此外,在程序2,我用一个单独来获取连接对象。但在程序1没有全局连接对象,我需要重新创建HttpURLConnection的对象,每次我提出请求。请让我知道如果我在正确的轨道上。

感谢您


解决方案

  

此外,在程序2,我用一个单独来获取连接对象。但在程序1没有全局连接对象,我需要重新创建HttpURLConnection的对象,每次我提出请求。


2的方法貌似简单,但它是如此古老


  

的Apache HTTP客户端 - HTTPPost


  
  

DefaultHttpClient和它的兄弟AndroidHttpClient是可扩展的
  适用于Web浏览器的HTTP客户端。他们有很大的灵活
  蜜蜂。它们的实现是稳定的,他们有一些bug。但是,
  这个API的大尺寸使得我们很难对其进行改进,而不
  破坏兼容性。 Android团队不积极努力
  Apache的HTTP客户端。


  
  

HttpURLConnection类


  
  

HttpURLConnection类是一个通用的,重量轻的HTTP客户端
  适用于大多数应用。这个类有卑微,但
  其重点API使得我们很容易稳步提高。


  
  

在此之前的Froyo,HttpURLConnection类有一些令人沮丧的错误。


我们应该选择方法1时


  

有关姜饼更好,HttpURLConnection类是最好的选择。它的
  简单的API和小尺寸使得它非常适合于Android。透明
  COM pression和响应缓存减少网络的使用,提高速度和
  节省电池。新的应用程序应该使用HttpURLConnection的;它是
  在这里我们将花费我们的能源未来。


方法2时,


  

Apache的HTTP客户端对巧克力慕斯蛋糕和升级Froyo错误更少。正是由于这些版本的最佳选择。


谢谢,

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

解决方案

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,

这篇关于管理Android HTTP连接正道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆