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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值