OkHttp3整体架构图
拦截器
主要代码:RealCall的 getResponseWithInterceptorChain(),将所有拦截器按顺序保存在list中。
拦截器调用流程:(递归形式调用,最终结果一层一层返回)
自定义拦截器 --> RetryAndFollowUpInterceptor --> BridgeInterceptor --> CacheInterceptor --> ConnectIntereptor --> 自定义网络拦截器 --> CallServerInterceptor
内置拦截器:
- RetryAndFollowUpInterceptor
负责网络失败重连。
- BridgeInterceptor
客户端与服务端通信桥梁,为请求头添加参数。
- CacheInterceptor
缓存拦截器,如果服务端Response设置了Cache-Control,那么进行缓存处理。
- ConnectInterceptor
网络连接拦截器,与服务端建立连接,正式进行http请求。
- CallServerInterceptor
负责网络请求和网络响应。
自定义拦截器
- 应用拦截器
最原始的request,最终结果的response。只能在应用请求时,不能操作中间流程,
使用场景:添加公共参数,日志输出。
- 网络拦截器
可以获取到最终网络请求的requst,能得到真正网络响应的response。
注意点: