- Applicaion Interceptor和NetworkInterceptor区别
Application interceptors:
- Don’t need to worry about intermediate responses like redirects and retries.(不必担心重定向和重试等中间响应,因为它处于第一个拦截器,会获取到最终的响应 response 。)
- Are always invoked once, even if the HTTP response is served from the cache.(即使从缓存提供HTTP响应,也始终调用一次)
- Observe the application’s original intent. Unconcerned with OkHttp-injected headers like If-None-Match.(观察应用程序的原始意图。 不去关注OkHttp注入的头信息,如If-None-Match。)
- Permitted to short-circuit and not call Chain.proceed().(允许短路而不调用Chain.proceed(),因为是第一个被执行的拦截器,因此它有权决定了是否要调用其他拦截,也就是 Chain.proceed() 方法是否要被执行。)
- Permitted to retry and make multiple calls to Chain.proceed().(允许重试并多次调用Chain.proceed(),因为是第一个被执行的拦截器,因此它有可以多次调用 Chain.proceed() 方法,其实也就是相当与重新请求的作用了。)
Network Interceptors
- Able to operate on intermediate responses like redirects and retries.(能够对重定向和重试等中间响应进行操作,因为 NetworkInterceptor 是排在第 6 个拦截器中,因此可以操作经过 RetryAndFollowup 进行失败重试或者重定向之后得到的resposne)
- Not invoked for cached responses that short-circuit the network.(未调用使网络短路的缓存响应,对于从缓存获取的 response 则不会去触发 NetworkInterceptor 。因为响应直接从 CacheInterceptor 返回了)
- Observe the data just as it will be transmitted over the network.(观察数据,就像它将通过网络传输一样)
- Access to the Connection that carries the request.(访问带有请求的Connection)
参考地址:
[1]. 官网 https://github.com/square/okhttp/wiki/Interceptors
[2]. 简书 https://www.jianshu.com/p/d04b463806c8