ijava学习网> 知识分享> Web 服务> okhttp流程的分析

okhttp流程的分析

时间: 2018-11-09 09:46:19 标签

1、okhttp网络请求

异步调用

 
   private void testOkhttpGet() {
        String url = "http://api.k780.com/?app=weather.history";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).get().build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {
                showResult(e.getMessage());
            }@Overridepublic void onResponse(Call call, okhttp3.Response response) throws IOException {
                showResult(response.body().string());
            }
        });

    }

同步调用

    private String testOkhttpGet() throws IOException {
        String url = "http://api.k780.com/?app=weather.history";

        Request request = new Request.Builder().url(url).get().build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Response response = okHttpClient.newCall(request).execute();if (response.isSuccessful()) {return response.body().string();
        } else {throw new IOException("Unexpected code " + response);
        }

    }

2、流程图

就以上的调用步骤总结一个流程图

7e153093b9ede9b85d6c76f94b15267930f.jpg

3、流程分析

3.1 创建OKHttpClient对象

OkHttpClient client = new OkHttpClient();

其实OkHttpClient使用了建造者模式构造实例,并且启用了默认的配置,其构造函数如下:

public OkHttpClient() {  this(new Builder());
}

OkHttpClient(Builder builder) {this.dispatcher = builder.dispatcher;this.proxy = builder.proxy;this.protocols = builder.protocols;this.connectionSpecs = builder.connectionSpecs;this.interceptors = Util.immutableList(builder.interceptors);this.networkInterceptors = Util.immutableList(builder.networkInterceptors);this.eventListenerFactory = builder.eventListenerFactory;this.proxySelector = builder.proxySelector;this.cookieJar = builder.cookieJar;this.cache = builder.cache;this.internalCache = builder.internalCache;this.socketFactory = builder.socketFactory;boolean isTLS = false;for (ConnectionSpec spec : connectionSpecs) {
      isTLS = isTLS || spec.isTls();
    }if (builder.sslSocketFactory != null || !isTLS) {      this.sslSocketFactory = builder.sslSocketFactory;      this.certificateChainCleaner = builder.certificateChainCleaner;
    } else {
      X509TrustManager trustManager = Util.platformTrustManager();      this.sslSocketFactory = newSslSocketFactory(trustManager);      this.certificateChainCleaner = CertificateChainCleaner.get(trustManager);
    }if (sslSocketFactory != null) {
      Platform.get().configureSslSocketFactory(sslSocketFactory);
    }this.hostnameVerifier = builder.hostnameVerifier;this.certificatePinner = builder.certificatePinner.withCertificateChainCleaner(
        certificateChainCleaner);this.proxyAuthenticator = builder.proxyAuthenticator;this.authenticator = builder.authenticator;this.connectionPool = builder.connectionPool;this.dns = builder.dns;this.followSslRedirects = builder.followSslRedirects;this.followRedirects = builder.followRedirects;this.retryOnConnectionFailure = builder.retryOnConnectionFailure;this.connectTimeout = builder.connectTimeout;this.readTimeout = builder.readTimeout;this.writeTimeout = builder.writeTimeout;this.pingInterval = builder.pingInterval;if (interceptors.contains(null)) {      throw new IllegalStateException("Null interceptor: " + interceptors);
    }if (networkInterceptors.contains(null)) {      throw new IllegalStateException("Null network interceptor: " + networkInterceptors);
    }
  }//Builder是OkHttpClient的内部类... public Builder() {
      dispatcher = new Dispatcher();
      protocols = DEFAULT_PROTOCOLS;
      connectionSpecs = DEFAULT_CONNECTION_SPECS;
      eventListenerFactory = EventListener.factory(EventListener.NONE);
      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;
      pingInterval = 0;
    }

3.1 创建Request对象

同样也是通过建造者模式来构建Request对象,

Request request = new Request.Builder().url(url).get().build();public static class Builder {HttpUrl url;
    String method;
    Headers.Builder headers;
    RequestBody body;public Builder() {      this.method = "GET";      this.headers = new Headers.Builder();
    }

    Builder(Request request) {      this.url = request.url;      this.method = request.method;      this.body = request.body;      this.tags = request.tags.isEmpty()
          ? Collections.<Class, Object>emptyMap()
          : new LinkedHashMap<>(request.tags);      this.headers = request.headers.newBuilder();
    }public Builder url(HttpUrl url) {      if (url == null) throw new NullPointerException("url == null");      this.url = url;      return this;
    }public Builder url(String url) {      if (url == null) throw new NullPointerException("url == null");      // Silently replace web socket URLs with HTTP URLs.  if (url.regionMatches(true, 0, "ws:", 0, 3)) {
        url = "http:" + url.substring(3);
      } else if (url.regionMatches(true, 0, "wss:", 0, 4)) {
        url = "https:" + url.substring(4);
      }      return url(HttpUrl.get(url));
    }  public Builder url(URL url) {      if (url == null) throw new NullPointerException("url == null");      return url(HttpUrl.get(url.toString()));
    }public Request build() {      if (url == null) throw new IllegalStateException("url == null");      return new Request(this);
    }//部分省略
  }

