OkHttp is the most popular web request framework. Since Android4.4, Google has started to replace HttpURLConnection with OkHttp in the source code, and in the SDK after Android6.0 Google has removed support for HttpClient. The popular Retrofit is also repackaged using OkHttp. This shows how powerful OkHttp is.
Okhttp is also one of the most frequently asked questions in an interview, which can be a bit difficult if you’re just an API engineer.
Android interview questions about the web
OkHttp is an excellent web request framework that Has been added to the Android source code by Google. The more popular Retrofit also uses OkHttp by default.
1, support HTTP2, all requests to a machine to share the same socket 2, built-in connection pool, support connection reuse, reduce delay 3, support transparent GZIP compression response body GZIP is a website compression acceleration technology, for open can speed up the opening of our website, the principle is through the server compression, Client browser quickly decompress the principle, can greatly reduce the site traffic
What are the benefits of GZIP? After Gzip is enabled, the data output to the user’s browser is compressed, which reduces the amount of data transmitted over the network and improves the browser startup speed.
4. Avoid repeated request caching through caching
When the client requests data for the first time, the server returns the expiration time of the Cache (indicated by field Expires and cache-Control). If the Cache does not expire, the server does not need to request the Cache. Otherwise, request data from the server.
Comparing caches When comparing caches, each request needs to interact with the server, and the server determines whether the cache can be used.
5, request failure automatically retry host other IP, automatic redirection 6, good API
Have you seen the source code of OkHttp
First, build all requests through a Builder pattern (request.Builder) and send them to the Dispatcher. Second, the Dispatcher dispatches the request to the HttpEngine (the class that does the real work). The HttpEngine first looks at whether the request has a cache. If so, it gets the information from the cache and returns it to the response. If there is no caching, HttpEngine distributes the request to a ConnectionPool. Third, in a ConnectionPool, a request is sent through Connection, Route and Platfrom are selected first, then to Server (Socket), Data is retrieved, and response is returned.
Create OkHttpClient object: OkHttpClient client = new OkHttpClient(); 2, create network Request, the Request Request = new Request. The Builder (). The url (” sethfeng. Making. IO/index. HTML “). The build (); Call = client.newCall(request); // Create a RealCall object that contains a reference to the client object. 4. Sending asynchronous requests:
Call.queue (new Callback() {@override public void onFailure(Request Request, IOException e) {... } @override public void onResponse(Response Response) throws IOException {... }});Copy the code
4, volley bxbxbai. Making. IO / 2014/09/14 /… Blog.csdn.net/guolin_blog…
5, how to control congestion blog.csdn.net/yechaodechu TCP connection…
6, three-way handshake blog.csdn.net/whuslei/art…
7. TCP and UDP
8, the Android client and server and how to use the Token Session wyong.blog.51cto.com/1115465/155…
Retrofit source code
This article is in the open source project: github.com/Android-Alv… Has been included, which contains different directions of self-study programming routes, interview questions set/interviews, and a series of technical articles, etc., resources continue to update…