另见http://blog.csdn.net/mingli198611/article/details/8766585

在官方blog中,android工程师谈到了如何去选择apache client和httpurlconnection的问题: 

  原文见http://android-developers.blogspot.com/2011/09/androids-http-clients.html 

这里小结下几点。 



1) apache httpclient比较稳定点,少BUG,但由于API的关系,扩展改造麻烦点, 

所以android team现在不鸟这东西了基本 



2) httpurlconnection比较轻便,灵活,易于扩展,在2。2前有个BUG, 

见http://code.google.com/p/android/issues/detail?id=2939 

  可以通过如下代码去解决:

  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中,还增加了对缓存的支持呢!比如下面的代码:

  1. private void enableHttpResponseCache()
  2. {
  3. try {
  4. * 1024 * 1024; // 10 MiB
  5. File httpCacheDir = new File(getCacheDir(), "http");
  6. Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class.invoke(null, httpCacheDir, httpCacheSize);
  7. }
  8. catch
  9. (Exception httpResponseCacheNotAvailable) {
  10. }
  11. }

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




另有文章:

以下是官方原文(地址:http://developer.android.com/training/basics/network-ops/connecting.html

---------------原文-------------------------

Most network-connected Android apps use HTTP to send and receive data. Android includes two HTTP clients: HttpURLConnection and
Apache HttpClient.
Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling. We recommend using HttpURLConnection for
applications targeted at Gingerbread and higher. For more discussion of this topic, see the blog post Android's
HTTP Clients
.


最新文章

  1. Python 历遍目录
  2. Mybatis中javaType和jdbcType对应关系
  3. python日常-int和float
  4. python中isort的使用
  5. Oracle 数据泵文件
  6. shell script针对参数已经有配置好变量名称
  7. 项目ppt演讲与阶段性总结
  8. Upcase 将edit1中的每个字符串改为首字母大写
  9. js 等待刷新技术
  10. 关于Android App开发技术分类的一个小总结
  11. 8、Dockerfile详解
  12. 3-Longest Substring Without Repeating Characters @LeetCode
  13. 2017-2018-2 20165312 实验三《敏捷开发与XP实践》实验报告
  14. hanlp提取文本关键词的使用方法记录
  15. 《Effective Java》笔记-服务提供者框架
  16. Sharepoint claim认证的login name
  17. linux 配置文件(启动文件、环境文件)启动顺序
  18. Git远程操作详解【转】
  19. jdk各版本
  20. SQL Server之看懂执行计划

热门文章

  1. 迷宫问题python实现(靠右手摸墙)
  2. Codeforces 463D Gargari and Permutations
  3. 能让汇编转到C51的初学者有更清晰的认识的一篇文章
  4. 《Programming WPF》翻译 第9章 3.自定义功能
  5. 【转】网络视频监控P2P解决方案
  6. python3-day1(文件操作)
  7. A星算法
  8. python list 去重
  9. 修改UISearchBar输入框字体颜色
  10. jQuery插件Jeditable的使用(Struts2处理)