Retrofit源码分析-基于2.6.0版本

Retrofit的基本使用

此文章分析的Retrofit是基于Retrofit2.6.0版本。Gradle依赖如下:

implementation 'com.squareup.retrofit2:retrofit:2.6.0'

Retrofit使用步骤

//创建Retrofit对象
val retrofit = Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())////设置数据解析器为GSON
    //.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//返回的结果通过RxJava处理
    .baseUrl("http://xxx")
    .build()
//2.创建网络接口实例
val apiService = retrofit.create(ApiService::class.java)
//3.进行网络请求,得到数据
apiService.getHomeData()

创建Retrofit对象

我们可以看到创建Retrofit对象是通过Build构建者模式来创建的,我们看下源码

    Builder(Platform platform) {
      this.platform = platform;
    }

    public Builder() {
      this(Platform.get());
    }

Builder需要传入一个Platfrom,我们接下来看下Platform

class Platform {
  private static final Platform PLATFORM = findPlatform();

  static Platform get() {
    return PLATFORM;
  }

  private static Platform findPlatform() {
    try {
      Class.forName("android.os.Build");
      if (Build.VERSION.SDK_INT != 0) {
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    try {
      Class.forName("java.util.Optional");
      return new Java8();
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform();
  }
  ...
  static class Android extends Platform {
    ...
  }
  static class Java8 extends Platform {
    ...
  }
}

我们可以看到Platform的findPlatform方法会帮忙匹配到对应的平台,目前支持的平台有Android和Java8。
Platform中的defaultCallbackExecutor提供了我们异步回调的线程切换。我们看Android平台的Platform。

  static class Android extends Platform {
    //因为Java8中接口中新增了默认方法,这个是健壮性判断
    @Override boolean isDefaultMethod(Method method) {
      if (Build.VERSION.SDK_INT < 24) {
        return false;
      }
      return method.isDefault();
    }
    //返回的Executor实际是运行在主线程的,后面收到返回结果用于线程切换
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }

    @Override List<? extends CallAdapter.Factory> defaultCallAdapterFactories(
        @Nullable Executor callbackExecutor) {
      if (callbackExecutor == null) throw new AssertionError();
      DefaultCallAdapterFactory executorFactory = new DefaultCallAdapterFactory(callbackExecutor);
      return Build.VERSION.SDK_INT >= 24
        ? asList(CompletableFutureCallAdapterFactory.INSTANCE, executorFactory)
        : singletonList(executorFactory);
    }

    @Override int defaultCallAdapterFactoriesSize() {
      return Build.VERSION.SDK_INT >= 24 ? 2 : 1;
    }

    @Override List<? extends Converter.Factory> defaultConverterFactories() {
      return Build.VERSION.SDK_INT >= 24
          ? singletonList(OptionalConverterFactory.INSTANCE)
          : Collections.<Converter.Factory>emptyList();
    }

    @Override int defaultConverterFactoriesSize() {
      return Build.VERSION.SDK_INT >= 24 ? 1 : 0;
    }
    //封装的Executor用于线程线程切换
    static class MainThreadExecutor implements Executor {
      private final Handler handler = new Handler(Looper.getMainLooper());//获得主线程的Looper

      @Override public void execute(Runnable r) {
        handler.post(r);//Runnable将在主线程运行
      }
    }
  }

我们继续分析build方法

public Retrofit build() {
    //验证参数的合法性,如果我们没有传baseUrl则会抛出异常,baseUrl是必传字段
    if (baseUrl == null) {
      throw new IllegalStateException("Base URL required.");
    }
    //Retrofit是对okHttp的封装,真正的网络请求还是OkHttp
    okhttp3.Call.Factory callFactory = this.callFactory;
    if (callFactory == null) {
      callFactory = new OkHttpClient();//如果我们没有传入OkHttp,则new出来
    }
    //用于对Respone结果处理的Executor,主要作用是线程切换
    Executor callbackExecutor = this.callbackExecutor;
    if (callbackExecutor == null) {
      //callbackExecutor我们没有传入的则采用Platform中提供的Executor,在Android平台其实就是MainThreadExecutor
      callbackExecutor = platform.defaultCallbackExecutor();
    }
    //添加callAdapterFactories到集合中,先添加我们传入的,再添加默认的defaultCallAdapterFactories
    List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
    callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));
    //添加converterFactories,先添加内置的,再添加我们传入的,最后添加默认的
    List<Converter.Factory> converterFactories = new ArrayList<>(
        1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());
    converterFactories.add(new BuiltInConverters());
    converterFactories.addAll(this.converterFactories);
    converterFactories.addAll(platform.defaultConverterFactories());

