Android中的网络库详解(三. OkHttp)

1.前言

在上一章节中,简单分析了Volley源码的实现,他并不是一个底层网络库,而是上层网络库的封装,这一章将继续讲解网络库的第三篇OkHttp,他是Square公司Jake Wharton的杰作,是android端最主流的开源框架

2.目录

目录

3.OkHttp

3.1.OkHttp的优势

首先回顾下OkHttp的优势

  • 1.对同一主机发出的所有请求都可以共享相同的套接字连接
  • 2.使用连接池来复用连接以提高效率
  • 3.提供了对GZIP的默认支持来降低传输内容的大小
  • 4.对Http响应的缓存机制,可以避免不必要的网络请求
  • 5.当网络出现问题时,OkHttp会自动重试一个主机的多个IP地址

3.2.OkHttp的简单使用

  • 同步请求(需在子线程运行)
private void synchronizeRequest() {
    //  构建okHttpClient,相当于请求的客户端,Builder设计模式
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(5, TimeUnit.SECONDS)
            .build();
    // 构建一个请求体,同样也是Builder设计模式
    Request request = new Request.Builder()
            .url("http://www.baidu.com")
            .build();
    //  生成一个Call对象,该对象是接口类型
    Call call = okHttpClient.newCall(request);
    try {
        //  拿到Response
        Response response = call.execute();
        System.out.println(response.body().string());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 异步请求(响应回调在子线程)
private void asyncRequest() {
    //  构建okHttpClient,相当于请求的客户端,Builder设计模式
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(5, TimeUnit.SECONDS)
            .build();
    // 构建一个请求体,同样也是Builder设计模式
    Request request = new Request.Builder()
            .url("http://www.baidu.com")
            .build();
    //  生成一个Call对象,该对象是接口类型
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            System.out.println(response.body().string());
        }
    });
}
  • post请求的两种方式
private static void postRequest() throws IOException {
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .build();

    //表单形式
    FormBody formBody = new FormBody.Builder()
            .add("name", "mary")
            .build();
    Request request = new Request.Builder()
            .post(formBody)
            .build();
    okhttp3.Response execute = okHttpClient.newCall(request).execute();

    //json形式
    MediaType mediaType = MediaType.parse("application/json");
    //object表示实体类
    String json = GsonUtils.toJson(new Object());
    RequestBody requestBody = RequestBody.create(mediaType, json);
    Request request1 = new Request.Builder()
            .post(requestBody)
            .build();
    okhttp3.Response execute1 = okHttpClient.newCall(request1).execute();
}

3.3.OkHttp的源码分析

先简单看一下OkHttp的执行流程

  • 从流程图看一看出异步enqueue()请求是由Dispatcher进行分发,在新线程调用,execute()请求在当前线程调用的
  • 后面会通过责任链模式进行请求和响应的处理
执行流程

首先OkHttp通过建造者模式可以配置的参数

//拦截器
final List interceptors = new ArrayList<>();
//网络拦截器
final List networkInterceptors = new ArrayList<>();
//缓存
@Nullable Cache cache;
//内部缓存
@Nullable InternalCache internalCache;
public Builder() {
    //分发器
    dispatcher = new Dispatcher();
    //协议
    protocols = DEFAULT_PROTOCOLS;
    //传输层版本和连接协议(Https需进行配置)
    connectionSpecs = DEFAULT_CONNECTION_SPECS;
    //事件监听工厂
    eventListenerFactory = EventListener.factory(EventListener.NONE);
    //代理选择器
    proxySelector = ProxySelector.getDefault();
    //cookie
    cookieJar = CookieJar.NO_COOKIES;
    //socket工厂
    socketFactory = SocketFactory.getDefault();
    //主机名字验证
    hostnameVerifier = OkHostnameVerifier.INSTANCE;
    //证书链
    certificatePinner = CertificatePinner.DEFAULT;
    //代理身份验证
    proxyAuthenticator = Authenticator.NONE;
    //本地身份验证
    authenticator = Authenticator.NONE;
    //连接池
    connectionPool = new ConnectionPool();
    //域名
    dns = Dns.SYSTEM;
    //安全桃姐层重定向
    followSslRedirects = true;
    //本地重定向
    followRedirects = true;
    //失败重连
    retryOnConnectionFailure = true;
    //连接超时
    connectTimeout = 10_000;
    //读操作超时
    readTimeout = 10_000;
    //写操作超时
    writeTimeout = 10_000;
    //ping测试周期
    pingInterval = 0;
}

接着再看看rquest可配置的参数

Request(Request.Builder builder) {
    //url地址
    this.url = builder.url;
    //请求方式
    this.method = builder.method;
    //请求头
    this.headers = builder.headers.build();
    //请求体(post请求RequestBody)
    this.body = builder.body;
    //请求标识
    this.tag = builder.tag != null ? builder.tag : this;
}

