Android OkHttp(一)源码出发探寻执行原理

    现在主流的网络请求都是使用 Retrofit + OkHttp ,在掌握了一般使用后,就要往深里探究框架源码,这样不光是为了面试装逼,更是提升了对源码理解能力。经过大约一周的时间准备,源码看了个大概,也就大致明白了原理,这里总结一下整理成文,希望对大家能有所帮助。

推荐文章:

Android 必须知道的网络请求框架库,你不可错过的框架介绍篇

Android Retrofit 2.0(一)初次见面请多多关照

Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava

Android Retrofit 2.0(三)从源码分析原理

流程结构

首先,OkHttpClient 通过Builder构建 Request(url\method\header\body) 转换为 newCall;

然后,在 RealCall 中执行异步或同步任务;

最后,配合一些拦截器 interceptor 发送请求得到返回 response(code\message\headers\body)。

——OkHttp流程图,如下:

 

Get请求过程

案例是,假设我们正在进行一个普通的okhttp进行同步请求,然后我们按照前面的流程图依次进行分析,简单易懂。

    public String get(String url) throws IOException {
        //创建 OKHttpClient对象
        OkHttpClient client = new OkHttpClient();
        //创建 Request对象
        Request request = new Request.Builder()
                .url(url)
                .build();
        //创建 Response接收响应
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        }else{
            throw new IOException("Unexpected code " + response);
        }
    }

1、创建 OKHttpClient 对象

OkHttpClient client = new OkHttpClient();

该对象包括以下内容:

    final Dispatcher dispatcher;  //分发器
    final Proxy proxy;  //代理
    final List protocols; //协议
    final List connectionSpecs; //传输层版本和连接协议
    final List interceptors; //拦截器
    final List networkInterceptors; //网络拦截器
    final ProxySelector proxySelector; //代理选择
    final CookieJar cookieJar; //cookie
    final Cache cache; //缓存
    final InternalCache internalCache;  //内部缓存
    final SocketFactory socketFactory;  //socket 工厂
    final SSLSocketFactory sslSocketFactory; //安全套接层socket 工厂,用于HTTPS
    final CertificateChainCleaner certificateChainCleaner; // 验证确认响应证书 适用 HTTPS 请求连接的主机名。
    final HostnameVerifier hostnameVerifier;    //  主机名字确认
    final CertificatePinner certificatePinner;  //  证书链
    final Authenticator proxyAuthenticator;     //代理身份验证
    final Authenticator authenticator;      // 本地身份验证
    final ConnectionPool connectionPool;    //连接池,复用连接
    final Dns dns;  //域名
    final boolean followSslRedirects;  //安全套接层重定向
    final boolean followRedirects;  //本地重定向
    final boolean retryOnConnectionFailure; //重试连接失败
    final int connectTimeout;    //连接超时
    final int readTimeout; //read 超时
    final int writeTimeout; //write 超时

可以看出 OkHttpClient 相当于OKhttp网络请求的配置控制器。另外,OkHttpClient是建造者模式实现的,我们可以改变配置的一些参数,然后通过builder()方法进行构建,如果我们不手动设置,它就会使用默认的设置,如下:

            dispatcher = new Dispatcher();
            protocols = DEFAULT_PROTOCOLS;
            connectionSpecs = DEFAULT_CONNECTION_SPECS;
            proxySelector = ProxySelector.getDefault();
            cookieJar = CookieJar.NO_COOKIES;
            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;

2、newCall()方法

        @Override
        public Call newCall(Request request) {
            return new RealCall(this, request);
        }

准备要执行request:当通过建造者模式创建了Request之后,

再通过 Response response = client.newCall(request).execute();  开启GET请求的流程。

3、RealCall()方法

protected RealCall(OkHttpClient client, Request originalRequest) {  
    this.client = client;    
    this.originalRequest = originalRequest;    
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client);
}

真正的执行request:传一个 OkHttpClient 对象和 originalRequest 对象。