    return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
        unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
}

创建网络接口实例

  public <T> T create(final Class<T> service) {
    //验证接口的合法性
    Utils.validateServiceInterface(service);
    //提前检测方法的合法性
    if (validateEagerly) {
      eagerlyValidateMethods(service);
    }
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
        new InvocationHandler() {
          private final Platform platform = Platform.get();
          private final Object[] emptyArgs = new Object[0];

          @Override public @Nullable Object invoke(Object proxy, Method method,
              @Nullable Object[] args) throws Throwable {
            // 如果是Object的方法则直接调用不做处理
            if (method.getDeclaringClass() == Object.class) {
              return method.invoke(this, args);
            }
            //如果是java8中接口的默认方法,则直接调用不做处理
            if (platform.isDefaultMethod(method)) {
              return platform.invokeDefaultMethod(method, service, proxy, args);
            }
            return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
          }
        });
  }
  //验证接口的合法性,此接口不允许继承其他的接口
  static <T> void validateServiceInterface(Class<T> service) {
    if (!service.isInterface()) {
      throw new IllegalArgumentException("API declarations must be interfaces.");
    }
    if (service.getInterfaces().length > 0) {
      throw new IllegalArgumentException("API interfaces must not extend other interfaces.");
    }
  }

create的核心是通过动态代理返回一个接口的实例,这段代码的核心是loadServiceMethod(method),下面我们看下这个代码:

  ServiceMethod<?> loadServiceMethod(Method method) {
    //如果缓存中有则直接取缓存,否则创建
    ServiceMethod<?> result = serviceMethodCache.get(method);
    if (result != null) return result;
    //加锁
    synchronized (serviceMethodCache) {
      result = serviceMethodCache.get(method);
      if (result == null) {
        //根据method得到对应的ServiceMethod,并加入缓存
        result = ServiceMethod.parseAnnotations(this, method);
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

ServiceMethod是缓存到了一个Map中,首先会通过Map获取其实例,如果Map中有其实例,就直接返回,否者创建一个缓存到Map中,然后返回
我们可以看到创建代码ServiceMethod的核心是ServiceMethod#parseAnnotations,我们继续分析这个方法

  static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
    RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);
    ...
    return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
  }

最后调用的是HttpServiceMethod#parseAnnotations

  static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
      Retrofit retrofit, Method method, RequestFactory requestFactory) {
    boolean isKotlinSuspendFunction = requestFactory.isKotlinSuspendFunction;
    boolean continuationWantsResponse = false;
    boolean continuationBodyNullable = false;

    Annotation[] annotations = method.getAnnotations();
    Type adapterType;
    if (isKotlinSuspendFunction) {
      Type[] parameterTypes = method.getGenericParameterTypes();
      Type responseType = Utils.getParameterLowerBound(0,(ParameterizedType) parameterTypes[parameterTypes.length - 1]);
      if (getRawType(responseType) == Response.class && responseType instanceof ParameterizedType) {
        responseType = Utils.getParameterUpperBound(0, (ParameterizedType) responseType);
        continuationWantsResponse = true;
      } else {
      }
      adapterType = new Utils.ParameterizedTypeImpl(null, Call.class, responseType);
      annotations = SkipCallbackExecutorImpl.ensurePresent(annotations);
    } else {
      adapterType = method.getGenericReturnType();
    }
    //从callAdapterFactories中找到合适的CallAdapter
    CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method, adapterType, annotations);
    Type responseType = callAdapter.responseType();
    //验证responseType的合法性
    if (responseType == okhttp3.Response.class) {
      throw methodError(method, "'"
          + getRawType(responseType).getName()
          + "' is not a valid response body type. Did you mean ResponseBody?");
    }
    //验证responseType的合法性
    if (responseType == Response.class) {
      throw methodError(method, "Response must include generic type (e.g., Response<String>)");
    }
    //验证请求合法性
    if (requestFactory.httpMethod.equals("HEAD") && !Void.class.equals(responseType)) {
      throw methodError(method, "HEAD method must use Void as response type.");
    }
    //从converterFactories中找到合适Converter
    Converter<ResponseBody, ResponseT> responseConverter = createResponseConverter(retrofit, method, responseType);
    okhttp3.Call.Factory callFactory = retrofit.callFactory;
    if (!isKotlinSuspendFunction) {
      return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter);
    } else if (continuationWantsResponse) {
      return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForResponse<>(requestFactory,
          callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter);
    } else {
      return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForBody<>(requestFactory,
          callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter,
          continuationBodyNullable);
    }
  }