接着看请求的构建okHttpClient.newCall(request)

@Override public Call newCall(Request request) {
    return RealCall.newRealCall(this, request, false /* for web socket */);
}

static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    // 构建一个RealCall
    RealCall call = new RealCall(client, originalRequest, forWebSocket);
    //call事件的绑定
    call.eventListener = client.eventListenerFactory().create(call);
    return call;
}

接着再看请求的同步执行call.execute()

@Override public Response execute() throws IOException {
    //防止并发安全,且同一个请求只能执行一次
    synchronized (this) {
        if (executed) throw new IllegalStateException("Already Executed");
        executed = true;
    }
    captureCallStackTrace();
    //call回调开始事件
    eventListener.callStart(this);
    try {
        //通过dispatcher将这个请求加入到runningSyncCalls队列中
        client.dispatcher().executed(this);
        //调用拦截链获取响应结果
        Response result = getResponseWithInterceptorChain();
        if (result == null) throw new IOException("Canceled");
        return result;
    } catch (IOException e) {
        //call回调失败事件
        eventListener.callFailed(this, e);
        throw e;
    } finally {
        //请求结束 将请求从runningSyncCalls队列移除
        client.dispatcher().finished(this);
    }
}

接着再来看看dispatcher是如何分发处理请求的,从上面分分析可以看到dispatcher是在构建Builder()是初始化的dispatcher = new Dispatcher(),下面来看看Dispatcher这个类

public final class Dispatcher {
   //异步请求的最大请求数 超过则添加到准备队列
  private int maxRequests = 64;
  //异步请求中每个主机的同时执行的最大请求数
  private int maxRequestsPerHost = 5;
  //闲置时的回调(没有正在执行的同步和异步请求)
  private @Nullable Runnable idleCallback;
  //线程池执行器,懒汉式创建
  private @Nullable ExecutorService executorService;
  //准备执行的异步队列请求(会在前面的请求执行完毕后进行判断调用)
  private final Deque readyAsyncCalls = new ArrayDeque<>();
  //正在执行的异步请求队列
  private final Deque runningAsyncCalls = new ArrayDeque<>();
  //正在执行的同步请求队列(仅作为取消和统计请求数时使用)
  private final Deque runningSyncCalls = new ArrayDeque<>();
}

接着查看client.dispatcher().executed(this)方法

//同步的将其添加到正在运行的同步请求队列
synchronized void executed(RealCall call) {
    runningSyncCalls.add(call);
}

接着对比查看异步请求call.enqueue(new Callback())

@Override public void enqueue(Callback responseCallback) {
    //同同步,同一请求只执行一次
    //异步请求,一般不会出现并发安全
    synchronized (this) {
        if (executed) throw new IllegalStateException("Already Executed");
        executed = true;
    }
    captureCallStackTrace();
    //call开始事件
    eventListener.callStart(this);
    //通过dispatcher将异步请求添加到异步队列中
    client.dispatcher().enqueue(new RealCall.AsyncCall(responseCallback));
}

synchronized void enqueue(RealCall.AsyncCall call) {
    //判断正在运行的请求数是否小于最大请求数及同一主机的请求数是否小于同一主机的最大请求数
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
        //小于则添加到异步运行队列
        runningAsyncCalls.add(call);
        //并通过线程执行器执行请求
        executorService().execute(call);
    } else {
        //大于则添加到等待队列
        readyAsyncCalls.add(call);
    }
}

然后再来查看RealCall.AsyncCall,实际上是一个Runnable,但是run()方法有调用了execute()方法

@Override protected void execute() {
    boolean signalledCallback = false;
    try {
        //通过拦截连获取响应数据
        Response response = getResponseWithInterceptorChain();
        //请求是否被取消
        if (retryAndFollowUpInterceptor.isCanceled()) {
            signalledCallback = true;
             //请求失败被取消
            responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
        } else {
            signalledCallback = true;
            //响应成功的回调
            responseCallback.onResponse(RealCall.this, response);
        }
    } catch (IOException e) {
        //响应是否已经返回
        if (signalledCallback) {
            // Do not signal the callback twice!
            Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
           //响应失败的回调
            eventListener.callFailed(RealCall.this, e);
            responseCallback.onFailure(RealCall.this, e);
        }
    } finally {
        //请求结束,移除异步执行队列
        client.dispatcher().finished(this);
    }
}

