OKHttp3源码分析-流程与结构

本文深入探讨OkHttp3的使用方法,包括获取OkHttpClient对象、创建Request对象、调用execute和enqueue方法。分析了OkHttpClient的Builder模式,Request的组成部分,Call对象的创建以及execute和enqueue的执行流程。同时介绍了拦截器的实现,利用责任链模式进行网络请求的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本用法

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

//1. 获取OkHttpClient对象
OkHttpClient client = new OkHttpClient.Builder().build();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  //2. 获取Request对象
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  //3. 获取Call对象,并调用同步的execute方法,或者异步的enqueue方法
  Response response = client.newCall(request).execute();
  return response.body().string();
}
  1. 获取OkHttpClient对象
  2. 获取Request对象
  3. 根据Request来获取Call对象
  4. 最后是调用Call的同步方法execute或者异步的enqueue方法来进行网络请求。

我们会根据这个四个步骤来分析OkHttp的源码。

获取OkHttpClient对象

获取OkHttpClient对象流程:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LCDw9Tbj-1615800271979)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/%E8%8E%B7%E5%8F%96OkHttpClient.png?raw=true)]

获取OkHttpClient是通过OkHttpClient.Builder来创建。整体采用Builder设计模式

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AsrbFNcr-1615800271981)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/OkHttpClient%E4%B8%8EBuilder.png?raw=true)]

添加超时时间可以通过

  • connetionTimeOut方法来设置连接超时时间
  • readTimeOut方法来设置读取的超时时间
  • writeTimeOut方法来设置写入的超时时间

添加Cache,通过cache方法来设置

也可以设置拦截器,通过addInterceptor来设置拦截器。

创建Request对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E3Nv4NDt-1615800271983)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Request%E7%B1%BB%E7%BB%93%E6%9E%84.png?raw=true)]

Request基本包含四部分

  • method,说明是通过什么方式来进行网络请求
  • HttpUrl说明请求的Url,包含schemehostportpath,还有查询参数。
  • Headers,请求的头部信息
  • RequestBody,请求的实体部分

RequestHeadersRequestBodyHttpUrl采用的都是Builder设计模式。

获取Call对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FtuOavRE-1615800271984)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/%E8%8E%B7%E5%8F%96Call%E5%AF%B9%E8%B1%A1.png?raw=true)]

获取Call对象是通过OKHttpClientnewCall方法。

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

然后调用RealCallnewRealCall创建Call对象。

final class RealCall implements Call {
    static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    	// Safely publish the Call instance to the EventListener.
    	RealCall call = new RealCall(client, originalRequest, forWebSocket);
    	call.eventListener = client.eventListenerFactory().create(call);
    	return call;
  }
}

整体采用抽象工厂模式,如下图OkHttpClient继承CallFactory,依赖RealCallRealCall继承自Call

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SdNEirhN-1615800271986)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%B1%BB%E7%9B%B8%E5%85%B3%E7%9A%84%E7%BB%93%E6%9E%84.png?raw=true)]

调用Call的方法

Call中两个请求方法:

  • execute方法进行同步请求
  • enqueue方法进行异步网络请求

execute方法

上一节我们知道返回的Call实际为RealCall对象。

调用的顺序如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YH05RQRf-1615800271987)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%9A%84execute%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B.png?raw=true)]

  • 调用Dispatcherexecuted方法
  • 调用RealCallgetResponseWithInterceptorChain方法获取Response(拦截器一节会说明此方法)
  • 调用Dispatcherfinished方法
final class RealCall implements Call {
    @Override public Response execute() throws IOException {
    	//设置executed为true,说明RealCall已经执行,每个RealCall只能执行一次
    	synchronized (this) {
      		if (executed) throw new IllegalStateException("Already Executed");
      		executed = true;
    	}
    	······
    	try {
      		//调用Dispatcher的executed
      		client.dispatcher().executed(this);
      		//调用getResponseWithInterceptorChain获取Response
      		Response result = getResponseWithInterceptorChain();
      		if (result == null) throw new IOException("Canceled");
      		return result;
    	} catch (IOException e) {
      		throw e;
   		} finally {
      		client.dispatcher().finished(this);
    	}
  }
}