4、execute()方法

 @Override
    public Response execute() throws IOException {
        synchronized (this) {
            if (executed) throw new IllegalStateException("Already Executed"); //(1)
            executed = true;
        }
        try {
            client.dispatcher.executed(this);//(2)
            Response result = getResponseWithInterceptorChain();//(3)
            if (result == null) throw new IOException("Canceled");
            return result;
        }finally {
            client.dispatcher.finished(this);//(4)
        }
    }
1、加了同步锁 synchronized ,并检查 call 是否已被执行,每个 call 只能被执行一次。
2、进行实际执行。dispatcher 是 OkHttpClient.Builder 的一个成员,是 HTTP 请求的执行策略。
3、进行一系列“拦截”操作,并获取 HTTP 返回结果。(下一个就要说它)

4、通知 dispatcher 已经执行完毕。

5、getResponseWithInterceptorChain()


拦截器的责任链:真正发出网络请求、解析返回结果的重要方法!!! 

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

     Interceptor.Chain chain = new RealInterceptorChain(
             interceptors, null, null, null, 0, originalRequest);
     return chain.proceed(originalRequest); //(8)
 }
  1. 在配置 OkHttpClient 时设置一些 interceptors 拦截器们;
  2. 负责失败重试以及重定向的 RetryAndFollowUpInterceptor 拦截器;
  3. 负责把用户的请求转换为发送到服务器的请求和把服务器的返回转换为用户友好响应的拦截器;
  4. 负责读取缓存直接返回、更新缓存的拦截器;
  5. 负责和服务器建立连接的拦截器;
  6. 负责建立网络关系的拦截器;
  7. 负责向服务器发送请求、从服务器读取响应数据的拦截器。(下一个会说它)
  8. 开启链式调用:

6、RealInterceptorChain()

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      Connection connection) throws IOException {
    if (index >= interceptors.size()) throw new AssertionError();
    calls++;
    //如果我们已经有一个stream。确定即将到来的request会使用它
    if (this.httpCodec != null && !sameConnection(request.url())) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must retain the same host and port");
    }
    //如果我们已经有一个stream, 确定chain.proceed()唯一的call
    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);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
      throw new IllegalStateException("network interceptor " + interceptor
          + " must call proceed() exactly once");
    }
    if (response == null) {
      throw new NullPointerException("interceptor " + interceptor + " returned null");
    }
    return response;
  }

代码很多,主要代码如下:

    //调用链的下一个拦截器
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpCodec, connection, index + 1, request);    //(1)
    Interceptor interceptor = interceptors.get(index);     //(2)
    Response response = interceptor.intercept(next);    //(3)
1、实例化下一个拦截器对应的RealIterceptorChain对象,这个对象会在传递给当前的拦截器;
2、得到当前的拦截器:interceptors是存放拦截器的ArryList;
3、调用当前拦截器的intercept()方法,并将下一个拦截器的RealIterceptorChain对象传递下去


下面说负责失败重试以及重定向 :retryAndFollowUpInterceptor拦截器

7、RetryAndFollowUpInterceptor()

