Advantages and disadvantages of httpclient and httpurlconnection in Android and common bug Solutions

Source: Internet
Author: User

Official comments:

1) Apache httpclient is highly efficient and stable, but the maintenance cost is high. Therefore, the android development team is unwilling to maintain the library, but switched to httpurlconnection, Which is lighter;

2) httpurlconnection is lightweight, flexible, and easy to expand. It had such a bug before 2.2,
See http://code.google.com/p/android/issues/detail for details? Id = 2939

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 in httpurlconection after 3.0 and 4.0, such as HTTPS support,
In 4.0, cache support is also added, for example:

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 there are some bugs in this version:

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.

Personal Opinion:

My Opinions on Google's official developers are not similar. I am more inclined to use httpclient, because we can understand from the explanation of poolingclientconnectionmanager:

Manages a pool of {@ link operatedclientconnection client connections} and is able to service connection requests from multiple execution threads.
Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

This can save us time for frequent connection establishment. In our apps, we often use drop-down lists to call interfaces. The cost of repeatedly creating connections can be imagined.

Please pay attention to my later articles. I will make a comprehensive analysis on the httpclient 4.2 architecture of Apache.

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.