然后就是两种请求方式都调用的Response response = getResponseWithInterceptorChain()方法,这是OkHttp的核心方法,弄懂这个流程基本上也就了解了OkHttp的原理

Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List interceptors = new ArrayList<>();
    //okHttpClient配置的拦截器
    interceptors.addAll(client.interceptors());
    //失败和重定向拦截器   
    interceptors.add(retryAndFollowUpInterceptor);
    //从应用程序代码到网络代码的桥梁.根据用户请求构建网络请求,根据网络响应构建用户响应
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    //处理 缓存配置 根据条件返回缓存响应
    //设置请求头(If-None-Match、If-Modified-Since等) 服务器可能返回304(未修改)
    //可配置用户自己设置的缓存拦截器
    interceptors.add(new CacheInterceptor(client.internalCache()));
    //连接服务器 负责和服务器建立连接 这里才是真正的请求网络
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
         //okHttpClient配置的网络拦截器
        //返回观察单个网络请求和响应的不可变拦截器列表
        interceptors.addAll(client.networkInterceptors());
    }
    //执行流操作(写出请求体,获得响应数据) 负责向服务器发送请求数据,从服务器读取响应数据
    //进行http请求报文的封装与请求报文的解析
    interceptors.add(new CallServerInterceptor(forWebSocket));
    //创建责任链,请求的调用者
    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
            originalRequest, this, eventListener, client.connectTimeoutMillis(),
            client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
}

上述代码的关键在于RealInterceptorChain,他是使责任链运行的关键代码,先看看他们的结构

public final class RealInterceptorChain implements Interceptor.Chain {
    //构造器
    public RealInterceptorChain(List interceptors, StreamAllocation streamAllocation,
      HttpCodec httpCodec, RealConnection connection, int index, Request request, Call call,
      EventListener eventListener, int connectTimeout, int readTimeout, int writeTimeout) {
    //拦截器集合
    this.interceptors = interceptors;
    //实际建立连接的类
    this.connection = connection;
    //流分配类
    this.streamAllocation = streamAllocation;
    //http请求响应编解码
    this.httpCodec = httpCodec;
    //集合中拦截器的索引
    this.index = index;
    //当前请求
    this.request = request;
    //操作请求的接口类
    this.call = call;
    //网络请求流程事件监听类
    this.eventListener = eventListener;
    //连接超时
    this.connectTimeout = connectTimeout;
    //IO读操作超时
    this.readTimeout = readTimeout;
    //IO写操作超时
    this.writeTimeout = writeTimeout;
  }
}

然后会调用chain.proceed(originalRequest)

@Override public Response proceed(Request request) throws IOException {
    return proceed(request, streamAllocation, httpCodec, connection);
}

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
                        RealConnection connection) throws IOException {
    //索引越界则抛出 断言Error
    if (index >= interceptors.size()) throw new AssertionError();
    //calls的执行次数
    calls++;
    // If we already have a stream, confirm that the incoming request will use it.
    if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
        throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
                + " must retain the same host and port");
    }
    // If we already have a stream, confirm that this is the only call to chain.proceed().
    if (this.httpCodec != null && calls > 1) {
        throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
                + " must call proceed() exactly once");
    }
    // 获取下一个拦截链
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
            connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
            writeTimeout);
    //获取拦截器对象
    Interceptor interceptor = interceptors.get(index);
    //拦截器执行拦截链的方法
    Response response = interceptor.intercept(next);
    // Confirm that the next interceptor made its required call to chain.proceed().
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
        throw new IllegalStateException("network interceptor " + interceptor
                + " must call proceed() exactly once");
    }
    // Confirm that the intercepted response isn't null.
    if (response == null) {
        throw new NullPointerException("interceptor " + interceptor + " returned null");
    }
    if (response.body() == null) {
        throw new IllegalStateException(
                "interceptor " + interceptor + " returned a response with no body");
    }
    return response;
}

可能现在还看不太清楚到底是怎么一步步的调用每一个拦截器的,这里主要涉及一个接口Interceptor及其内部接口Interceptor.Chain

public interface Interceptor {
    Response intercept(okhttp3.Interceptor.Chain chain) throws IOException;
    interface Chain {
        Request request();
        Response proceed(Request request) throws IOException;
    }
}
  • 1.首先是RealInterceptorChain.proceed(originalRequest),开发分发请求
  • 2.RealInterceptorChain next = new RealInterceptorChain(index + 1...),将索引加1,获取下一个拦截链(见3)
  • 2.Interceptor interceptor = interceptors.get(index)获取index的拦截器
  • 3.Response response = interceptor.intercept(next),具体拦截器执行,并传入写一个RealInterceptorChain,里面有请求的各种参数
  • 4.然后查看具体的拦截器的intercept()方法,如:RealInterceptorChain拦截器,intercept()方法中realChain.proceed(request, streamAllocation, null, null)又会递归回调RealInterceptorChain.proceed(originalRequest)回到第1步,至此整个递归的闭环流程就走完了
