Android的三种http请求方式的区别
PHP中文网
PHP中文网 2017-04-17 17:45:01
0
2
567

关于Android的三种http请求方式:
(1)okhttp
(2)httpURLConnection
(3)httpClient

谁熟悉的能介绍一下,并解析一下区别呢?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
小葫芦

This question is a bit problematic. These are not three ways. okhttp is just a higher-level encapsulation of network access. httpURLConnection and httpClient are two specific ways to achieve access.
1), HttpClient
HttpClient is a sub-project under Apache Jakarta Common, used to provide an efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest version and recommendations of the HTTP protocol.

Features:

  1. Based on standard, pure Java language. Implemented Http1.0 and Http1.1

  2. Implements all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible object-oriented structure.

  3. Supports HTTPS protocol.

  4. Establish a transparent connection through HTTP proxy.

  5. Utilize the CONNECT method to establish a tunneled https connection through the Http proxy.

  6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos authentication scheme.

  7. Plug-in custom authentication scheme.

  8. Portable and reliable socket factory makes it easier to use third-party solutions.

  9. Connection manager supports multi-threaded applications. Supports setting the maximum number of connections, as well as setting the maximum number of connections for each host, and discovers and closes expired connections.

  10. Automatically handle cookies in Set-Cookie.

  11. Plug-in custom cookie policy.

  12. The output stream of Request can avoid buffering the content in the stream directly to the socket server.

  13. The input stream of Response can effectively read the corresponding content directly from the socket server.

  14. Utilize KeepAlive to maintain persistent connections in http1.0 and http1.1.

  15. Get the response code and headers sent by the server directly.

  16. Ability to set connection timeout.
    How to use

Using HttpClient to send requests and receive responses is very simple and generally requires the following steps.

  1. Create HttpClient object.

  2. Create an instance of the request method and specify the request URL. If you need to send a GET request, create an HttpGet object; if you need to send a POST request, create an HttpPost object.

  3. If you need to send request parameters, you can call the setParams (HetpParams params) method common to HttpGet and HttpPost to add request parameters; for the HttpPost object, you can also call the setEntity (HttpEntity entity) method to set the request parameters.

  4. Call execute(HttpUriRequest request) of the HttpClient object to send a request. This method returns an HttpResponse.

  5. Call HttpResponse's getAllHeaders(), getHeaders(String name) and other methods to obtain the server's response headers; call HttpResponse's getEntity() method to obtain the HttpEntity object, which wraps the server's response content. The program can obtain the server's response content through this object.

  6. Release connection. Regardless of whether the execution method is successful or not, the connection must be released

2), HttpURLConnection
Just find a picture for you

3) okhttp
You can search and parse a lot of this online, but I won’t move it

One thing to say here is: in Android 2.3 and above versions, try to use HttpURLConnection, and in Android 2.2 and below versions, try to use HttpClient.
Because:
HttpClient
DefaultHttpClient and its brother AndroidHttpClient are both specific implementation classes of HttpClient. They both have many APIs, their implementation is relatively stable, and the number of bugs is very small.
But at the same time, due to the excessive number of HttpClient APIs, it is difficult for us to upgrade and expand it without destroying compatibility. Therefore, the Android team is currently not active in improving and optimizing HttpClient.
HttpURLConnection
HttpURLConnection is a multi-purpose, lightweight HTTP client that can be used to perform HTTP operations and is suitable for most applications. Although the API provided by HttpURLConnection is relatively simple, it also makes it easier for us to use and extend it.
However, before Android version 2.2, HttpURLConnection always had some annoying bugs. For example, when the close() method is called on a readable InputStream, it may cause the connection pool to fail. Then our usual solution is to directly disable the connection pool function

左手右手慢动作

The difference is so huge that it would take a lot of time to expand on it, so let’s just talk about it briefly.

HttpClient is an excellent HTTP client toolkit in the Java world, so initially Android also included it in support. I hope everyone can use it conveniently to make HTTP requests. However, what Android originally included was a beta version of HttpClient, which resulted in the subsequent use of HttpClient in Android being riddled with bugs. Android engineers did not seem to intend to solve this problem, and even kicked HttpClient out of the later framework, so it is basically not recommended to use HttpClient on Android.

Why is Android willing to abandon HttpClient? An important reason is that they prefer to use HttpURLConnection. HttpURLConnection belongs to the category of Java API. Compared with third-party libraries such as HttpClient, HttpURLConnection is easier to control in Google's own hands. However, the encapsulation of HttpURLConnection is relatively simple, and the processing of HTTP requests is relatively low-level, so it is not particularly convenient to use.

OkHttp is similar to HttpClient and is also a third-party library. However, its packaging is relatively high-level, which means it is simpler and easier to use, and it has dedicated support for Android, which is much better than unfinished projects like HttpClient.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template