@Override 
public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        streamAllocation = new StreamAllocation(
                client.connectionPool(), createAddress(request.url()));
        int followUpCount = 0;
        Response priorResponse = null;
        while (true) {
            if (canceled) {
                streamAllocation.release();
                throw new IOException("Canceled");
            }
            Response response = null;
            boolean releaseConnection = true;
            try {
                response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);    //(1)
                releaseConnection = false;
            } catch (RouteException e) {
                //通过路线连接失败,请求将不会再发送
                if (!recover(e.getLastConnectException(), true, request)) throw e.getLastConnectException();
                releaseConnection = false;
                continue;
            } catch (IOException e) {
                // 与服务器尝试通信失败,请求不会再发送。
                if (!recover(e, false, request)) throw e;
                releaseConnection = false;
                continue;
            } finally {
                //抛出未检查的异常,释放资源
                if (releaseConnection) {
                    streamAllocation.streamFailed(null);
                    streamAllocation.release();
                }
            }
            // 附加上先前存在的response。这样的response从来没有body
            if (priorResponse != null) {
                response = response.newBuilder()
                        .priorResponse(priorResponse.newBuilder()
                                .body(null)
                                .build())
                        .build();
            }
            Request followUp = followUpRequest(response); //判断状态码 (2)
            if (followUp == null){
                if (!forWebSocket) {
                    streamAllocation.release();
                }
                return response;
            }
            closeQuietly(response.body());
            if (++followUpCount > MAX_FOLLOW_UPS) {
                streamAllocation.release();
                throw new ProtocolException("Too many follow-up requests: " + followUpCount);
            }
            if (followUp.body() instanceof UnrepeatableRequestBody) {
                throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
            }
            if (!sameConnection(response, followUp.url())) {
                streamAllocation.release();
                streamAllocation = new StreamAllocation(
                        client.connectionPool(), createAddress(followUp.url()));
            } else if (streamAllocation.codec() != null) {
                throw new IllegalStateException("Closing the body of " + response
                        + " didn't close its backing stream. Bad interceptor?");
            }
            request = followUp;
            priorResponse = response;
        }
    }

1、这里是最关键的代码,可以看出再这里直接调用了下一个拦截器,然后捕获可能的异常来进行操作;

2、这里对于返回的response的状态码进行判断,然后进行处理。

下面说,负责把用户的请求转换为发送到服务器的请求和把服务器的返回转换为用户友好响应的拦截器:

8、BridgeInterceptor()

@Override 
public Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();
    //检查request。将用户的request转换为发送到server的请求
    RequestBody body = userRequest.body();     //(1)
    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");
    }
    //GZIP压缩
    boolean transparentGzip = false;
    if (userRequest.header("Accept-Encoding") == null) {
      transparentGzip = true;
      requestBuilder.header("Accept-Encoding", "gzip");
    }
    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());   //(2)
    HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers()); //(3)
    Response.Builder responseBuilder = networkResponse.newBuilder()
        .request(userRequest);
    if (transparentGzip&& "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      GzipSource responseBody = new GzipSource(networkResponse.body().source());
      Headers strippedHeaders = networkResponse.headers().newBuilder()
          .removeAll("Content-Encoding")
          .removeAll("Content-Length")
          .build();
      responseBuilder.headers(strippedHeaders);
      responseBuilder.body(new RealResponseBody(strippedHeaders, Okio.buffer(responseBody)));
    }
    return responseBuilder.build();
  }

1、下面一段代码都是 BridgeInterceptor 对于 request 的格式进行检查,让构建了一个新的request;

2、调用下一个interceptor来得到response;

3、下面都是对得到的response进行一些判断操作,最后将结果返回。