拦截器执行链
3.3.1. RetryAndFollowUpInterceptor拦截器
  • 作用:用于失败时恢复以及在必要时进行重定向
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();
    //创建流分配相关类
    StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
            createAddress(request.url()), call, eventListener, callStackTrace);
    this.streamAllocation = streamAllocation;
    //重试次数
    int followUpCount = 0;
    //先前的Response
    Response priorResponse = null;
    while (true) {
        //取消 释放流分配类
        if (canceled) {
            streamAllocation.release();
            throw new IOException("Canceled");
        }

        Response response;
        boolean releaseConnection = true;
        try {
            //递归调用拦截器链
            response = realChain.proceed(request, streamAllocation, null, null);
            releaseConnection = false;
        } catch (RouteException e) {
            // The attempt to connect via a route failed. The request will not have been sent.
            //是否是一个可恢复的请求
            if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
                throw e.getLastConnectException();
            }
            releaseConnection = false;
            //重试 不继续向下执行
            continue;
        } catch (IOException e) {
            // An attempt to communicate with a server failed. The request may have been sent.
            //请求是否已经发送
            boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
            //是否是一个可恢复的请求
            if (!recover(e, streamAllocation, requestSendStarted, request)) throw e;
            releaseConnection = false;
            //重试 不继续向下执行
            continue;
        } finally {
            // We're throwing an unchecked exception. Release any resources.
            if (releaseConnection) {
                //没有捕获到异常 释放流分配类
                streamAllocation.streamFailed(null);
                streamAllocation.release();
            }
        }
        //priorResponse用于保存前一个Response
        // Attach the prior response if it exists. Such responses never have a body.
        if (priorResponse != null) {
            response = response.newBuilder()
                    .priorResponse(priorResponse.newBuilder()
                            .body(null)
                            .build())
                    .build();
        }
        //判断是否需要重定向 需要则返回一个重定向的Request 没有则返回null
        Request followUp = followUpRequest(response, streamAllocation.route());
        if (followUp == null) {
            //不需要重定向
            if (!forWebSocket) {
                //不是webSocket 释放streamAllocation
                streamAllocation.release();
            }
            //直接返回response
            return response;
        }
        //需要重定向 关闭响应流
        closeQuietly(response.body());
        //重定向次数加1 若大于最大重定向次数 释放streamAllocation 抛出ProtocolException
        if (++followUpCount > MAX_FOLLOW_UPS) {
            streamAllocation.release();
            throw new ProtocolException("Too many follow-up requests: " + followUpCount);
        }
        //如果是不能被再次操作的请求体
        if (followUp.body() instanceof UnrepeatableRequestBody) {
            streamAllocation.release();
            throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
        }
        //判断是否为同一连接 请求
        if (!sameConnection(response, followUp.url())) {
            streamAllocation.release();
            //否则重新创建StreamAllocation
            streamAllocation = new StreamAllocation(client.connectionPool(),
                    createAddress(followUp.url()), call, eventListener, callStackTrace);
            this.streamAllocation = streamAllocation;
        } else if (streamAllocation.codec() != null) {
            throw new IllegalStateException("Closing the body of " + response
                    + " didn't close its backing stream. Bad interceptor?");
        }
        //重新赋值request priorResponse 再次执行
        request = followUp;
        priorResponse = response;
    }
}

然后查看是否可重试的方法recover()

private boolean recover(IOException e, boolean requestSendStarted, Request userRequest) {
    streamAllocation.streamFailed(e);

    // The application layer has forbidden retries.
    //如果OkHttpClient直接配置拒绝失败重连,return false
    if (!client.retryOnConnectionFailure()) return false;

    // We can't send the request body again.
    //如果请求已经发送,并且这个请求体是一个UnrepeatableRequestBody类型,则不能重试。
    //StreamedRequestBody实现了UnrepeatableRequestBody接口,是个流类型,不会被缓存,所以只能执行一次,具体可看。
    if (requestSendStarted && userRequest.body() instanceof UnrepeatableRequestBody) return false;

    // This exception is fatal.
    //一些严重的问题,就不要重试了
    if (!isRecoverable(e, requestSendStarted)) return false;

    // No more routes to attempt.
    //没有更多的路由就不要重试了
    if (!streamAllocation.hasMoreRoutes()) return false;

    // For failure recovery, use the same route selector with a new connection.
    return true;
  }

从上面的代码中可以看出,RetryAndFollowUpInterceptor内部开启了while(true)循环

  • 1.内部请求抛出异常时,判断是否需要重试
  • 2.当响应结果是重定向时,构建新的请求

是否可以重试的规则recover():

  • 1.client的retryOnConnectionFailure参数设置为false,不进行重试
  • 2.请求的body已经发出,不进行重试
  • 3.特殊的异常类型,不进行重试
  • 4.没有更多的route(包含proxy和inetaddress),不进行重试
