andnroid

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

2018-07-19  本文已影响43人  有兴不虚昧

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的拦截器链不仅仅是请求的时候环环相扣,拿到响应返回的时候也是,所以我们才可以自定义拦截器,在请求和响应的时候添加一些我们自己定义的操作。

上一篇 下一篇

猜你喜欢

热点阅读