On the official blog, Android engineers talked about how to choose Apache client and httpurlconnection:
See http://android-developers.blogspot.com/2011/09/androids-http-clients.html for more information
Summary:
1) Apache httpclient is relatively stable and has few bugs. However, due to the API relationship, it is difficult to scale up and transform,
Therefore, the android team is no longer familiar with this.
2) httpurlconnection is lightweight, flexible, and easy to expand, at 2. 2 before there is a bug, see http://code.google.com/p/android/issues/detail? Id = 2939
You can solve the problem by using the following code:
Java code
- 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) In gingerbread, httpurlconnection adds processing for the compressed packet header. The server can use gzip. For details, see:
Http://developer.android.com/reference/java/net/HttpURLConnection.html
4) improvements have been made to httpurlconection after 3.0 and 4.0, such as HTTPS support,
In 4.0, we also added support for caching! For example, the following code:
Java code
- Private void enablehttpresponsecache ()
- {
- Try {
- Long httpcachesize = 10*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 ){
- }
- }
5) in the android SDK, httpclient uses 4.0beta2. I have to say that this version contains some bad bugs:
I. Auth caching;
Ii. In SDK 4.0, WiFi and 3G are enabled at the same time. Theoretically, the network interface should go through WiFi but go through proxy, leading to network access failure;
The only way to solve the above problem is to introduce.
----------------------------
Reprinted from:
Http://jackyrong.iteye.com/blog/1228220
Http://blog.csdn.net/androidzhaoxiaogang/article/details/8158122