OkHttp3源码详解(三) 拦截器,算法面试经典100题

本文深入探讨了OkHttp3的execute方法,解释了如何构建和执行拦截器链来处理网络请求。在拦截器链中,包括了用户自定义拦截器和框架内置拦截器,如重试、桥接、缓存、连接和网络拦截器。通过对RealInterceptorChain的分析,展示了如何逐个调用拦截器并处理服务器响应。
摘要由CSDN通过智能技术生成
  1. Response response = getResponseWithInterceptorChain();

  2. if (retryAndFollowUpInterceptor.isCanceled()) {

  3. signalledCallback = true;

  4. responseCallback.onFailure(RealCall.this, new IOException(“Canceled”));

  5. } else {

  6. signalledCallback = true;

  7. responseCallback.onResponse(RealCall.this, response);

  8. }

  9. } catch (IOException e) {

  10. if (signalledCallback) {

  11. // Do not signal the callback twice!

  12. Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);

  13. } else {

  14. responseCallback.onFailure(RealCall.this, e);

  15. }

  16. } finally {

  17. client.dispatcher().finished(this);

  18. }

  19. }

  20. }

execute方法的逻辑并不复杂,简单的说就是:

调用getResponseWithInterceptorChain获取服务器返回

通知任务分发器(client.dispatcher)该任务已结束

getResponseWithInterceptorChain构建了一个拦截器链,通过依次执行该拦截器链中的每一个拦截器最终得到服务器返回。

3. 构建拦截器链

首先来看下getResponseWithInterceptorChain的实现:

源码路径:okhttp3/RealCall.java

// 开始执行整个请求

Response getResponseWithInterceptorChain() thro

  • 21
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值