3.3.2. BridgeInterceptor拦截器
  • 作用:根据用户请求构建网络请求,根据网络响应构建用户响应
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();
    //处理请求header相关信息
    RequestBody body = userRequest.body();
    if (body != null) {
        MediaType contentType = body.contentType();
        if (contentType != null) {
            requestBuilder.header("Content-Type", contentType.toString());
        }
        long contentLength = body.contentLength();
        if (contentLength != -1) {
            requestBuilder.header("Content-Length", Long.toString(contentLength));
            requestBuilder.removeHeader("Transfer-Encoding");
        } else {
            requestBuilder.header("Transfer-Encoding", "chunked");
            requestBuilder.removeHeader("Content-Length");
        }
    }
    if (userRequest.header("Host") == null) {
        requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }
    if (userRequest.header("Connection") == null) {
        requestBuilder.header("Connection", "Keep-Alive");
    }
    // If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
    // the transfer stream.
    //没有添加编码方式,默认添加gzip的编解码
    boolean transparentGzip = false;
    if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
        transparentGzip = true;
        requestBuilder.header("Accept-Encoding", "gzip");
    }
    //如果添加了cookieJar,这里会设置到Cookie上
    List cookies = cookieJar.loadForRequest(userRequest.url());
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", cookieHeader(cookies));
    }

    if (userRequest.header("User-Agent") == null) {
        requestBuilder.header("User-Agent", Version.userAgent());
    }
    //上面是请求头的处理
    //递归调用连接链
    Response networkResponse = chain.proceed(requestBuilder.build());
    //根据响应结果 是否保存cookie
    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());
    //重新设置响应头
    Response.Builder responseBuilder = networkResponse.newBuilder()
            .request(userRequest);

    if (transparentGzip
            && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
            && HttpHeaders.hasBody(networkResponse)) {
        //当服务器返回的数据是 GZIP 压缩的,那么客户端就有责任去进行解压操作 使用的是okio
        GzipSource responseBody = new GzipSource(networkResponse.body().source());
        Headers strippedHeaders = networkResponse.headers().newBuilder()
                .removeAll("Content-Encoding")
                .removeAll("Content-Length")
                .build();
        responseBuilder.headers(strippedHeaders);
        String contentType = networkResponse.header("Content-Type");
        responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
    }
    return responseBuilder.build();
}

BridgeInterceptor负责在request阶段对请求头添加一些字段,cookie处理,在response阶段对响应进行一些gzip解压(okio)操作。

