OKHttp source code analysis
https://www.jianshu.com/p/27c1554b7fee
Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
//在配置 OkHttpClient 时设置的 interceptors,放在第一位
interceptors.addAll(client.interceptors());
// 负责失败重试以及重定向
interceptors.add(retryAndFollowUpInterceptor);
// 桥拦截器,添加一些Header和移除一些header,例如body的contentLength是-1,则移除Content-Length这个header
interceptors.add(new BridgeInterceptor(client.cookieJar()));
// 负责读取缓存直接返回、更新缓存
interceptors.add(new CacheInterceptor(client.internalCache()));
// 负责和服务器建立连接
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
// 配置 OkHttpClient 时设置的 networkInterceptors
interceptors.addAll(client.networkInterceptors());
}
// 负责向服务器发送请求数据、从服务器读取响应数据
interceptors.add(new CallServerInterceptor(forWebSocket));
OkHttp analysis (1) See the principle from the usage
https://www.jianshu.com/p/7b29b89cd7b5
OkHttp analysis (two) network connection
https://www.jianshu.com/p/da071ad21b6d
Interview Essentials-Source Code Series OkHttp3
https://www.jianshu.com/p/7c2bb28ccac4
Detailed explanation of OKHTTP cache configuration
https://blog.csdn.net/briblue/article/details/52920531
okhttp connection pool reuse mechanism
https://blog.csdn.net/tangjiean/article/details/51729371
OkHttp3 connection pool and connection establishment process analysis (part 1)
https://sq.163yun.com/blog/article/188729834576564224
Okhttp3 interceptor-the difference between application interceptor and network interceptor
https://www.pianshen.com/article/7635452443/
In Okhttp3, interceptors are divided into application interceptors and network interceptors. There is a big difference between the two. When using them, we must pay attention to prevent unnecessary troubles caused by wrong use. Next, I will explain the differences between these two interceptors. .
First look at the Okhttp execution flow chart, the interceptor execution process
Look at the interceptor execution sequence diagram
According to the above two pictures, we can list the execution flowchart of Application Interceptor and Network Interceptor
Application interceptor:
- No need to care about whether to redirect or fail to reconnect
- The application interceptor will only be called once, even if the data comes from the cache
- Only consider the initial intent of the application, without considering the header injected by Okhhtp such as: if-None-Match, which means that regardless of other external factors, only the final return result is considered
- According to the second picture, we can see that the custom application interceptor is the first interceptor to start execution, so the meaning of this sentence is that the application interceptor can decide whether to execute other interceptors, through Chain.proceed ().
- Similar to the meaning of the previous sentence, you can execute multiple calls to other interceptors through Chain.proceed().
Network blocker:
- According to the third picture, we can understand the meaning of this sentence is that the network interceptor can operate the return value of redirection and failed reconnection
- According to the first picture, we can see that the meaning of this sentence is that the data in the cache will not go to Chain.proceed(). Therefore, the network interceptor cannot be executed.
- It means that all data transmitted through the network can be observed through the network interceptor
- According to the second picture, we can see that the interceptor that requests the service connection is executed before the network interceptor, so when the network interceptor is executed, you can see the server request connection information in the Request, because the application interceptor does not get it. To the corresponding connection information.