@Override 
public Response intercept(Chain chain) throws IOException {
    Response cacheCandidate = cache != null   //(1)
        ? cache.get(chain.request()) //通过request得到缓存
        : null;
    long now = System.currentTimeMillis();
    CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get(); //(2)
    Request networkRequest = strategy.networkRequest;
    Response cacheResponse = strategy.cacheResponse;
    if (cache != null) {
      cache.trackResponse(strategy);
    }
    if (cacheCandidate != null && cacheResponse == null) { //存在缓存的response,但是不允许缓存
      closeQuietly(cacheCandidate.body()); // 缓存不适合,关闭
    }
      //如果我们禁止使用网络,且缓存为null,失败
    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(EMPTY_BODY)
          .sentRequestAtMillis(-1L)
          .receivedResponseAtMillis(System.currentTimeMillis())
          .build();
    }
    if (networkRequest == null) {  //没有网络请求,跳过网络,返回缓存
      return cacheResponse.newBuilder()
          .cacheResponse(stripBody(cacheResponse))
          .build();
    }
    Response networkResponse = null;
    try {
      networkResponse = chain.proceed(networkRequest);//网络请求拦截器    //======(3)
    } finally {
        //如果我们因为I/O或其他原因崩溃,不要泄漏缓存体
      if (networkResponse == null && cacheCandidate != null) {
        closeQuietly(cacheCandidate.body());
      }
    }
    //(4)
      //如果我们有一个缓存的response,然后我们正在做一个条件GET
    if (cacheResponse != null) {
      if (validate(cacheResponse, networkResponse)) { //比较确定缓存response可用
        Response response = cacheResponse.newBuilder()
            .headers(combine(cacheResponse.headers(), networkResponse.headers()))
            .cacheResponse(stripBody(cacheResponse))
            .networkResponse(stripBody(networkResponse))
            .build();
        networkResponse.body().close();
         //更新缓存,在剥离content-Encoding之前
        cache.trackConditionalCacheHit();
        cache.update(cacheResponse, response);
        return response;
      } else {
        closeQuietly(cacheResponse.body());
      }
    }
    Response response = networkResponse.newBuilder()
        .cacheResponse(stripBody(cacheResponse))
        .networkResponse(stripBody(networkResponse))
        .build();
    if (HttpHeaders.hasBody(response)) {    // (5)
      CacheRequest cacheRequest = maybeCache(response, networkResponse.request(), cache);
      response = cacheWritingResponse(cacheRequest, response);
    }
    return response;
  }
  • 首先,根据request来判断cache中是否有缓存的response,如果有,得到这个response,然后进行判断当前response是否有效,没有将cacheCandate赋值为空。
  • 根据request判断缓存的策略,是否要使用了网络,缓存 或两者都使用
  • 调用下一个拦截器,决定从网络上来得到response
  • 如果本地已经存在cacheResponse,那么让它和网络得到的networkResponse做比较,决定是否来更新缓存的cacheResponse
  • 缓存未经缓存过的response

9、ConnectInterceptor()建立连接

@Override 
public Response intercept(Chain chain) throws IOException {
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Request request = realChain.request();
    StreamAllocation streamAllocation = realChain.streamAllocation();

    boolean doExtensiveHealthChecks = !request.method().equals("GET");
    HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();

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

实际上建立连接就是创建了一个 HttpCodec 对象,它将在后面的步骤中被使用,那它又是何方神圣呢?它是对 HTTP 协议操作的抽象,有两个实现:Http1Codec和Http2Codec,顾名思义,它们分别对应 HTTP/1.1 和 HTTP/2 版本的实现。
在Http1Codec中,它利用 Okio 对Socket的读写操作进行封装,Okio 以后有机会再进行分析,现在让我们对它们保持一个简单地认识:它对java.io和java.nio进行了封装,让我们更便捷高效的进行 IO 操作。
而创建HttpCodec对象的过程涉及到StreamAllocation、RealConnection,代码较长,这里就不展开,这个过程概括来说,就是找到一个可用的RealConnection,再利用RealConnection的输入输出(BufferedSource和BufferedSink)创建HttpCodec对象,供后续步骤使用。

10、CallServerInterceptor()发送和接收数据

 @Override public Response intercept(Chain chain) throws IOException {
    HttpCodec httpCodec = ((RealInterceptorChain) chain).httpStream();
    StreamAllocation streamAllocation = ((RealInterceptorChain) chain).streamAllocation();
    Request request = chain.request();
    long sentRequestMillis = System.currentTimeMillis();
    httpCodec.writeRequestHeaders(request);
    if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {   //===(1)
      Sink requestBodyOut = httpCodec.createRequestBody(request, request.body().contentLength());
      BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);
      request.body().writeTo(bufferedRequestBody);
      bufferedRequestBody.close();
    }
    httpCodec.finishRequest();
    Response response = httpCodec.readResponseHeaders()     //====(2)
        .request(request)
        .handshake(streamAllocation.connection().handshake())
        .sentRequestAtMillis(sentRequestMillis)
        .receivedResponseAtMillis(System.currentTimeMillis())
        .build();
    if (!forWebSocket || response.code() != 101) {
      response = response.newBuilder()
          .body(httpCodec.openResponseBody(response))
          .build();
    }
    if ("close".equalsIgnoreCase(response.request().header("Connection"))
        || "close".equalsIgnoreCase(response.header("Connection"))) {
      streamAllocation.noNewStreams();
    }
    int code = response.code();
    if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
      throw new ProtocolException(
          "HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
    }
    return response;
  }
  1. 检查请求方法,用Httpcodec处理request;
  2. 进行网络请求得到response;
  3. 返回response。