3.3.3. CacheInterceptor拦截器
  • 作用:根据用户的配置,处理缓存相关内容
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
    //根据配置读取候选缓存
    Response cacheCandidate = cache != null
            ? cache.get(chain.request())
            : null;
    long now = System.currentTimeMillis();
    //解析请求和缓存策略
    CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
    //如果仅度缓存,那么networkRequest会为null
    Request networkRequest = strategy.networkRequest;
    Response cacheResponse = strategy.cacheResponse;
    if (cache != null) {
        cache.trackResponse(strategy);
    }
    if (cacheCandidate != null && cacheResponse == null) {
        // The cache candidate wasn't applicable. Close it.
        //候选缓存 无用可关闭
        closeQuietly(cacheCandidate.body());
    }
    //不使用网络请求且缓存为空 返回504 空的响应
    // If we're forbidden from using the network and the cache is insufficient, fail.
    if (networkRequest == null && cacheResponse == null) {
        return new Response.Builder()
                .request(chain.request())
                .protocol(Protocol.HTTP_1_1)
                .code(504)
                .message("Unsatisfiable Request (only-if-cached)")
                .body(Util.EMPTY_RESPONSE)
                .sentRequestAtMillis(-1L)
                .receivedResponseAtMillis(System.currentTimeMillis())
                .build();
    }
    //不使用网络请求 返回缓存响应
    // If we don't need the network, we're done.
    if (networkRequest == null) {
        return cacheResponse.newBuilder()
                .cacheResponse(stripBody(cacheResponse))
                .build();
    }
    Response networkResponse = null;
    try {
        //前面都没有return 继续执行拦截器链
        networkResponse = chain.proceed(networkRequest);
    } finally {
        //防止内存泄露 关闭候选缓存
        // If we're crashing on I/O or otherwise, don't leak the cache body.
        if (networkResponse == null && cacheCandidate != null) {
            closeQuietly(cacheCandidate.body());
        }
    }
    //根据网络结果 如果是304 把网络响应和缓存的响应合并 然后返回响应数据
    // If we have a cache response too, then we're doing a conditional get.
    if (cacheResponse != null) {
        if (networkResponse.code() == HTTP_NOT_MODIFIED) {
            Response response = cacheResponse.newBuilder()
                    .headers(combine(cacheResponse.headers(), networkResponse.headers()))
                    .sentRequestAtMillis(networkResponse.sentRequestAtMillis())
                    .receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
                    .cacheResponse(stripBody(cacheResponse))
                    .networkResponse(stripBody(networkResponse))
                    .build();
            networkResponse.body().close();

            // Update the cache after combining headers but before stripping the
            // Content-Encoding header (as performed by initContentStream()).
            cache.trackConditionalCacheHit();
            //更新缓存
            cache.update(cacheResponse, response);
            return response;
        } else {
            //内容有修改 关闭缓存
            closeQuietly(cacheResponse.body());
        }
    }
    //无匹配的缓存
    Response response = networkResponse.newBuilder()
            .cacheResponse(stripBody(cacheResponse))
            .networkResponse(stripBody(networkResponse))
            .build();
    if (cache != null) {
        if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
            // Offer this request to the cache.
            // 将响应数据写入缓存
            CacheRequest cacheRequest = cache.put(response);
            return cacheWritingResponse(cacheRequest, response);
        }
        //是否是无效的缓存
        if (HttpMethod.invalidatesCache(networkRequest.method())) {
            try {
                //删除已有缓存
                cache.remove(networkRequest);
            } catch (IOException ignored) {
                // The cache cannot be written.
            }
        }
    }
    return response;
}
  • CacheInterceptor首先根据request尝试从cache读取缓存,由于OkHttp默认不支持缓存,需通过OkHttpClient配置
  • 根据缓存策略是否使用网络,是否存在缓存,来构建Response返回
  • 如果缓存策略中网络请求为空,继续执行拦截链,然后根据networkResponse````来对response```进行缓存和返回
  • 当数据指定只从缓存获取时,后面的拦截链将不会执行
3.3.4. ConnectInterceptor拦截器
  • 作用:负责DNS解析和Scoket连接
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    //获取RetryAndFollowUpInterceptor创建的StreamAllocation
    //包含connectionPool Address Call等信息
    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 = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    //获取创建的connection
    RealConnection connection = streamAllocation.connection();
    //将建立的连接,传递到下一个拦截器链
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
}

ConnectInterceptor类本身来看代码比较简单,但其逻辑非常复杂,涉及网络连接建立的整个过程,是最重要的拦截器之一

首先查看streamAllocation.newStream()创建连接的方法

public HttpCodec newStream(OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {
    int connectTimeout = chain.connectTimeoutMillis();
    int readTimeout = chain.readTimeoutMillis();
    int writeTimeout = chain.writeTimeoutMillis();
    int pingIntervalMillis = client.pingIntervalMillis();
    //重试
    boolean connectionRetryEnabled = client.retryOnConnectionFailure();
    try {
        //获取健康的连接
        RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
                writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks);
        //对请求以及结果 编解码的类(分http 1.1 和http 2.0)
        HttpCodec resultCodec = resultConnection.newCodec(client, chain, this);
        synchronized (connectionPool) {
            codec = resultCodec;
            return resultCodec;
        }
    } catch (IOException e) {
        throw new RouteException(e);
    }
}

接着查看获取健康可用连接的方法findHealthyConnection ()

private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout,
                                             int pingIntervalMillis, boolean connectionRetryEnabled,
                                             boolean doExtensiveHealthChecks) throws IOException {
    //循环查找 直至找到
    while (true) {
        //查找连接
        RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
                pingIntervalMillis, connectionRetryEnabled);
        // If this is a brand new connection, we can skip the extensive health checks.
        //连接池同步查找 判断是否是一个新的连接 是就直接返回
        synchronized (connectionPool) {
            if (candidate.successCount == 0) {
                return candidate;
            }
        }
        // Do a (potentially slow) check to confirm that the pooled connection is still good. If it
        // isn't, take it out of the pool and start again.
        //如果不是 判断是否是一个健康的连接 是则在后面返回
        if (!candidate.isHealthy(doExtensiveHealthChecks)) {
            noNewStreams();
            continue;
        }
        return candidate;
    }
}

然后继续查看查找连接的方法findConnection()

private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
                                      int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {
    //是否在连接池中找到Connection
    boolean foundPooledConnection = false;
    //对应找到的可用连接
    RealConnection result = null;
    //对应找到的路由
    Route selectedRoute = null;
    //可释放的连接
    Connection releasedConnection;
    //需要关闭的连接
    Socket toClose;
    //同步连接池 获取里面的连接
    synchronized (connectionPool) {
        //是否释放
        if (released) throw new IllegalStateException("released");
        //是否没有编码
        if (codec != null) throw new IllegalStateException("codec != null");
        //是否取消
        if (canceled) throw new IOException("Canceled");

        // Attempt to use an already-allocated connection. We need to be careful here because our
        // already-allocated connection may have been restricted from creating new streams.
        //首先尝试将当前连接设置为可释放的连接
        releasedConnection = this.connection;
        //如果限制了新的流的创建 需释放当前连接 并返回需关闭的socket
        toClose = releaseIfNoNewStreams();
        if (this.connection != null) {
            // We had an already-allocated connection and it's good.
            //如果当前connection不为空 可直接使用
            result = this.connection;
            releasedConnection = null;
        }
        if (!reportedAcquired) {
            // If the connection was never reported acquired, don't report it as released!
            releasedConnection = null;
        }

        if (result == null) {
            // Attempt to get a connection from the pool.
            //当前连接不可用 通过for循环从连接池获取合格的连接 成功获取会更新connection的值
            Internal.instance.get(connectionPool, address, this, null);
            if (connection != null) {
                foundPooledConnection = true;
                result = connection;
            } else {
                selectedRoute = route;
            }
        }
    }
    //关闭socket
    closeQuietly(toClose);
    //事件的绑定
    if (releasedConnection != null) {
        eventListener.connectionReleased(call, releasedConnection);
    }
    if (foundPooledConnection) {
        eventListener.connectionAcquired(call, result);
    }
    if (result != null) {
        // If we found an already-allocated or pooled connection, we're done.
        //如果找到已分配或连接池可复用的连接 则直接返回该对象
        return result;
    }
    // If we need a route selection, make one. This is a blocking operation.
    boolean newRouteSelection = false;
    if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
        newRouteSelection = true;
        //切换路由
        routeSelection = routeSelector.next();
    }
    synchronized (connectionPool) {
        //是否取消
        if (canceled) throw new IOException("Canceled");
        if (newRouteSelection) {
            // Now that we have a set of IP addresses, make another attempt at getting a connection from
            // the pool. This could match due to connection coalescing.
            List routes = routeSelection.getAll();
            for (int i = 0, size = routes.size(); i < size; i++) {
                Route route = routes.get(i);
                //会更新connection值 找到则跳出for循环
                Internal.instance.get(connectionPool, address, this, route);
                if (connection != null) {
                    foundPooledConnection = true;
                    result = connection;
                    this.route = route;
                    break;
                }
            }
        }
        //如果任然没有找到可用连接
        if (!foundPooledConnection) {
            //如果当前路由为空
            if (selectedRoute == null) {
                //切换路由
                selectedRoute = routeSelection.next();
            }
            // Create a connection and assign it to this allocation immediately. This makes it possible
            // for an asynchronous cancel() to interrupt the handshake we're about to do.
            route = selectedRoute;
            refusedStreamCount = 0;
            //创建新连接
            result = new RealConnection(connectionPool, selectedRoute);
            //会更新connection值
            acquire(result, false);
        }
    }

    // If we found a pooled connection on the 2nd time around, we're done.
    // 如果找到(通过一组ip地址查找)
    if (foundPooledConnection) {
        eventListener.connectionAcquired(call, result);
        //返回可复用的连接
        return result;
    }

    // Do TCP + TLS handshakes. This is a blocking operation.
    //TCP的三次握手 和 TLS握手
    result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis,
            connectionRetryEnabled, call, eventListener);
    routeDatabase().connected(result.route());

    Socket socket = null;
    synchronized (connectionPool) {
        reportedAcquired = true;
        //同步将connect加入连接池
        // Pool the connection.
        Internal.instance.put(connectionPool, result);

        // If another multiplexed connection to the same address was created concurrently, then
        // release this connection and acquire that one.
        //多路复用的判断 http2才有
        if (result.isMultiplexed()) {
            socket = Internal.instance.deduplicate(connectionPool, address, this);
            result = connection;
        }
    }
    //关闭socket
    closeQuietly(socket);
    //连接的事件回调
    eventListener.connectionAcquired(call, result);
    return result;
}

ConnectInterceptor的连接过程还是比较复杂的,上面大概过程都已讲述,主要的过程就是获取当前连接(connection)如果不可用,则从连接池中获取可复用连接,如果仍然获取不到,则新建连接,通过连接生成编解码对象(HttpCodec),继续交由拦截链处理

3.3.5. CallServerInterceptor拦截器
  • 作用:http请求报文的封装与请求报文的解析
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    HttpCodec httpCodec = realChain.httpStream();
    StreamAllocation streamAllocation = realChain.streamAllocation();
    RealConnection connection = (RealConnection) realChain.connection();
    Request request = realChain.request();
    long sentRequestMillis = System.currentTimeMillis();
    realChain.eventListener().requestHeadersStart(realChain.call());
    //sink(Okio中OutputStream)中写头信息
    httpCodec.writeRequestHeaders(request);
    realChain.eventListener().requestHeadersEnd(realChain.call(), request);
    Response.Builder responseBuilder = null;
    //检查是否是GET和HEAD以外的请求
    if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
        // If there's a "Expect: 100-continue" header on the request, wait for a "HTTP/1.1 100
        // Continue" response before transmitting the request body. If we don't get that, return
        // what we did get (such as a 4xx response) without ever transmitting the request body.
        //如果头部添加了"100-continue",相当于第一次握手,只有拿到服务器结果再继续
        if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
            httpCodec.flushRequest();
            realChain.eventListener().responseHeadersStart(realChain.call());
            //构建responseBuilder 当response100 返回null
            responseBuilder = httpCodec.readResponseHeaders(true);
        }
        // 前面的"100-continue" responseBuilder 为null
        if (responseBuilder == null) {
            // Write the request body if the "Expect: 100-continue" expectation was met.
            //head成功响应
            realChain.eventListener().requestBodyStart(realChain.call());
            long contentLength = request.body().contentLength();
            //请求体的输出流
            CallServerInterceptor.CountingSink requestBodyOut =
                    new CallServerInterceptor.CountingSink(httpCodec.createRequestBody(request, contentLength));
            BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
            //发送请求体
            request.body().writeTo(bufferedRequestBody);
            bufferedRequestBody.close();
            realChain.eventListener()
                    .requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
        } else if (!connection.isMultiplexed()) {
            // If the "Expect: 100-continue" expectation wasn't met, prevent the HTTP/1 connection
            // from being reused. Otherwise we're still obligated to transmit the request body to
            // leave the connection in a consistent state.
            //初次握手失败
            //进制同主机请求流的分配
            streamAllocation.noNewStreams();
        }
    }
    //flush流
    httpCodec.finishRequest();
    //如果是GET请求 挥着需要"100-continue"握手成功的情况下
    if (responseBuilder == null) {
        realChain.eventListener().responseHeadersStart(realChain.call());
        //再次构建responseBuilder
        responseBuilder = httpCodec.readResponseHeaders(false);
    }
    //获取响应
    Response response = responseBuilder
            //原请求
            .request(request)
            //握手情况
            .handshake(streamAllocation.connection().handshake())
            //请求时间
            .sentRequestAtMillis(sentRequestMillis)
            //响应时间
            .receivedResponseAtMillis(System.currentTimeMillis())
            .build();

    int code = response.code();
    if (code == 100) {
        // server sent a 100-continue even though we did not request one.
        // try again to read the actual response
        //即使我们没有请求 服务端也会发送一个100-continue
        //重新读取真正的响应
        responseBuilder = httpCodec.readResponseHeaders(false);
        //构建response
        response = responseBuilder
                .request(request)
                .handshake(streamAllocation.connection().handshake())
                .sentRequestAtMillis(sentRequestMillis)
                .receivedResponseAtMillis(System.currentTimeMillis())
                .build();

        code = response.code();
    }

    realChain.eventListener()
            .responseHeadersEnd(realChain.call(), response);

    if (forWebSocket && code == 101) {
        // Connection is upgrading, but we need to ensure interceptors see a non-null response body.
        //返回一个空的响应体
        response = response.newBuilder()
                .body(Util.EMPTY_RESPONSE)
                .build();
    } else {
        response = response.newBuilder()
                .body(httpCodec.openResponseBody(response))
                .build();
    }
    //请求关闭连接 连接不需要保活
    if ("close".equalsIgnoreCase(response.request().header("Connection"))
            || "close".equalsIgnoreCase(response.header("Connection"))) {
        streamAllocation.noNewStreams();
    }
    //抛出协议异常 204:No Content 205:Reset Content
    if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
        throw new ProtocolException(
                "HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
    }
    return response;
}

CallServerInterceptor拦截器的主要工作过程是获取HttpCodec对象,针对Http1.1之前和Http2不同协议的http处理,发送http请求构建Response.Builder对象,然后构建```Response````并返回