Retrofit2.6.0版本增加了对协程的处理,支持了协程。我们暂时先不分析这部门,如果不是协程,则返回的是CallAdapted,我们再去看这个

CallAdapted(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
    Converter<ResponseBody, ResponseT> responseConverter,
    CallAdapter<ResponseT, ReturnT> callAdapter) {
  super(requestFactory, callFactory, responseConverter);
  this.callAdapter = callAdapter;
}
//保存变量
HttpServiceMethod(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
    Converter<ResponseBody, ResponseT> responseConverter) {
  this.requestFactory = requestFactory;
  this.callFactory = callFactory;
  this.responseConverter = responseConverter;
}

保存requestFactory、callFactory和responseConverter,得到了ServiceMethod
下面我们分析下createCallAdapter

private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
    Retrofit retrofit, Method method, Type returnType, Annotation[] annotations) {
  try {
    return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);
  } catch (RuntimeException e) { // Wide exception range because factories are user code.
    throw methodError(method, e, "Unable to create call adapter for %s", returnType);
  }
}

Retrofit.java

public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
  return nextCallAdapter(null, returnType, annotations);
}
  public CallAdapter<?, ?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, Type returnType,
      Annotation[] annotations) {
    ...省略代码
    int start = callAdapterFactories.indexOf(skipPast) + 1;
    for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
    //根据returnType、annotations和Retrofit从callAdapterFactories找到匹配的CallAdapter
      CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }
    ...省略代码
  }

createResponseConverter的流程与上面类似,这里就不作分析了。

进行网络请求,得到数据

调用ServiceMethod的invoke,我们看他的实现类HttpServiceMethod的invoke

@Override final @Nullable ReturnT invoke(Object[] args) {
  Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
  return adapt(call, args);
}

首先创建了OkHttpCall,根据保存的ServiceMethod创建对应的OkHttpCall。

OkHttpCall(RequestFactory requestFactory, Object[] args,
    okhttp3.Call.Factory callFactory, Converter<ResponseBody, T> responseConverter) {
  this.requestFactory = requestFactory;
  this.args = args;
  this.callFactory = callFactory;//这里的callFactory是Factory callFactory = retrofit.callFactory;也就是Retrofit中的callFactory
  this.responseConverter = responseConverter;
}

然后调用adapt,我们还看实现类,这里我们看CallAdapted

@Override protected ReturnT adapt(Call<ResponseT> call, Object[] args) {
  return callAdapter.adapt(call);
}

在看callAdapter的adapt调用的是DefaultCallAdapterFactory#get返回的匿名类CallAdapter中adapt

final Executor executor = Utils.isAnnotationPresent(annotations, SkipCallbackExecutor.class)? null: callbackExecutor;
return new CallAdapter<Object, Call<?>>() {
  @Override public Type responseType() {
    return responseType;
  }
  @Override public Call<Object> adapt(Call<Object> call) {
    return executor == null? call: new ExecutorCallbackCall<>(executor, call);//这里面的executor是ExecutorCallbackCall
  }
};