enqueue方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qWmvJ4Uv-1615800271988)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Call%E7%9A%84enqueue%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C.png?raw=true)]
调用RealCallenqueue的方法的时候会调用到Dispatcherenqueue方法,

final class RealCall implements Call {
    @Override
    public void enqueue(Callback responseCallback) {
        synchronized (this) {
            if (executed) throw new IllegalStateException("Already Executed");
            executed = true;
        }
        //调用到Dispatcher的enqueue
        client.dispatcher().enqueue(new AsyncCall(responseCallback));
    }
}

Dispatcherenqueue方法中会添加CallreadyAsyncCalls中,然后调用其promoteAndExecute方法。

public final class Dispatcher {
  ......
  void enqueue(AsyncCall call) {
    synchronized (this) {
      readyAsyncCalls.add(call);
    }
    promoteAndExecute();
  }
  ......
}

DispatcherpromoteAndExecute方法中会检测当前的运行的Call时候超过了最大值,超过了直接break。没有超过会放入本地的executableCallsList集合中,然后会遍历调用executableCalls中的AsyncCall对象的executeOn方法。DispatcherexecutorService方法返回的是当前设置的线程池,如果没有设置就使用默认的线程池。

public final class Dispatcher {
  ......
  private boolean promoteAndExecute() {
    ......
    List<AsyncCall> executableCalls = new ArrayList<>();
    boolean isRunning;
    synchronized (this) {
      for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
        AsyncCall asyncCall = i.next();

        if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.
        if (runningCallsForHost(asyncCall) >= maxRequestsPerHost) continue; // Host max capacity.

        i.remove();
        executableCalls.add(asyncCall);
        runningAsyncCalls.add(asyncCall);
      }
      isRunning = runningCallsCount() > 0;
    }

    for (int i = 0, size = executableCalls.size(); i < size; i++) {
      AsyncCall asyncCall = executableCalls.get(i);
      asyncCall.executeOn(executorService());
    }
    return isRunning;
  }
  ......
}

我们接下来分析一下AsyncCallexecuteOn方法。通过ExecutorService对象来执行AsyncCallAsyncCall继承自NamedRunnable,并实现其execute方法。

final class AsyncCall extends NamedRunnable  {
    void executeOn(ExecutorService executorService) {
      assert (!Thread.holdsLock(client.dispatcher()));
      boolean success = false;
      try {
        executorService.execute(this);
        success = true;
      } catch (RejectedExecutionException e) {
        InterruptedIOException ioException = new InterruptedIOException("executor rejected");
        ioException.initCause(e);
        eventListener.callFailed(RealCall.this, ioException);
        responseCallback.onFailure(RealCall.this, ioException);
      } finally {
        if (!success) {
          client.dispatcher().finished(this); // This call is no longer running!
        }
      }
    }
}

AsyncCall调用RealCallgetResponseWithInterceptorChain方法(拦截器一节会说明此方法)来获取Response,然后根据是否取消调用相应的回调方法。

final class AsyncCall extends NamedRunnable  {
     @Override protected void execute() {
      ······
      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) {
        ······
      } finally {
        client.dispatcher().finished(this);
      }
    }
}

拦截器

前面调用RealCallgetResponseWithInterceptorChain来获取到响应Response。其实使用了责任链的方式,我们来一起分析一下。

把拦截器添加到责任链中。

final class RealCall implements Call {
  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> 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 (!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);
  }
}

基本结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pH1x6iy2-1615800271989)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Interceptor%E7%B1%BB%E7%BB%93%E6%9E%84.png?raw=true)]
拦截器的执行顺序:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Uttcwtb-1615800271990)(https://github.com/wfeii/wfeii.github.io_raw_important/blob/master/OkHttpClient3.4/Interceptor%E7%9A%84%E6%89%A7%E8%A1%8C%E6%B5%81%E7%A8%8B.png?raw=true)]
这里只是简单的说明执行顺序,如何运行的可以查看拦截器的intercept方法。
后面我们会详细讲解各个Interceptor的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值