3.3.6. Interceptors和networkInterceptors

Interceptors:应用拦截器,对发出去的Request做最初的处理,拿到Response做最后的处理

  • 不用担心响应和重定向之间的中间响应(发生重定向也只调用一次)
  • 始终调用一次,即使Http响应是从缓存中提供的
  • 关注原始的request,而不关心注入的headers,比如if-None-Match
  • 允许短路,并且不调用chain.proceed()(意思是可通过缓存返回Response实例)
  • 允许请求失败重试,并多次调用chain.proceed()

networkInterceptors:对发出去的Request做最后的处理,拿到Response时做最初的处理

  • 允许像重定向和重试一样的中间响应(重定向多次调用)
  • 网络发生短路时不调用缓存响应(必须通过chain.proceed()获取)
  • 在数据被传递到网络时观察数据(网络请求过程日志)
  • 有权获得装载请求的连接(可通过chain.connection()获取)
拦截器执行流程

4.总结

至此,OkHttp的整个请求过程大致过了一遍,最为核心的内容为拦截器部分,因为所有请求的创建,连接,响应都是在这里面处理的

关于连接池的复用是通过connectionPool处理的,他会将要执行的RealConnection维护到一个队列中,并且会清理掉keepAliveDurationNs超时和longestIdleDurationNs超时的连接,然后执行网络请求时会从队列中获取合适的连接,获取不到则建立新的连接


参考:
https://zhuanlan.zhihu.com/p/116777864

你可能感兴趣的:(Android中的网络库详解(三. OkHttp))