Okhttp源码学习二(拦截器的工作过程)

Okhttp源码学习一(基本请求流程)中,只是学习了okhttp请求网络的一个基本流程,但是最关键的点,同步或异步请求过程中的第二步:执行网络请求,拿到响应结果,这一步还没有具体分析. okhttp的具体请求过程被封装在内置的拦截器中,所以本篇就先学习okhttp拦截器的工作过程

okhttp不管是同步还是异步请求,他们请求网络的核心步骤都是一样的,都会去调用 Response response = getResponseWithInterceptorChain()这一行代码,这一行代码就封装了okhttp请求网络的具体实现,看一下它的源码:

//RealCall.getResponseWithInterceptorChain
Response getResponseWithInterceptorChain() throws IOException {
  // 拦截器集合,包括我们自定义的,okhttp内置的
  List<Interceptor> interceptors = new ArrayList<>();
  //添加我们在构建OkhttpClient对象时,通过okhttpClient.builder添加的自定义拦截器
  interceptors.addAll(client.interceptors());
  //取消或失败重试,重定向
  interceptors.add(retryAndFollowUpInterceptor);
  //桥接,连接用户请求信息和 HTTP 请求的桥梁(把用户构造的请求转换为发送到服务器的请求)
  interceptors.add(new BridgeInterceptor(client.cookieJar()));
  //缓存
  interceptors.add(new CacheInterceptor(client.internalCache()));
  //连接
  interceptors.add(new ConnectInterceptor(client));
  if (!forWebSocket) {
    interceptors.addAll(client.networkInterceptors());
  }
  //网络请求
  interceptors.add(new CallServerInterceptor(forWebSocket));
  //创建一个拦截器链,将前面的拦截器列表作为参数传入
  Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
      originalRequest, this, eventListener, client.connectTimeoutMillis(),
      client.readTimeoutMillis(), client.writeTimeoutMillis());
  //开始请求
  return chain.proceed(originalRequest);
}

getResponseWithInterceptorChain()是定义在RealCall里面的,可以看到如果我们不添加任何自定义拦截器的话,okhttp默认就有5个内置的拦截器:

RetryAndFollowUpInterceptor,BridgeInterceptor,CacheInterceptor,ConnectInterceptor,CallServerInterceptor.

getResponseWithInterceptorChain()要注意的一点是:

 Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
    originalRequest, this, eventListener, client.connectTimeoutMillis(),
    client.readTimeoutMillis(), client.writeTimeoutMillis());

在创建RealInterceptorChain对象的时候,RealInterceptorChain的构造函数的第5个参数index,可以看到传入的是0,这个参数非常重要,后面会用到。getResponseWithInterceptorChain()最后调用了chain.proceed()

public interface Interceptor {
  Response intercept(Chain chain) throws IOException;

  interface Chain {
    Request request();

    Response proceed(Request request) throws IOException;

    @Nullable Connection connection();

    Call call();

    int connectTimeoutMillis();

    Chain withConnectTimeout(int timeout, TimeUnit unit);

    int readTimeoutMillis();

    Chain withReadTimeout(int timeout, TimeUnit unit);

    int writeTimeoutMillis();

    Chain withWriteTimeout(int timeout, TimeUnit unit);
  }
}

ChainInterceptor接口的内部接口,RealInterceptorChain实现了 Interceptor.Chain接口,所以直接看RealInterceptorChainChain()方法:

 public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
  RealConnection connection) throws IOException {
   if (index >= interceptors.size()) throw new AssertionError();

   calls++;

   .....
   //又重新创建了一条链,并且把index+1,index就是前面说到的getResponseWithInterceptorChain()传入的初始值0
   RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
   connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,writeTimeout);
   Interceptor interceptor = interceptors.get(index);
   Response response = interceptor.intercept(next);

   .....

   return response;
}

RealInterceptorChain.chain()这里面的逻辑是:

  1. 创建一条新链(下一个链),然后把创建RealInterceptorChain对象时传入的index+1,在 getResponseWithInterceptorChain()中创建第一个拦截器链对象RealInterceptorChain时index传入的是0;
  2. 根据原始索引值拿到拦截器列表对应位置的置拦截器
  3. 通过层层递归调用拦截器的intercept()方法,拿到resoponse响应,并返回

这里的逻辑有点绕,先看下面这张图:


执行逻辑.png

根据上面的图,屡屡逻辑:

  1. 当调用Response response = getResponseWithInterceptorChain()这行代码的时候,从前面的源码我们知道,RealCall.getResponseWithInterceptorChain() 会去创建第一个拦截器链RealInterceptorChain(也就是链1),index传入的是0
  2. 然后链1会去调用自己的proceed(),在proceed()里面又重新去创建了一条新链RealInterceptorChain(也就是链2),链2的index是链1的index+1(也就是1)
  3. 接着链1从拦截器列表中取出下标为自己的index(下标为0)的拦截器0,调用拦截器0的intercept(),并且把链2作为intercept()的参数传入
  4. 通过查看okhttp内置的5个拦截器的intercept()可以看到,每个拦截器的intercept()内部都会去调用chain.proceed(request),除了最后一个,而这个chain就是调用intercept()传入的参数,也就是当前链的下一个链(在第三步,拦截器0的intercept()传入的参数是链2). 链2也是RealInterceptorChain类型,所以又重新走一遍第二步的逻辑,创键链3,然后取出拦截器1.......以此类推,直到index大于或等于interceptors.size()的时候结束递归
  5. 当执行到最后一个拦截器4的时候,也就是okhttp的内置拦截器CallServerInterceptor,CallServerInterceptor的intercept()并没有调用chain.proceed(request),因为如果它也调用,那么执行 if (index >= interceptors.size()) throw new AssertionError();这一行的时候,会抛异常,然后response会为null,所以在CallServerInterceptor.intercpt()里面就要从服务端读取响应数据
  6. 当拦截器4 CallServerInterceptor返回response,拦截器4的调用者链5的proceed()也返回response。链5的调用者拦截器3也就返回reponse.......以此类推,最后直到链1的拦截器0返回response,链1的proceed()返回response,也就是RealCall.getResponseWithInterceptorChain()返回response

从上面的执行逻辑可以看到,okhttp的拦截器链不仅仅是请求的时候环环相扣,拿到响应返回的时候也是,所以我们才可以自定义拦截器,在请求和响应的时候添加一些我们自己定义的操作。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容