深入学习Android有关网络连接的内容
基于实际项目中碰到的些许问题,开始重新深入学习Android网络连接有关知识。
Android的4.1.2版本的AndroidHttpClient模块setSoTimeout之后实际超时时间和设置的时间不一致现象 不知何种原因,高版本平台使用AndroidHttpClient(setSoTimeout设置的超时时间为60秒),实际反应出的超时时间不一致,需要4分钟才超时。 设备1:U880(2.2平台)实际超时时间 = setSoTimeout的时间 设备2:J2(4.1.2平台)实际超时时间 = setSoTimeout的时间 * 4倍
参考资料:
-
Connecting to the Network | Android Developers
http://developer.android.com/intl/zh-cn/training/basics/network-ops/connecting.html - 官方讨论博客:选择最佳HTTP Client
http://android-developers.blogspot.com/2011/09/androids-http-clients.html(需自带梯子)
Android访问网络,使用HttpURLConnection还是HttpClient? - 郭霖的专栏 - 博客频道 - CSDN.NET(翻译好的)
Which client is best? Apache HTTP client has fewer bugs on Eclair(2.0) and Froyo(2.2). It is the best choice for these releases. For Gingerbread(2.3) 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.
- 官方API文档 | Android Developers
http://developer.android.com/intl/zh-cn/reference/java/net/HttpURLConnection.html
-
开源的针对 HttpURLConnection 的封装,里面还有介绍许多别的开源组件
hgoebl/DavidWebb · GitHub
https://github.com/hgoebl/DavidWebb#not-for-you -
较简单的介绍
How to use java.net.URLConnection to fire and handle HTTP requests? - Stack Overflow - 较简单的封装
basic-http-client - Basic HTTP client w/ Android AsyncTask wrapper - Google Project Hosting
知识小结:
-
- Android的HTTP Client 有:
1.Apache 的DefaultHttpClient 或 Android基于此再封装一次的 AndroidHttpClient
2.JAVA 的 HttpURLConnection,不过兼容低版本时需注意的有:
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"); } }
- Android的HTTP Client 有:
作者:Asion Tang
凡是没有注明[转载]的文章,本Blog发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。