OKHttp源码解析(5)----拦截器ConnectInterceptor

系列文章

OKio源码解析

OKHttp源码解析(1)----整体流程

OKHttp源码解析(2)----拦截器RetryAndFollowUpInterceptor

OKHttp源码解析(3)----拦截器BridgeInterceptor

OKHttp源码解析(4)----拦截器CacheInterceptor

OKHttp源码解析(5)----拦截器ConnectInterceptor

OKHttp源码解析(6)----拦截器CallServerInterceptor

1.简介

Opens a connection to the target server and proceeds to the next interceptor.

连接拦截器,负责和服务器建立连接

2.源码解析

  @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");
     //在连接池中找个可用的链接,并创建HttpCodec
    HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();

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

复制代码

连接拦截器的代码比较简单,主要是将网络设置组合起来,给下一个拦截器----CallServerInterceptor,用于向服务器发起真正的网络请求。这些网络设施主要有request(请求)、streamAllocation(网络设施,可以获取HttpCodec和RealConnection)、HttpCodec(htt编解码器)和RealConnection(连接)

实际的网络请求可以在拦截器CallServerInterceptor查看。

小结

未完待续。

转载于:https://juejin.im/post/5c07915151882536c42b6bac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值