How to Select 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


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 

  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 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 

  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 ){
  10. }
  11. }


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

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.