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
Here we will summarize the following points.
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. There was a BUG before,
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 will increase the processing of the compressed packet header. The server can use
For details, see:
Http://developer.android.com/reference/java/net/HttpURLConnection.html
4) for HTTPURLCONECTION, in 3. After 0 and 4. For example, HTTPS support,
At 4. 0, and 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) & nbsp