Source code analysis of android okhttp

Posted by Evoke on Mon, 06 May 2019 06:15:03 +0200

okhttp, a powerful development contribution of Square, is now the mainstream Android network framework.
Now let's look at this framework and explore its source code.

    compile 'com.squareup.okhttp3:okhttp:3.8.0'

The version we analyzed was 3.8.0.

public class OkHttpClient implements Cloneable, Call.Factory, WebSocket.Factory {

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

   * Uses {@code request} to connect a new web socket.
  @Override public WebSocket newWebSocket(Request request, WebSocketListener listener) {
    RealWebSocket webSocket = new RealWebSocket(request, listener, new Random());
    return webSocket;

First you see that the facade class OkHttpClient implements two factory methods

public interface Call extends Cloneable {
  Request request();
  Response execute() throws IOException;
  void enqueue(Callback responseCallback);
  void cancel();
  boolean isExecuted();
  boolean isCanceled();
  Call clone();
  interface Factory {
    Call newCall(Request request);

Call is also an interface method that understands well, requests, executions, queues (internal queues, later analysis), and methods of returning status.

public interface WebSocket {
  Request request();
  long queueSize();
  boolean send(String text);
  boolean send(ByteString bytes);
  boolean close(int code, @Nullable String reason);

  void cancel();

  interface Factory {
    WebSocket newWebSocket(Request request, WebSocketListener listener);

WebSocket interface

static final List<Protocol> DEFAULT_PROTOCOLS = Util.immutableList(
      Protocol.HTTP_2, Protocol.HTTP_1_1);//Connection protocol

  static final List<ConnectionSpec> DEFAULT_CONNECTION_SPECS = Util.immutableList(
      ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);//line type

  static {
    Internal.instance = new Internal() {
      @Override public void addLenient(Headers.Builder builder, String line) {

      @Override public void addLenient(Headers.Builder builder, String name, String value) {
        builder.addLenient(name, value);

      @Override public void setCache(OkHttpClient.Builder builder, InternalCache internalCache) {

      @Override public boolean connectionBecameIdle(
          ConnectionPool pool, RealConnection connection) {
        return pool.connectionBecameIdle(connection);

      @Override public RealConnection get(ConnectionPool pool, Address address,
          StreamAllocation streamAllocation, Route route) {
        return pool.get(address, streamAllocation, route);

      @Override public boolean equalsNonHost(Address a, Address b) {
        return a.equalsNonHost(b);

      @Override public Socket deduplicate(
          ConnectionPool pool, Address address, StreamAllocation streamAllocation) {
        return pool.deduplicate(address, streamAllocation);

      @Override public void put(ConnectionPool pool, RealConnection connection) {

      @Override public RouteDatabase routeDatabase(ConnectionPool connectionPool) {
        return connectionPool.routeDatabase;

      @Override public int code(Response.Builder responseBuilder) {
        return responseBuilder.code;

      public void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean isFallback) {
        tlsConfiguration.apply(sslSocket, isFallback);

      @Override public HttpUrl getHttpUrlChecked(String url)
          throws MalformedURLException, UnknownHostException {
        return HttpUrl.getChecked(url);

      @Override public StreamAllocation streamAllocation(Call call) {
        return ((RealCall) call).streamAllocation();

      @Override public Call newWebSocketCall(OkHttpClient client, Request originalRequest) {
        return new RealCall(client, originalRequest, true);

  final Dispatcher dispatcher;
  final @Nullable Proxy proxy;
  final List<Protocol> protocols;
  final List<ConnectionSpec> connectionSpecs;
  final List<Interceptor> interceptors;
  final List<Interceptor> networkInterceptors;
  final EventListener.Factory eventListenerFactory;
  final ProxySelector proxySelector;
  final CookieJar cookieJar;
  final @Nullable Cache cache;
  final @Nullable InternalCache internalCache;
  final SocketFactory socketFactory;
  final @Nullable SSLSocketFactory sslSocketFactory;
  final @Nullable CertificateChainCleaner certificateChainCleaner;
  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;
  final int writeTimeout;
  final int pingInterval;

You can see that there are many member variables. Many variables know what to do when they are named. At present, this version of okhttpclient applies builder mode. It has many defaults. You just need to configure what you care about.

 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;

Connect Timeout, Connect Timeout, Write Timeout basically changed the price. After all, the mobile phone will silently set a longer network environment. RetryOn Connection Failure default retry is good. cookieJar = CookieJar.NO_COOKIES does not have cookie s saved.

public interface CookieJar {
  /** A cookie jar that never accepts any cookies. */
  CookieJar NO_COOKIES = new CookieJar() {
    @Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {

    @Override public List<Cookie> loadForRequest(HttpUrl url) {
      return Collections.emptyList();

  void saveFromResponse(HttpUrl url, List<Cookie> cookies);

  List<Cookie> loadForRequest(HttpUrl url);

NO_COOKIES is the default implementation. You can see that saveFromResponse does nothing. Load ForRequest, take an empty request. So generally, we need to implement this interface by ourselves to implement what cookie-related requirements. Setting it in, it is roughly based on the host as the key, persistence, and take it with us next time.

Following is a step-by-step analysis based on our usual API approach

  Request.Builder builder = new Request.Builder();
    Object object = params.createParameters();
   FormBody.Builder formBuild = new FormBody.Builder();
      if (object != null && object instanceof List) {
			List<KeyValuePair> list = (List<KeyValuePair>) object;
             if (list != null && !list.isEmpty()) {
              //Create a FormBody.Builder
                     for (KeyValuePair keyValuePair : list) {
                             formBuild.add(keyValuePair.getKey(), keyValuePair.getValue());
 Request request = builder.url(params.getRequestURL()) .post(;
    Call call = okHttpClient.newCall(request);
    Response response = call.execute();

API is still very convenient. Request.Builder assembles parameters, sets URL s, sets method parameters, bu ild returns a request, okHttpClient. newCall returns a call type, call has an execution method, and returns the result.

First look at Request Request Builder

public final class Request {
  final HttpUrl url;
  final String method;
  final Headers headers;
  final @Nullable RequestBody body;
  final Object tag;

  private volatile CacheControl cacheControl; // Lazily initialized.

  Request(Builder builder) {
    this.url = builder.url;
    this.method = builder.method;
    this.headers =;
    this.body = builder.body;
    this.tag = builder.tag != null ? builder.tag : this;

  public HttpUrl url() {
    return url;

  public String method() {
    return method;

  public Headers headers() {
    return headers;

  public String header(String name) {
    return headers.get(name);

  public List<String> headers(String name) {
    return headers.values(name);

  public @Nullable RequestBody body() {
    return body;

  public Object tag() {
    return tag;

  public Builder newBuilder() {
    return new Builder(this);

  public CacheControl cacheControl() {
    CacheControl result = cacheControl;
    return result != null ? result : (cacheControl = CacheControl.parse(headers));

  public boolean isHttps() {
    return url.isHttps();

  @Override public String toString() {
    return "Request{method="
        + method
        + ", url="
        + url
        + ", tag="
        + (tag != this ? tag : null)
        + '}';

  public static class Builder {
    HttpUrl url;
    String method;
    Headers.Builder headers;
    RequestBody body;
    Object tag;

    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.tag = request.tag;
      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);

      HttpUrl parsed = HttpUrl.parse(url);
      if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
      return url(parsed);

    public Builder url(URL url) {
      if (url == null) throw new NullPointerException("url == null");
      HttpUrl parsed = HttpUrl.get(url);
      if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
      return url(parsed);

    public Builder header(String name, String value) {
      headers.set(name, value);
      return this;

    public Builder addHeader(String name, String value) {
      headers.add(name, value);
      return this;

    public Builder removeHeader(String name) {
      return this;

    public Builder headers(Headers headers) {
      this.headers = headers.newBuilder();
      return this;

    public Builder cacheControl(CacheControl cacheControl) {
      String value = cacheControl.toString();
      if (value.isEmpty()) return removeHeader("Cache-Control");
      return header("Cache-Control", value);

    public Builder get() {
      return method("GET", null);

    public Builder head() {
      return method("HEAD", null);

    public Builder post(RequestBody body) {
      return method("POST", body);

    public Builder delete(@Nullable RequestBody body) {
      return method("DELETE", body);

    public Builder delete() {
      return delete(Util.EMPTY_REQUEST);

    public Builder put(RequestBody body) {
      return method("PUT", body);

    public Builder patch(RequestBody body) {
      return method("PATCH", body);

    public Builder method(String method, @Nullable RequestBody body) {
      if (method == null) throw new NullPointerException("method == null");
      if (method.length() == 0) throw new IllegalArgumentException("method.length() == 0");
      if (body != null && !HttpMethod.permitsRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must not have a request body.");
      if (body == null && HttpMethod.requiresRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must have a request body.");
      this.method = method;
      this.body = body;
      return this;

    public Builder tag(Object tag) {
      this.tag = tag;
      return this;

    public Request build() {
      if (url == null) throw new IllegalStateException("url == null");
      return new Request(this);

High quality code is a lot of judgement parameters, for URLs. Method, URL parsing, which method do not need body, is very careful. It basically supports http method. When there are many parameters, it is basically builder mode, which is also a habit.

Topics: socket DNS OkHttp network