目录
系列文章:
Retrofit源码学习五:Retrofit中同步、异步请求解析
Retrofit同步请求和异步请求的区别:其实,这两个方法大体相同。唯一的不同在,异步请求会将回调方法交给回调执行器Executor,然后由Executor指定不同的线程去完成不同的操作。
我们先来总结一下同步方法的步骤:
同步请求:OkHttpCall.execute()
一、用ParameterHandler对网络请求接口中的方法、参数进行解析
二、用ServiceMethod创建一个Okhttp的Request对象
三、通过okhttp发送网络请求
四、用数据解析器converter解析服务器返回的数据(Retrofit中默认用的是GsonConverterFactory)
如下图所示:
我们看具体的代码:
//class OkHttpCall开始
final class OkHttpCall<T> implements Call<T> {
private okhttp3.Call rawCall;
@Override
public Response<T> execute() throws IOException {
okhttp3.Call call;
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed.");
executed = true;
if (creationFailure != null) {