从一个简单请求进入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 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官方文档中介绍拦截器时给出的这张图

从一个简单请求进入Okhttp源码_第1张图片
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源码)