OKHTTP源码分析-ConnectInterceptor

看了上面的CacheInterceptor估计许多小伙伴们都开始感觉要吐了,没准心里还可能奔腾着一万只羊驼,这次我们要讲的内容比较简单是这几个拦截器里面比较好理解的。
先上代码

 @Override 
 public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    StreamAllocation streamAllocation = realChain.streamAllocation();

    // We need the network to satisfy this request. Possibly for validating a conditional GET.
    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    // newStream -> HttpCodec
    HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    // 拿到一个连接
    RealConnection connection = streamAllocation.connection();
    // 把这个连接传给下一级
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
  }

是不是很简单,没骗大家把,我们来看

StreamAllocation streamAllocation = realChain.streamAllocation();

总金额里的处理是将我们的RetryAndFollowUpInterceptor 拦截器创建的StreamAllocation 拿到不明白的童鞋可以参考下下面的文章
OKHttp源码分析拦截器-RetryAndFollowUpInterceptor

// We need the network to satisfy this request. Possibly for validating a conditional GET.
    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    // newStream -> HttpCodec
    HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    // 拿到一个连接
    RealConnection connection = streamAllocation.connection();

streamAllocation.newStream这段的作用是创建一个健康的链接,streamAllocation.connection这段的作用是返回sockit链接。最后我们还是调用了下面的方法将处理交给下一级。

return realChain.proceed(request, streamAllocation, httpCodec, connection)

猜你喜欢

转载自blog.csdn.net/u011048906/article/details/79653524