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?