OkHttpClient源码分析(五)—— ConnectInterceptor和CallServerInterceptor

上一篇我们介绍了缓存拦截器CacheInterceptor,本篇将介绍剩下的两个拦截器: ConnectInterceptorCallServerInterceptor

ConnectInterceptor

该拦截器主要是负责建立可用的链接,主要作用是打开了与服务器的链接,正式开启了网络请求。
查看其intercept()方法:

  @Override public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    //从拦截器链中获取StreamAllocation对象
    Request request = realChain.request();
    StreamAllocation streamAllocation = realChain.streamAllocation();
    
    //创建HttpCodec对象
    HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    
    //获取realConnetion
    RealConnection connection = streamAllocation.connection();

    //执行下一个拦截器,返回response
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
  }

可以看到intercept中的处理很简单,主要有以下几步操作:

  1. 从拦截器链中获取StreamAllocation对象,在讲解第一个拦截器

你可能感兴趣的:(高手修炼秘笈,OkHttp最易懂解析)