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
可以通过如下代码去解决:
- private void disableConnectionReuseIfNecessary() {
- // HTTP connection reuse which was buggy pre-froyo
- if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) { System.setProperty("http.keepAlive", "false");
- }
- }
3) 在Gingerbread中,httpurlconnection会增加对压缩报文头的处理,服务端可以用
GZIP,详细见:
http://developer.android.com/reference/java/net/HttpURLConnection.html
4) 对HTTPURLCONECTION中,在3。0后以及4。0中都进行了改善,比如对HTTPS的支持,
在4。0中,还增加了对缓存的支持呢!比如下面的代码:
- private void enableHttpResponseCache()
- {
- try {
- * 1024 * 1024; // 10 MiB
- File httpCacheDir = new File(getCacheDir(), "http");
- Class.forName("android.net.http.HttpResponseCache").getMethod("install", File.class, long.class.invoke(null, httpCacheDir, httpCacheSize);
- }
- catch
- (Exception httpResponseCacheNotAvailable) {
- }
- }
最后的建议,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.
最新文章
- Python 历遍目录
- Mybatis中javaType和jdbcType对应关系
- python日常-int和float
- python中isort的使用
- Oracle 数据泵文件
- shell script针对参数已经有配置好变量名称
- 项目ppt演讲与阶段性总结
- Upcase 将edit1中的每个字符串改为首字母大写
- js 等待刷新技术
- 关于Android App开发技术分类的一个小总结
- 8、Dockerfile详解
- 3-Longest Substring Without Repeating Characters @LeetCode
- 2017-2018-2 20165312 实验三《敏捷开发与XP实践》实验报告
- hanlp提取文本关键词的使用方法记录
- 《Effective Java》笔记-服务提供者框架
- Sharepoint claim认证的login name
- linux 配置文件(启动文件、环境文件)启动顺序
- Git远程操作详解【转】
- jdk各版本
- SQL Server之看懂执行计划