Android-based network access (I) Selection of Apache httpclient and httpurlconnection in Android

Source: Internet
Author: User

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
  1. Private void disableConnectionReuseIfNecessary (){
  2. // HTTP connection reuse which was buggy pre-froyo
  3. If (Integer. parseInt (Build. VERSION. SDK) <Build. VERSION_CODES.FROYO) {System. setProperty ("http. keepAlive", "false ");
  4. }
  5. }


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

  1. Private void enableHttpResponseCache ()
  2. {
  3. Try {
  4. Long httpCacheSize = 10*1024*1024; // 10 MiB
  5. File httpcachedir = new file (getcachedir (), "HTTP ");
  6. Class. forname ("android.net. http. httpresponsecache"). getmethod ("Install", file. Class, long. Class. Invoke (null, httpcachedir, httpcachesize );
  7. }
  8. Catch
  9. (Exception httpresponsecachenotavailable) & nbsp
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.