100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android网络连接的两种方法:apache client和httpurlconnection的比较

Android网络连接的两种方法:apache client和httpurlconnection的比较

时间:2021-12-19 19:53:33

相关推荐

Android网络连接的两种方法:apache client和httpurlconnection的比较

另见/mingli198611/article/details/8766585

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

原文见http://android-//09/androids-http-clients.html

这里小结下几点。

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

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

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

见/p/android/issues/detail?id=2939

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

Java代码 privatevoiddisableConnectionReuseIfNecessary(){ //HTTPconnectionreusewhichwasbuggypre-froyo if(Integer.parseInt(Build.VERSION.SDK)<Build.VERSION_CODES.FROYO){System.setProperty("http.keepAlive","false"); } }

3) 在Gingerbread中,httpurlconnection会增加对压缩报文头的处理,服务端可以用

GZIP,详细见:

/reference/java/net/HttpURLConnection.html

4) 对HTTPURLCONECTION中,在3。0后以及4。0中都进行了改善,比如对HTTPS的支持,

在4。0中,还增加了对缓存的支持呢!比如下面的代码:

Java代码 privatevoidenableHttpResponseCache() { try{ longhttpCacheSize=10*1024*1024;//10MiB FilehttpCacheDir=newFile(getCacheDir(),"http"); Class.forName(".http.HttpResponseCache").getMethod("install",File.class,long.class.invoke(null,httpCacheDir,httpCacheSize); } catch (ExceptionhttpResponseCacheNotAvailable){ } }

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

另有文章:

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。