About Android HttpClient HttpURLConnectionencounter a problem
When writing network access code with httpclient on Android studio, it was discovered that the class could not be imported and used .... Baidu after a while
Found a strong netizen has been solved. Related blog: Http://stackoverflow.com/questions/32153318/httpclient-wont-import-in-android-studio
The reason: in Android 2.3 and later, the use of httpurlconnection,httpclient is outdated, and in Android 2.2 and below, using the httpclient.
the difference between the two
Related blog: http://blog.csdn.net/guolin_blog/article/details/12452307
Here are some summaries of the article:
Less httpclient:bug, more API, and because of too many APIs, not conducive to upgrade maintenance and expansion
HttpURLConnection: Simple, easy to use and extensible.
HttpURLConnection Points of attention:
- The gzip compression feature is turned on automatically. Which involves a knowledge point: Multi-threaded Breakpoint download file
If the function of the response compression is activated, the content-length in the HTTP response header will represent the compressed length, then using the Getcontentlength () method to remove the extracted data is wrong.
about how to turn off the gzip compression feature:urlconnection.setrequestproperty ("Accept-encoding", "identity");
- https-
We've also added some improvements to HTTPS in the Android 2.3 version, and now Httpsurlconnection will connect using SNI (Server Name indication). Enables multiple HTTPS hosts to share the same IP address. In addition, a number of compression and session mechanisms have been added. If the connection fails, it will automatically attempt to reconnect. This allows httpsurlconnection to connect to the latest servers more efficiently without compromising the compatibility of older versions.
- Network data cache-android4.0 is added after
about choosing which is better
Choose the new one, of course: The API is easy to use. Google will also continue to maintain it. The future direction is certainly the performance is getting better, the function is more and more comprehensive
Currently you can also choose some open source projects that are widely accepted on GitHub.
- Volley
- Okhttp
- Android-async-http
About Android HttpClient HttpURLConnection