Turn-android networking-httpurlconnection and httpclient choose which good?

Source: Internet
Author: User

Http://www.ituring.com.cn/article/199619?utm_source=tuicool

In Android development, access to the network we choose HttpURLConnection or httpclient good? What is the difference between these two types of network protocols? Let's look at it.

1, HttpClient

HttpClient has two specific implementations of defaulthttpclient and Androidhttpclient, all of which have many APIs and are fairly stable and have few bug counts.

But it is also because the HttpClient API is too numerous to make it difficult to upgrade and expand without breaking compatibility, so the Android development team is not actively working on upgrading and optimizing httpclient.

2, HttpURLConnection

HttpURLConnection is a versatile, lightweight HTTP client that can be used for HTTP operations for most applications. Although the HttpURLConnection API provides a relatively simple, it also makes it easier to use and extend it.

But before the Android 2.2 version, HttpURLConnection has been having some annoying bugs. For example, when you call the Close () method on a readable inputstream, it is possible to invalidate the connection pool. Then our usual solution is to disable the connection pooling function directly:

Code:

private void Disableconnectionreuseifnecessary () {

This is a bug before the 2.2 version

if (Integer.parseint (Build.VERSION.SDK) < Build.version_codes. FROYO) {

System.setproperty ("Http.keepalive", "false");

}
}

3, which one is the best?

 
Different versions, the best choice must be different, before the Android 2.2 version, HttpClient has fewer bugs, so using it is the best choice.

   And inAndroid 2.3Version and later,HttpURLConnectionis the best choice. It'sApiSimple, small size, so it is very suitable forAndroidProject. Compression and caching mechanisms can effectively reduce network access to traffic, in terms of speed and power saving also played a big role. For new applications, you should be more inclined to useHttpURLConnectionhttpurlconnection  so everyone in the development of android when applied, depends on what you are developing androidandroid 2.2 version and below, then choose httpclient If the target customer is android 2.3 version and above, select androidandroid  2.3 version and above, so for now, httpurlconnection 

Turn-android networking-httpurlconnection and httpclient Choose which is good?

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.