最终得到的实现类是ExecutorCallbackCall。当我们调用enqueue之后,会先调用OkHttpCall的enqueue,OkHttpCall的enqueue本质是调用OkHttp的Call,然后把返回的结果传递到ExecutorCallbackCall中,通过callbackExecutor进行转换,这里面callbackExecutor也是我们上面分析的defaultCallbackExecutor(运行在主线程中)。这样就把结果回调到主线程中了。

@Override public void enqueue(final Callback<T> callback) {
  checkNotNull(callback, "callback == null");
  delegate.enqueue(new Callback<T>() {
    @Override public void onResponse(Call<T> call, final Response<T> response) {
      callbackExecutor.execute(new Runnable() {//进行线程切换
        @Override public void run() {
          if (delegate.isCanceled()) {
            // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
            callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
          } else {
            callback.onResponse(ExecutorCallbackCall.this, response);
          }
        }
      });
    }

    @Override public void onFailure(Call<T> call, final Throwable t) {
      callbackExecutor.execute(new Runnable() {//进行线程切换
        @Override public void run() {
          callback.onFailure(ExecutorCallbackCall.this, t);
        }
      });
    }
  });
}

当我们调用execute的时候,不做处理,最终直接调用OkHttp的execute。

@Override public boolean isExecuted() {
  return delegate.isExecuted();
}

数据解析

我们通过OkHttp得到的数据并不是我们想要的Bean,Retrofit是通过parseResponse对数据进行解析

  Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
    ResponseBody rawBody = rawResponse.body();

    // Remove the body's source (the only stateful object) so we can pass the response along.
    rawResponse = rawResponse.newBuilder()
        .body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength()))
        .build();

    int code = rawResponse.code();
    if (code < 200 || code >= 300) {//排除错误的请求
      try {
        // Buffer the entire body to avoid future I/O.
        ResponseBody bufferedBody = Utils.buffer(rawBody);
        return Response.error(bufferedBody, rawResponse);
      } finally {
        rawBody.close();
      }
    }
    if (code == 204 || code == 205) {//如果是204 205直接返回原始数据
      rawBody.close();
      return Response.success(null, rawResponse);
    }
    ExceptionCatchingResponseBody catchingBody = new ExceptionCatchingResponseBody(rawBody);//封装了异常捕获
    try {
      T body = responseConverter.convert(catchingBody);//进行数据解析
      return Response.success(body, rawResponse);
    } catch (RuntimeException e) {
      catchingBody.throwIfCaught();
      throw e;
    }
  }

总结

Retrofit中运用了大量的设计模式,其本质是对OkHttp的一层封装,这篇文章简单的Retrofit的基本流程进行了分析,具体细节还需要看代码分析。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,444评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,421评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,363评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,460评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,502评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,511评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,280评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,736评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,014评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,190评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,848评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,531评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,159评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,411评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,067评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,078评论 2 352

推荐阅读更多精彩内容

  • 前言 注解式的框架非常火,注解以其轻量,简洁等特性被人们所喜爱者,关键是它解藕。网络请求的框架非常多,比较受欢迎的...
    萨达哈鲁酱阅读 579评论 0 5
  • 适配器模式上一篇文章我们已经分析了Retrofit解析注解封装进ServiceMethod的流程,读者在这里要记住...
    andcoder阅读 648评论 0 2
  • Retrofit 2 源码解析 关于Retrofit 2的使用请看上一篇https://www.jianshu.c...
    gogoingmonkey阅读 516评论 0 1
  • 一,前言 Retrofit其实是将OKhttp封装起来,和volley一样。那解析Retrofit其实就是解析它如...
    求闲居士阅读 1,327评论 2 5
  • 我想带你去看大海 那个很遥远很蓝的海 还有星罗棋布的岛屿 就在那儿 就在公路的尽头 在山的那端 被繁盛的森林掩盖 ...
    简钟阅读 204评论 0 1