3.2 OKHttpClient调用newCall创建Call

同步调用:Response response = okHttpClient.newCall(request).execute();

然后通过Response result = getResponseWithInterceptorChain();返回Response

实际是Call是个接口,其实现类是RealCall,

 //OkHttpClient.java
 /**
   * Prepares the {@code request} to be executed at some point in the future.
   */
  @Override public Call newCall(Request request) {return RealCall.newRealCall(this, request, false /* for web socket */);
  }//RealCall.java

  @Override public Response execute() throws IOException {synchronized (this) {      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);try {
      client.dispatcher().executed(this);
      Response result = getResponseWithInterceptorChain();      if (result == null) throw new IOException("Canceled");      return result;
    } catch (IOException e) {
      eventListener.callFailed(this, e);      throw e;
    } finally {
      client.dispatcher().finished(this);
    }
  }

异步调用:okHttpClient.newCall(request).enqueue(new Callback()...);

enqueue() 通过client.dispatcher().enqueue(new AsyncCall(responseCallback));

将一个AsynCall传给了ExecutorService这个线程池接口,AsynCall实际上是一个Runnable,在AsynCall的execute()方法里同样是通过:

Response response = getResponseWithInterceptorChain()来获取Response的,见下面源码:

//RealCall.java@Override public void enqueue(Callback responseCallback) {synchronized (this) {      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);
    client.dispatcher().enqueue(new AsyncCall(responseCallback));
  }//RealCall#AsyncCallfinal class AsyncCall extends NamedRunnable {private final Callback responseCallback;

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

    ... ...@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);
      }
    }
  }

4、getResponseWithInterceptorChain

不管是同步还是异步,可见关键点是:getResponseWithInterceptorChain

这里面主要是将各个Interceptor存入interceptors集合中,然后生成一个Interceptor.Chain实例,

RealInterceptorChain是Interceptor.Chain的实现类,然后调用其proceed方法,通过链式调用各个Intecepter处理request获取对应response,这个response可能至缓存、网络

//RealCall.javaResponse getResponseWithInterceptorChain() throws IOException {// Build a full stack of interceptors.Listinterceptors = 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 (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    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);
  }

5、Interceptor

Interceptor是okhttp核心接口类,其有5个实现类跟别是:

RetryAndFollowUpInterceptor:负责网络请求重试以及重定向

BridgeInterceptor:把用户请求构建成网络请求,然后连接网络,根据网络响应数据构建用户响应

CacheInterceptor:从缓存中读取已经缓存的数据,和更新缓存

ConnectInterceptor:功能是和目标服务器建立连接,然后执行下一个拦截器

CallServerInterceptor:这是整个调用链的最后一个拦截器,向目标服务器发送求情数据并获取服务器响应数据

整个拦截器链条环环相扣,各司其职,各个不同的拦截器前后顺序是既定,这是典型的责任链模式的例子。

这五类Inteceptor各自都实现了父类的intercept(Chain chain)的方法,都有可能返回给对应的请求的response

5.1、RetryAndFollowUpInterceptor

源码就不出来了,可以下载okhttp源码后对照看,可以看到其intercept方法中会通过while(true)的方式循环的处理一个请求直到获取到服务器正确响应,或者重试的次数哦超过最大限制20次,如果超过20次会关闭socket流以及释放资源。

5.2、BridgeInterceptor

组装一个request,并通过chain.proceed来获取networkResponse,如果资源是gzip格式个,则生成gzip资源,它同时能够使用CookieJar来策略性的使用Cookie,将Cookie添加到header中。

5.3、CacheInterceptor

返回缓存中请求过得数据,主要通过Cache类保存缓存数据,Cache通过DiskLruCache来持久化数据,DiskLruCache通过LinkedHashMap的数据格式来缓存数据,当超过缓存上线的时候,便会根据LRU算法来删除最近最少使用的元素。

5.4、ConnectInterceptor

只是负责和目标服务器连接,已经进入下一个,它其实调了RealInterceptorChain的proceed方法

5.5、CallServerInterceptor

这个是调用链的最后一环,和服务器进行连接并且获取服务器返回的结果,并且根据缓存策略缓存数据

 6、总结:

不管是同步还是异步请求都是getResponseWithInterceptorChain方法,然后使用Intercept,通过责任链的调用模式分级实现缓存获取,gzip压缩以及网络IO功能,最后获取request对应的response

                          

版权说明| 关于ijava| 合作伙伴| 联系我们| 网站地图| 招贤纳士

Copyright © 2017 www.ijava.com All Rights Reserved 版权所有•ijava学习网 京ICP备14061482号-18         官方QQ:3325669927

ijava学习网提供免费java教程和大量java面试题库,给高级会员提供免费的java培训,同时提共一些java开发项目和java下载,java工程师,java菜鸟们快来哦。记住我们的网站:www.ijava.com