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:
Based on standard, pure Java language. Implemented Http1.0 and Http1.1
Implements all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible object-oriented structure.
Supports HTTPS protocol.
Establish a transparent connection through HTTP proxy.
Utilize the CONNECT method to establish a tunneled https connection through the Http proxy.
Portable and reliable socket factory makes it easier to use third-party solutions.
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.
Automatically handle cookies in Set-Cookie.
Plug-in custom cookie policy.
The output stream of Request can avoid buffering the content in the stream directly to the socket server.
The input stream of Response can effectively read the corresponding content directly from the socket server.
Utilize KeepAlive to maintain persistent connections in http1.0 and http1.1.
Get the response code and headers sent by the server directly.
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.
Create HttpClient object.
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.
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.
Call execute(HttpUriRequest request) of the HttpClient object to send a request. This method returns an HttpResponse.
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.
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.
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:
Based on standard, pure Java language. Implemented Http1.0 and Http1.1
Implements all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible object-oriented structure.
Supports HTTPS protocol.
Establish a transparent connection through HTTP proxy.
Utilize the CONNECT method to establish a tunneled https connection through the Http proxy.
Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos authentication scheme.
Plug-in custom authentication scheme.
Portable and reliable socket factory makes it easier to use third-party solutions.
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.
Automatically handle cookies in Set-Cookie.
Plug-in custom cookie policy.
The output stream of Request can avoid buffering the content in the stream directly to the socket server.
The input stream of Response can effectively read the corresponding content directly from the socket server.
Utilize KeepAlive to maintain persistent connections in http1.0 and http1.1.
Get the response code and headers sent by the server directly.
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.
Create HttpClient object.
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.
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.
Call execute(HttpUriRequest request) of the HttpClient object to send a request. This method returns an HttpResponse.
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.
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.