Comparison of indicators:
1. CPU
2. Flow rate
3. Power
4. Memory consumption
5. Networking time
Function points:
1. Retry mechanism
2. Extended functions provided
3. Ease of Use
4, whether HTTPS
5, whether support reflect api,okhttp have matching method
6. Cache, retry
7. Cookie supports session ID sessions
8, weak network performance and stability
9. Timeout time, several timeout connection timeout, response timeout
10, suitable for a variety of models, 4.4 and previous version 2.3 4.1 5.0
4 ways to compare the functions of the Network connection form:
|
Cache |
Retry |
Https/http |
Stability |
Cookies |
Session |
HttpClient |
√ |
√ |
√ |
Ok |
√ Automatically Manage cookies |
√ |
HttpURLConnection |
Postx After get√4.0 |
√ |
√ |
Ok |
√ Set Request Cookie |
√ |
OkHttp |
√ |
√ |
√ |
Ok |
√ |
√ |
Volley |
√ |
√ |
√ |
Ok |
√ |
√ |
Ps: Stability: Four kinds of network connection methods are widely used in the industry, are relatively stable.
In particular, before Froyo (2.2), HttpURLConnection had a major Bug, calling the close () function would affect the connection pool, causing the connection reuse to fail, so the Froyo You need to close keepalive before using HttpURLConnection.
Additionally, gzip compression is turned on by default on Gingerbread (2.3) httpurlconnection, which improves the performance of HTTPS, and Ice Cream Sandwich (4.0) HttpURLConnection supports the request result cache.
Plus HttpURLConnection itself API is relatively simple, so for Android, after 2.3 recommended to use HttpURLConnection, previously recommended to use HttpClient.
Features comparison table Reference blog:
HttpClient Cookies:
http://zzc1684.iteye.com/blog/2162858
HttpClient Retry mechanism:
http://blog.csdn.net/weborn/article/details/9112309
Volley Source Code Analysis:
http://p.codekk.com/blogs/detail/54cfab086c4761e5001b2542
HttpURLConnection set the request cookie:
http://hw1287789687.iteye.com/blog/2240085
OkHttp Cookie Management:
1190000004345545
Volley cookiesession:
http://my.oschina.net/liusicong/blog/361853
HttpClient session remains:
Http://www.flysnow.org/2013/11/21/android-httpclitent-session-keep.html
1, HttpClient shortcomings list
Apache HttpClient long ago not recommended httpclient,5.0 after simply discarded, follow-up will be deleted. 6.0 deleted the httpclient. Java Development with HttpClient, officially recommended for Android development with HttpURLConnection.
Defaulthttpclient and its brother Androidhttpclient are httpclient specific implementation classes, they all have a lot of APIs, and the implementation is relatively stable, the number of bugs is very small.
But at the same time, because the HttpClient API is too large, it makes it difficult to upgrade and expand it without breaking compatibility, so the Android team is not actively working on upgrading and optimizing httpclient.
Efficient and stable, but the maintenance cost is high, so the Android development team is unwilling to maintain the library, but instead of the more lightweight httpurlconnection.
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:
In the Android 4.0 release, we added some caching mechanisms for the response. When the cache is installed (call Httpresponsecache's Install () method), all HTTP requests will meet the following three scenarios:
Relatively light, flexible and easy to expand
Improvements in 3.0 and 4.0, such as HTTPS support
In 4.0, support for caching has also been added
There are some bugs in HttpURLConnection in Android 2.2 and below, so it is recommended to use httpclient before using httpurlconnection,2.3 after Android 2.3.
3.OkHttpMore advantages
OkHttp is a modern, fast and efficient HTTP client that supports HTTP/2 and SPDY (SPDY introduction URL: Https://zh.wikipedia.org/wiki/SPDY,SPDY (pronunciation like English: Speedy ), a Open of the Network Transport Protocol , by Google development ), it has done a lot of things for you.
Okhttp is an Android version of the HTTP client. Very efficient, supports spdy, connection pooling, gzip, and HTTP caching.
Supports Spdy, can merge multiple requests to the same host
Okhttp implements a number of technologies such as connection pooling, gziping, caching and so on to know how complex network-related operations are.
Okhttp plays the role of the transport layer.
Okhttp uses Okio to greatly simplify the access and storage of data,Okio is a library that enhances java.io and Java.nio.
OkHttp handles a number of network problems: it automatically recovers from many common connectivity issues. If your server is configured with multiple IP addresses, Okhttp will automatically attempt the next IP when the first IP connection fails.
Okhttp also handles proxy server issues and SSL handshake failure issues.
Okhttp is a Java Http+spdy Client development package and also supports Android. Requires Android 2.3 or more
Okhttp is an Android version of the HTTP client. Very efficient, supports spdy, connection pooling, gzip, and HTTP caching.
By default, Okhttp automatically handles common network problems, such as two-connection, SSL handshake issues.
If you have integrated Okhttp,retrofit in your application, the default is to use Okhttp to handle other network layer requests.
The underlying implementation of HttpURLConnection starting with Android4.4 is okhttp
Cache responses to avoid duplicate network requests
Currently, the package repository supports:
? A general GET request
? General POST request
? HTTP-based file upload
? File download
? Upload the download Progress callback
? Loading pictures
? Support Request callback, return object, object collection directly
? Support for session retention
? Support Self-signed web site HTTPS access, provide method settings under the certificate on the line
? Support Canceling a request
Why cache, or what is the benefit?
Reduce server load and reduce latency to improve the user experience.
The complex cache policy will be based on the user's current network situation to take a different cache policy, such as in the case of poor 2g network, increase the time of the cache use, not the application, business requirements, interfaces need to cache policy is different, some to ensure real-time data, so there can be no cache, some you can cache 5 minutes Wait a minute. You have to give different scenarios based on the timeliness of the data you need for your specific situation. Of course you can also all the same caching strategy, look at yourself.
4, Volley
Volley is a library that simplifies network tasks. He is responsible for handling requests, loading, caching, threading, synchronization and other issues. It can handle JSON, images, caches, text sources, support a certain degree of customization.
Volley in Android 2.3 and later, using HttpURLConnection, while in Android 2.2 and below, use HttpClient.
Volley basic usage, online information countless, here is recommended Guo Lin great God's blog
Volley there is a cache thread, a network request thread pool (default of 4 threads).
Volley such direct use of development efficiency will be relatively low, I use the volley when I used the various techniques encapsulated into a library requestvolly.
I encapsulate the way the request is constructed in this library for functional invocation. Maintain a global request queue and expand some convenient APIs.
But how to encapsulate the volley is not always comparable to okhttp in functional extensibility.
Volley the update was stopped, and OkHttp has been officially recognized and is constantly being optimized.
so I finally replaced it to OkHttp
The okhttp uses Okio for data transfer. They're from Square House.
But not directly with Okhttp. Square also has a retrofit library with okhttp fighting power doubling.
Network Load Library: Picasso,Fresco, so I prefer Glide
But now the mainstream network requests are the Volley,okhttp voice is also very high.
The volley is designed for RPC network operations and is suitable for short-term operations.
Volley uses the Apache Http stack as its transport layer by default on Froyo, using HttpURLConnection stack as the transport layer on gingerbread and later versions. The reason is that there are some problems with each of the two HTTP stacks in different versions of Android.
Volley can easily set okhttp as its transport layer.
Volley was developed by Google.
Volley can be said to Asynchttpclient and universal-image-loader the advantages of a set in one, can be as very simple as asynchttpclient http communication, You can also load pictures on your network as easily as Universal-image-loader. In addition to simple and easy to use, volley in the performance of the large-scale adjustment, its design goal is very suitable for the data volume is not small, but the traffic is frequent network operation, and for large data volume of network operations, such as downloading files, volley performance will be very bad
Volley provides: Jsonobjectrequest, Jsonarrayrequest, Stringrequest, Imagerequest, Networkimageview and other request forms.
Volley has a lot of advantages, can be extended, reasonable structure, clear logic, can identify the cache, through a unified way to obtain network data, including and not limited to text, pictures and other resources. It took a while to decisively abandon the other frameworks used before.
Volley in the process of creating a request queue at the outset, you need to create a network thread and a cache thread, and also initialize disk-based caching, which has a lot of resource overhead and IO operations, all of which are slow.
4 Ways to compare:
1, HttpClient: before the Android 2.2 version, HttpClient has fewer bugs, so using it is the best choice.
2, HttpURLConnection: But in the Android 2.3 version and later, HttpURLConnection is the best choice. Its API is simple and small in size, making it ideal for Android projects.
for new applications, you should be more inclined to use httpurlconnection, because in future work we will also put more time in optimizing HttpURLConnection above.
Google itself is also recommended to use HttpURLConnection , it has been extensively optimized, as can be seen from the Android Help documentation:
Http://developer.android.com/reference/java/net/HttpURLConnection.html
3 , OkHttp : is a relatively mature solution, we have more reason to believe OkHttp of power.
Android4.4 can be seen in the source code HttpURLConnection has replaced OkHttp has been implemented.
OkHttp handles a number of network problems: it automatically recovers from many common connectivity issues.
If your server is configured with multiple IP addresses, Okhttp will automatically attempt the next IP when the first IP connection fails.
Okhttp also handles proxy server issues and SSL handshake failure issues.
You do not need to rewrite the network code in your program using OkHttp.
Okhttp implements almost the same API as Java.net.HttpURLConnection.
in fact, now, neither of them is used. Okhttp
HttpURLConnection Now the underlying implementation is through Okhttp
Features supported by the Network request framework:
1. Header to support custom requests
2. Basic request method to support http: GET, POST
3. Support File upload and download
4, can load the picture
5, support multi-task network request to do
6. Support Caching
7. Support Request Callback
8, support the session to maintain
Recommended points for network optimization:
1. Connection multiplexing saves connection settling time, such as opening keep-alive
2, without the domain name, with the IP direct connection eliminates the DNS resolution process, obtains the IP address according to the domain name
Reference blog:
Http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0106/2275.html
http://blog.csdn.net/zhangcanyan/article/details/51661448
First, the performance index comparison
Test Phone: Xiaomi MI3 (4.4.4), the Charm Blue 2 (5.1)
Beta version: Book flag Android 9.9.0
Test environment: Direct Connect under WiFi access
Test method: Install and open the easy-to-test app, install the network framework APK, make a networking request (http://www.csdn.net/), request multiple tests.
Test result calculation: Test 3 times.
Error description: Third-party software calculation, affected by the background application, there is a certain error.
1. CPU consumption
Error description: Third-party software calculation, affected by the background application, there is a certain error.
Conclusion: The overall CPU occupancy rate of the test model, okhttp network request is slightly lower than other networking methods.
2. Memory consumption
Error description: Third-party software calculation, affected by the background application, there is a certain error.
Conclusion: The total memory occupancy of the test model, on the Xiaomi phone, the Okhttp network request consumes less memory than other networking methods. On Meizu phones, memory consumption is slightly higher than in other ways. Show that the test model memory consumption performance varies.
3. Power consumption
Error description: Third-party software calculation, affected by the background application, there is a certain error.
Conclusion: The percentage of power consumed by Okhttp network requests is slightly lower than that of other networking methods, and the total power consumed is higher than other networking methods on the Xiaomi phone, which is slightly lower than other networking methods on the Meizu phone. The power consumption difference is not small, within 0.5mah.
4. Flow consumption
Error description: There is a certain error in the sending and receiving traffic of the network request.
Conclusion:
1, the network sends the traffic httpurlconnection highest, volley second, okhttp third, httpclient least. However, the amount of data varies little and fraction requests differ.
2, cumulative receive traffic httpclient maximum, httpurlconnection and volley the same, okhttp slightly larger than the former two.
5, the average time-consuming network
Error description: The condition of the network may affect the network request time.
Conclusion: The average time of network request is httpclient maximum, the httpurlconnection is the smallest, and the volley average is higher 10ms~20ms,okhttp than volley 20ms~40ms.
The final conclusion is drawn:
1 , for new applications, you should be more inclined to use httpurlconnection, because in future work , Google will also put more time to optimize HttpURLConnection above.
Google itself is also recommended to use HttpURLConnection , it has been extensively optimized, as can be seen from the Android Help documentation:
Http://developer.android.com/reference/java/net/HttpURLConnection.html
2 , for applications that have matured, Volley the update was stopped, and OkHttp has been officially recognized and is constantly being optimized.
so I recommend using OkHttp , now the latest stable version is 3.2.0
ANDROID4 network Connectivity HttpClient, HttpURLConnection, okhttp and volley pros and cons and performance comparisons