从一个简单请求进入Okhttp源码

一个简单的OkHttp(同步)请求

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new LoggingInterceptor())
    .build();
Request request = new Request.Builder()
    .url("X")
    .header("User-Agent", "OkHttp Example")
    .build();
Response response = client.newCall(request).execute();
response.body().close();

execute方法进入源码查看

 @Override public Response execute() throws IOException {
    ...
    try {
      client.dispatcher().executed(this); //这里是加入同步请求队列中
      Response result = getResponseWithInterceptorChain(); //这里才是真正的请求
      if (result == null) throw new IOException("Canceled");
      return result;
    } finally {
      client.dispatcher().finished(this);
    }
  }

点进getResponseWithInterceptorChain方法看看

 private Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    interceptors.add(retryAndFollowUpInterceptor);
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));
    if (!retryAndFollowUpInterceptor.isForWebSocket()) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(
        retryAndFollowUpInterceptor.isForWebSocket()));

  // 将所有的拦截器添加创建了一条拦截链
    Interceptor.Chain chain = new RealInterceptorChain(
        interceptors, null, null, null, 0, originalRequest);
    //启动拦截链
return chain.proceed(originalRequest);
  }

可以看到OkHttp中一个非常核心的东西interceptor,在这个方法中我们看到ApplicationInterceptors被添加到拦截链的最上层,而中间添加了OkHttp内自定义的几个拦截器,随后NetworkInterceptors也被添加进来,最后CallServerInterceptor被添加进来,这也正如OkHttp官方文档中介绍拦截器时给出的这张图

image

可以看到当拦截链被启动后,最终返回了response,这说明请求一定是在这些拦截器的某一个中进行,我们点进proceed()中查看一下这个方法是如何执行的

 public Response proceed(Request request, StreamAllocation streamAllocation, HttpStream httpStream,
      Connection connection) throws IOException {
    ...//省略一些判断
    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpStream, connection, index + 1, request);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

    // Confirm that the next interceptor made its required call to chain.proceed().
    if (httpStream != null && index + 1 < interceptors.size() && next.calls != 1) {
      throw new IllegalStateException("network interceptor " + interceptor
          + " must call proceed() exactly once");
    }
    ...
    return response;
  }

我们主要关注这一段代码,我们知道在每一个interceptor的intercept方法中,必须调用chain.proceed()才能使拦截器生效,原因就在这里,在不同的拦截器中我们对Request依次进行加工处理,最终到达CallServerInterceptor,在这里OkHttp借助Okio(sink可以看作是outputstream,而source则可以看作是inputstream)进行请求,然后将结果Response再按相反的顺序依次在拦截器中进行处理最终返回到了getResponseWithInterceptorChain()中,由此一个同步的网络请求就完成了。
而对于一个异步的请求,它与同步请求的不同就在于Dispatcher分发请求时,使用了内部封装的一个线程池,其它的基本上和同步请求时类似

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于okhttp是一款优秀的网络请求框架,关于它的源码分析文章有很多,这里分享我在学习过程中读到的感觉比较好的文章...
    蕉下孤客阅读 3,625评论 2 38
  • 这篇文章主要讲 Android 网络请求时所使用到的各个请求库的关系,以及 OkHttp3 的介绍。(如理解有误,...
    小庄bb阅读 1,233评论 0 4
  • 简介 目前在HTTP协议请求库中,OKHttp应当是非常火的,使用也非常的简单。网上有很多文章写了关于OkHttp...
    第八区阅读 1,398评论 1 5
  • 用OkHttp很久了,也看了很多人写的源码分析,在这里结合自己的感悟,记录一下对OkHttp源码理解的几点心得。 ...
    蓝灰_q阅读 4,375评论 4 34
  • 这个梗之前风靡朋友圈,今天就谈一谈为什么会这么火。 从创作者角度看: 创作的成本低,第一句话制造悬念,第二句话转折...
    撒哈拉的自己阅读 1,215评论 0 0