Request,RealCall 分析
源码地址:https://github.com/square/okhttp
上一篇 对okHttpClient做了简单的分析,现在就对另外两个比较重要的类进行学习分析(Request, RealCall),这两个类是我们调用时候的请求相关的类。
这里做简单的学习分析,便于后面流程的理解。
Request
Request这个请求类,同样的使用了Bulider模式,不过这个比OkhttpClient简单多了,主要用于添加请求需要的一些基本属性。
final HttpUrl url; final String method; final Headers headers; final RequestBody body; final Object tag; Request(Builder builder) { this.url = builder.url; this.method = builder.method; this.headers = builder.headers.build(); this.body = builder.body; this.tag = builder.tag != null ? builder.tag : this; }
- url,传入的String ,经过进一步加工处理,还有一些错误的判断,最后转换为HttpUrl的类。
- method,确定请求的类型,默认使用GET。支持http 基本的请求方式。
- headers, 我们可以自定义添加对应的Header , 最后转换成Header类。也可以对其中的Header进行操作。
- body,请求的实体,RequestBody ,传输的contentType,把传输的content,进行简单处理。
- tag,标签。
RealCall
RealCall, 实现了Call接口,也是OkHttp里面唯一一个Call的实现类。
1. RealCall几个重要的变量:
final OkHttpClient client; final RetryAndFollowUpInterceptor retryAndFollowUpInterceptor; final Request originalRequest;
client : 在上节中已经对其进行介绍,RealCall 在构造方法中以参数的形式传进来,供内部使用。
retryAndFollowUpInterceptor : 在第一节中也做过简单介绍,是一个拦截器,处理重试,网络错误,取消请求,以及请求重定向的一些操作。(后面会详细学习分析)
**originalRequest : ** 原始的请求类。
2. RealCall里面有个几个重要的方法:
execute() 同步请求
@Override public Response execute() throws IOException { synchronized (this) { if (executed) throw new IllegalStateException("Already Executed"); executed = true; } captureCallStackTrace(); try { client.dispatcher().executed(this); Response result = getResponseWithInterceptorChain(); if (result == null) throw new IOException("Canceled"); return result; } finally { client.dispatcher().finished(this); } }
发起的一个同步的请求,如果重复调用会抛出异常。
captureCallStackTrace()是用来跟踪调用栈的信息的。
后面就是把这个Call 加入到client的调度器里面,进行相应的线程管理。
最后就是调用getResponseWithInterceptorChain,这个在第一节做过分析,就是经过一堆拦截器,然后得到结果。
enqueue()异步请求
@Override public void enqueue(Callback responseCallback) { synchronized (this) { if (executed) throw new IllegalStateException("Already Executed"); executed = true; } captureCallStackTrace(); client.dispatcher().enqueue(new AsyncCall(responseCallback)); }
发起异步请求。如果重复调用会抛出异常。
captureCallStackTrace()同样是用来跟踪调用栈的信息的。
接下来,会新建一个AsyncCall,这个AsyncCall其实是一个Runnable线程,简单过一下这个AsyncCall的代码:
final class AsyncCall extends NamedRunnable { // 回调 private final Callback responseCallback; 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; } //run的时候调用的方法 @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); } } }
在execute的调用中,其实和同步调用时一样的,只是这个是在异步线程中进行。
最后这个新建的AsyncCall ,同样会通过client 添加到dispatcher当中,但是值得注意的是,同步和异步,其实是添加到dispatcher不同的List当中,进行管理的。
getResponseWithInterceptorChain()
调用一系列的拦截器,得到最终的结果,在第一节中对此做过解析,可以参考第一节最后的部分。这里就不做过多的说明,具体每个拦截器的作用,在后面会做学习分析。
cancel()
调用里面比较简单,就是通过retryAndFollowUpInterceptor这个拦截器去对请求进行取消。对于正在执行的call 进行取消,但是对于已经是在读写数据阶段的请求无法取消,而且会抛出异常。
@Override public void cancel() { retryAndFollowUpInterceptor.cancel(); }
总结
Request和RealCall, 在发起请求阶段,是两个重要的类。Request主要是接收了调用者的数据并进行加工处理;RealCall主要提供方法,进行同步或者异步的调用请求,还有就是取消请求的方法。
对于okhttp的整体以及前期的初始化的一些类也有所了解,接下来就是对每个拦截器进行解剖学习了。
系列:
OKhttp源码学习(一)—— 基本请求流程
OKhttp源码学习(二)—— OkHttpClient
OKhttp源码学习(四)——RetryAndFollowUpInterceptor拦截器分析
OKhttp源码学习(五)—— BridgeInterceptor
OKhttp源码学习(六)—— CacheInterceptor拦截器
OKhttp源码学习(七)—— ConnectInterceptor拦截器
OKhttp源码学习(八)——CallServerInterceptor拦截器
OKhttp源码学习(九)—— 任务管理(Dispatcher)
原著是一个有趣的人,若有侵权,请通知删除
还没有人抢沙发呢~