小结:前面说了拦截器用了责任链设计模式,它将请求一层一层向下传,知道有一层能够得到Resposne就停止向下传递,然后将response向上面的拦截器传递,然后各个拦截器会对respone进行一些处理,最后会传到RealCall类中通过execute来得到esponse。


异步请求流程分析

private final OkHttpClient client = new OkHttpClient();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://publicobject.com/helloworld.txt")
        .build();

    client.newCall(request).enqueue(new Callback() {
      @Override 
      public void onFailure(Call call, IOException e) {
        e.printStackTrace();
      }

      @Override 
      public void onResponse(Call call, Response response) throws IOException {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        Headers responseHeaders = response.headers();
        for (int i = 0, size = responseHeaders.size(); i < size; i++) {
          System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }
        System.out.println(response.body().string());
      }
    });
  }

由代码中client.newCall(request).enqueue(Callback),开始我们知道client.newCall(request)方法返回的是RealCall对象,接下来继续向下看enqueue()方法:

 //异步任务使用
    @Override 
    public void enqueue(Callback responseCallback) {
        synchronized (this) {
            if (executed) throw new IllegalStateException("Already Executed");
            executed = true;
        }
        client.dispatcher().enqueue(new AsyncCall(responseCallback));
    }

调用了上面我们没有详细说的Dispatcher类中的enqueue(Call )方法.接着继续看:

synchronized void enqueue(AsyncCall call) {
        if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
            runningAsyncCalls.add(call);
            executorService().execute(call);
        } else {
            readyAsyncCalls.add(call);
        }
    }

如果中的runningAsynCalls不满,且call占用的host小于最大数量,则将call加入到runningAsyncCalls中执行,同时利用线程池执行call;否者将call加入到readyAsyncCalls中。runningAsyncCalls和readyAsyncCalls是什么呢?看下面:

/** Ready async calls in the order they'll be run. */
private final Deque readyAsyncCalls = new ArrayDeque<>(); //正在准备中的异步请求队列

/** Running asynchronous calls. Includes canceled calls that haven't finished yet. */
private final Deque runningAsyncCalls = new ArrayDeque<>(); //运行中的异步请求

/** Running synchronous calls. Includes canceled calls that haven't finished yet. */
private final Deque runningSyncCalls = new ArrayDeque<>(); //同步请求

call加入到线程池中执行了。现在再看AsynCall的代码,它是RealCall中的内部类:

//异步请求
    final class AsyncCall extends NamedRunnable {
        private final Callback responseCallback;

        private AsyncCall(Callback responseCallback) {
            super("OkHttp %s", redactedUrl());
            this.responseCallback = responseCallback;
        }

        String host() {
            return originalRequest.url().host();
        }

        Request request() {
            return originalRequest;
        }

        RealCall get() {
            return RealCall.this;
        }

        @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 {
                    responseCallback.onFailure(RealCall.this, e);
                }
            } finally {
                client.dispatcher().finished(this);
            }
        }
    }

AysncCall中的execute()中的方法,同样是通过Response response = getResponseWithInterceptorChain();来获得response,这样异步任务也同样通过了interceptor,剩下的流程就和上面一样了。

束。


你可能感兴趣的:(Android,UI\HTTP框架)