android中httpclient和HttpURLConnection优缺点和常见bug解决方法

简介:

http://blog.csdn.net/androidzhaoxiaogang/article/details/8158122

官方意见:

1) apache httpclient比较稳定点,少BUG,但由于API的关系,扩展改造麻烦点,
所以android team现在不鸟这东西了基本

2) httpurlconnection比较轻便,灵活,易于扩展,在2。2前有个BUG,
见http://code.google.com/p/android/issues/detail?id=2939
  可以通过如下代码去解决:
 

[java]  view plain copy
  1. private void disableConnectionReuseIfNecessary() {    
  2.   // HTTP connection reuse which was buggy pre-froyo     
  3.  if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {        System.setProperty("http.keepAlive""false");     
  4.  }  
  5. }  


3) 在Gingerbread中,httpurlconnection会增加对压缩报文头的处理,服务端可以用
GZIP,详细见:
  http://developer.android.com/reference/java/net/HttpURLConnection.html

4) 对HTTPURLCONECTION中,在3。0后以及4。0中都进行了改善,比如对HTTPS的支持,
在4。0中,还增加了对缓存的支持呢!比如下面的代码:
[java]  view plain copy
  1. private void enableHttpResponseCache()   
  2. {    
  3.   try {  
  4.         long httpCacheSize = 10 * 1024 * 1024// 10 MiB      
  5.     File httpCacheDir = new File(getCacheDir(), "http");      
  6.     Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.classlong.class.invoke(null, httpCacheDir, httpCacheSize);     
  7.  }   
  8. catch   
  9. (Exception httpResponseCacheNotAvailable) {    
  10.   }  
  11. }  

  谷歌的的建议是,Gingerbread后的版本,都建议用httpurlconnection,获得更高的性能

5)在android SDk中httpclient使用的是4.0beta2,我不得不说这个版本里面有些蛋疼的bug:

I.auth caching;

II.在4.0上的sdk,将wifi和3g同时打开,理论上来说,网络接口应该走wifi,但是却走了代理,导致访问服务器网络失败;

解决上面问题的唯一办法就是引入“http://code.google.com/p/httpclientandroidlib/”中的库,然后修改相应的类,典型的例子就是ThreadSafeClientConnManager变成了PoolingClientConnectionManager

个人意见:

我对谷歌官方开发同事的意见有点不敢雷同,个人更倾向于使用httpclient,因为从PoolingClientConnectionManager得解释我们就可以知道:

Manages a pool of {@link OperatedClientConnection client connections} and is able to service connection requests from multiple execution threads.
Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

可以节省我们频繁建立连接的时间,往往在我们的app里面更多的情况是,不断的去下拉列表调用接口,反复创建连接的代价可想而知。

请注意关注我后面的文章,我会对apache的httpclient 4.2版本的架构做全面地分析。


相关文章
|
3月前
|
Android开发
【Bug】Android resource linking failed和error: failed linking references.
【Bug】Android resource linking failed和error: failed linking references.
|
5天前
|
Android开发
Android 盒子开发过程中遇到的问题及解决方法
Android 盒子开发过程中遇到的问题及解决方法
8 2
|
1月前
|
Android开发
Android事件冲突原理及解决方法
Android事件冲突原理及解决方法
19 0
|
9月前
|
XML Android开发 数据格式
Android 中使用HttpURLConnection进行网络请求详解
Android 中使用HttpURLConnection进行网络请求详解
136 0
|
9月前
|
Android开发
Android studio最新版本4.1.3使用过程中遇到的问题解决方法
Android studio最新版本4.1.3使用过程中遇到的问题解决方法
289 0
|
9月前
|
数据安全/隐私保护 Android开发
uniapp vue3版本 Android 引用 jsencrypt加密库 报错问题 “default“ is not exported by,解决方法
uniapp vue3版本 Android 引用 jsencrypt加密库 报错问题 “default“ is not exported by,解决方法
850 0
|
10月前
|
测试技术 Android开发
Android中如何使用HttpURLConnection来上传图片
目前有很多封装的框架,可以很简单的来实现,比如OkHttp,Retrofit等,那么用HttpURLConnection这个最基本的请求,如何实现上传一张图片呢?
142 0
|
12月前
|
Android开发
Android开发中Button背景颜色不能修改问题及解决方法
Android开发中Button背景颜色不能修改问题及解决方法
1222 0
|
12月前
|
XML Android开发 数据格式
Android Studio报Element XXXX must be declared的解决方法
Android Studio报Element XXXX must be declared的解决方法
160 0
|
12月前
|
Android开发
Bug日志(四)——Android 防止多次点击(另解决多个列表子项点击)
在用户使用 应用的时候,经常会出现点击过快且多次点击同一控件的情况, 一方面这是因为应用或手机当前有些卡顿,网络卡啥的 另一方面也可能是由于很多应用并没有设置按钮点击时的 selector 或者其它按钮响应方式(例如点击按钮时按钮放大,常见于游戏),导致用户误认为没有点击到当前按钮。(比如使用selector再点击后更换背景颜色,图片等等)