您的位置:首页 > 理论基础 > 计算机网络

深入学习Android有关网络连接的内容

2014-07-01 16:06 423 查看

基于实际项目中碰到的些许问题,开始重新深入学习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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: