Retrofit2源码分析

例子

从简单的例子开始分析Retrofit2是怎么和其他的库一起合作的,

下边是一个很简单的例子,是rxjava2 + retrofit2 + okhttp3 + gson混合使用,是访问淘宝的ip地址查询服务,返回信息输出到EditText里。

public static Retrofit getRetrofit() {
    if (retrofit == null) {
        synchronized (Retrofit.class) {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .client(getOkHttpClient())
                        .build();
            }
        }
    }
    return retrofit;
}
public interface IpServiceRx {
    @Headers({
            "Accept-Encoding: application/json",
            "User-Agent: wz"
    })
    @GET("getIpInfo.php")
    Observable<Response<IpModel>> getIpMsg(@Query("ip") String ip);
}
/**
 * rxjava2 + retrofit2 + okhttp3
 */
private void requestData3() {
    Retrofit retrofit = NetworkUtils.getRetrofit();

    IpServiceRx ipServiceRx = retrofit.create(IpServiceRx.class);
    String ip = "117.100.130.5";
    Observable<Response<IpModel>> ipMsg = ipServiceRx.getIpMsg(ip);
    ipMsg.throttleFirst(500, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Response<IpModel>>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {
                }

                @Override
                public void onNext(@NonNull Response<IpModel> ipModelResponse) {
                    IpModel ipModel = ipModelResponse.body();
                    if (ipModel == null) {
                        return;
                    }
                    IpData data = ipModel.getData();
                    if (data == null) {
                        return;
                    }
                    mEt.setText(getCSData(data));
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    mEt.setText(e.toString());
                    e.printStackTrace();
                }

                @Override
                public void onComplete() {
                }
            });
}

先从创建Retrofit时传递的几个factory看起

ConverterFactory

.addConverterFactory(GsonConverterFactory.create())

public Builder addConverterFactory(Converter.Factory factory) {
  converterFactories.add(checkNotNull(factory, "factory == null"));
  return this;
}

把转换器加入到了一个list中

public final class GsonConverterFactory extends Converter.Factory {
  /**
   * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  public static GsonConverterFactory create() {
    return create(new Gson());
  }

  /**
   * Create an instance using {@code gson} for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  @SuppressWarnings("ConstantConditions") // Guarding public API nullability.
  public static GsonConverterFactory create(Gson gson) {
    if (gson == null) throw new NullPointerException("gson == null");
    return new GsonConverterFactory(gson);
  }

  private final Gson gson;

  private GsonConverterFactory(Gson gson) {
    this.gson = gson;
  }

//返回解析okhttp3.ResponseBody的Converter实例
  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
      Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonResponseBodyConverter<>(gson, adapter);
  }

//返回解析okhttp3.RequsetBody的Converter实例
  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
      Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonRequestBodyConverter<>(gson, adapter);
  }
}
public interface Converter<F, T> {
  @Nullable T convert(F value) throws IOException;

  /** Creates {@link Converter} instances based on a type and target usage. */
  abstract class Factory {
    /**
     * Returns a {@link Converter} for converting an HTTP response body to {@code type}, or null if
     * {@code type} cannot be handled by this factory. This is used to create converters for
     * response types such as {@code SimpleResponse} from a {@code Call<SimpleResponse>}
     * declaration.
     */
    public @Nullable Converter<ResponseBody, ?> responseBodyConverter(Type type,
        Annotation[] annotations, Retrofit retrofit) {
      return null;
    }

    /**
     * Returns a {@link Converter} for converting {@code type} to an HTTP request body, or null if
     * {@code type} cannot be handled by this factory. This is used to create converters for types
     * specified by {@link Body @Body}, {@link Part @Part}, and {@link PartMap @PartMap}
     * values.
     */
    public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type,
        Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
      return null;
    }

    /**
     * Returns a {@link Converter} for converting {@code type} to a {@link String}, or null if
     * {@code type} cannot be handled by this factory. This is used to create converters for types
     * specified by {@link Field @Field}, {@link FieldMap @FieldMap} values,
     * {@link Header @Header}, {@link HeaderMap @HeaderMap}, {@link Path @Path},
     * {@link Query @Query}, and {@link QueryMap @QueryMap} values.
     */
    public @Nullable Converter<?, String> stringConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {
      return null;
    }

    /**
     * Extract the upper bound of the generic parameter at {@code index} from {@code type}. For
     * example, index 1 of {@code Map<String, ? extends Runnable>} returns {@code Runnable}.
     */
    protected static Type getParameterUpperBound(int index, ParameterizedType type) {
      return Utils.getParameterUpperBound(index, type);
    }

    /**
     * Extract the raw class type from {@code type}. For example, the type representing
     * {@code List<? extends Runnable>} returns {@code List.class}.
     */
    protected static Class<?> getRawType(Type type) {
      return Utils.getRawType(type);
    }
  }
}

CallAdapterFactory

.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
  callAdapterFactories.add(checkNotNull(factory, "factory == null"));
  return this;
}
public final class RxJava2CallAdapterFactory extends CallAdapter.Factory {
  /**
   * Returns an instance which creates synchronous observables that do not operate on any scheduler
   * by default.
   */
  public static RxJava2CallAdapterFactory create() {
    return new RxJava2CallAdapterFactory(null, false);
  }

  private final @Nullable Scheduler scheduler;
  private final boolean isAsync;

  private RxJava2CallAdapterFactory(@Nullable Scheduler scheduler, boolean isAsync) {
    this.scheduler = scheduler;
    this.isAsync = isAsync;
  }
  ...
}
public interface CallAdapter<R, T> {
  Type responseType();

  //注意这里的Call其实是Retrofit自己写的Call,并不是okhttp里的。
  T adapt(Call<R> call);

  /**
   * Creates {@link CallAdapter} instances based on the return type of {@linkplain
   * Retrofit#create(Class) the service interface} methods.
   */
  abstract class Factory {
    /**
     * Returns a call adapter for interface methods that return {@code returnType}, or null if it
     * cannot be handled by this factory.
     */
    public abstract @Nullable CallAdapter<?, ?> get(Type returnType, Annotation[] annotations,
        Retrofit retrofit);

    /**
     * Extract the upper bound of the generic parameter at {@code index} from {@code type}. For
     * example, index 1 of {@code Map<String, ? extends Runnable>} returns {@code Runnable}.
     */
    protected static Type getParameterUpperBound(int index, ParameterizedType type) {
      return Utils.getParameterUpperBound(index, type);
    }

    /**
     * Extract the raw class type from {@code type}. For example, the type representing
     * {@code List<? extends Runnable>} returns {@code List.class}.
     */
    protected static Class<?> getRawType(Type type) {
      return Utils.getRawType(type);
    }
  }
}

上边只是暂时列出来,后边会慢慢分析。

然后看下build()

public Retrofit build() {
// 没有设置时会自动创建一个OkHttpClient
  okhttp3.Call.Factory callFactory = this.callFactory;
  if (callFactory == null) {
    callFactory = new OkHttpClient();
  }

// platform是Android,defaultCallbackExecutor是主线程handler。
  Executor callbackExecutor = this.callbackExecutor;
  if (callbackExecutor == null) {
    callbackExecutor = platform.defaultCallbackExecutor();
  }

// 可以看到callAdapterFactories包含了我们设置的,还有platform自带的
  // Make a defensive copy of the adapters and add the default Call adapter.
  List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
  callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));

// 而converterFactories也是类似,包含了我们设置的,还有自带的几个。
  // Make a defensive copy of the converters.
  List<Converter.Factory> converterFactories = new ArrayList<>(
      1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());

  // Add the built-in converter factory first. This prevents overriding its behavior but also
  // ensures correct behavior when using converters that consume all types.
  converterFactories.add(new BuiltInConverters());
  converterFactories.addAll(this.converterFactories);
  converterFactories.addAll(platform.defaultConverterFactories());

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

接着看retrofit.create

IpServiceRx ipServiceRx = retrofit.create(IpServiceRx.class);
public <T> T create(final Class<T> service) {
  ...
  return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
      new InvocationHandler() {
// 此处platform是Android,抽象类Platform有两个继承类,一个叫Android,还有一个Java8。
        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);
          }

// jdk8引入的接口默认方法,不过由于Java8这个类实现了invokeDefaultMethod,而Android这个类没有实现此方法所以跳过
          if (platform.isDefaultMethod(method)) {
            return platform.invokeDefaultMethod(method, service, proxy, args);
          }

          return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
        }
      });
}

可以看到其实是使用了动态代理的方法,来把原类型创建出一个代理对象,

接着我们通过这个代理对象调用方法,

Observable<Response<IpModel>> ipMsg = ipServiceRx.getIpMsg(ip);
就会执行InvocationHandler.invoke方法,

invoke方法里,如果是object的方法则直接执行并返回,接着默认方法也跳过,

直接看loadServiceMethod

ServiceMethod<?> loadServiceMethod(Method method) {
  ServiceMethod<?> result = serviceMethodCache.get(method);
  if (result != null) return result;

  synchronized (serviceMethodCache) {
    result = serviceMethodCache.get(method);
    if (result == null) {
      result = ServiceMethod.parseAnnotations(this, method);
      serviceMethodCache.put(method, result);
    }
  }
  return result;
}

ServiceMethod

static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
// 这个类是用来把我们在方法上的注解和之后传递的参数生成一个okhttp的request,下边会用到。
  RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);

  Type returnType = method.getGenericReturnType();
  if (Utils.hasUnresolvableType(returnType)) {
    throw methodError(method,
        "Method return type must not include a type variable or wildcard: %s", returnType);
  }

// 返回类型不能时void
  if (returnType == void.class) {
    throw methodError(method, "Service methods cannot return void.");
  }

  return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
}

HttpServiceMethod

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) {
    ...
  } else {
// 方法的返回Type类型
    adapterType = method.getGenericReturnType();
  }

// 在下边进行分析
  CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method, adapterType, annotations);

// 校验返回类型是否正确,即Response<IpModel>
  Type responseType = callAdapter.responseType();
// 就是说返回类型不能时okhttp3.Response
  if (responseType == okhttp3.Response.class) {
    throw methodError(method, "‘"
        + getRawType(responseType).getName()
        + "‘ is not a valid response body type. Did you mean ResponseBody?");
  }
// 返回类型不能是Response,必须要包含泛型才行Response<String>,这个Response是retrofit2里定义的,不是okhttp3.Response
  if (responseType == Response.class) {
    throw methodError(method, "Response must include generic type (e.g., Response<String>)");
  }
  // TODO support Unit for Kotlin?
  if (requestFactory.httpMethod.equals("HEAD") && !Void.class.equals(responseType)) {
    throw methodError(method, "HEAD method must use Void as response type.");
  }

  // 在下边进行分析
  Converter<ResponseBody, ResponseT> responseConverter = createResponseConverter(retrofit, method, responseType);

// callFactory 其实就是OkHttpClient
  okhttp3.Call.Factory callFactory = retrofit.callFactory;
  if (!isKotlinSuspendFunction) {
    return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter);
  } else
...
  }
}

最后创建了一个CallAdapted对象返回,

CallAdapted继承关系:

CallAdapted<ResponseT, ReturnT> extends HttpServiceMethod<ResponseT, ReturnT>

HttpServiceMethod<ResponseT, ReturnT> extends ServiceMethod<ReturnT>

createCallAdapter

HttpServiceMethod.createCallAdapter

private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
    Retrofit retrofit, Method method, Type returnType, Annotation[] annotations) {
  try {
    //noinspection unchecked
    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.callAdapter

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++) {
    CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
    if (adapter != null) {
      return adapter;
    }
  }

  ...
  throw new IllegalArgumentException(builder.toString());
}

总的来说就是从我们之前设置的和自带的calladapterFactory中找到一个,调用get获取一个CallAdapter的就直接返回。

就用RxJava2CallAdapterFactory.get来说明:

@Override public @Nullable CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
// 我们的returnType是Observable<Response<IpModel>>的Type。
// 此方法返回Observable,具体看下边getRawType源码
  Class<?> rawType = getRawType(returnType);

// 显然下边都为false
  boolean isFlowable = rawType == Flowable.class;
  boolean isSingle = rawType == Single.class;
  boolean isMaybe = rawType == Maybe.class;
  if (rawType != Observable.class && !isFlowable && !isSingle && !isMaybe) {
    return null;
  }

  boolean isResult = false;
  boolean isBody = false;
  Type responseType;

// 返回泛型参数,即Response<IpModel>
  Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);

// 再次返回Response<IpModel>的RawType,即retrofit的Response
  Class<?> rawObservableType = getRawType(observableType);
  if (rawObservableType == Response.class) {
    // 再次返回Response<IpModel>的UpperBound,即IpModel
    responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
  } else if (rawObservableType == Result.class) {
    if (!(observableType instanceof ParameterizedType)) {
      throw new IllegalStateException("Result must be parameterized"
          + " as Result<Foo> or Result<? extends Foo>");
    }
    responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
    isResult = true;
  } else {
    responseType = observableType;
    isBody = true;
  }

// 由上边可知,传递进构造函数的Boolean都是false,创建RxJava2CallAdapterFactory时scheduler为null,isAsync为false,
// responseType为IpModel
  return new RxJava2CallAdapter(responseType, scheduler, isAsync, isResult, isBody, isFlowable, isSingle, isMaybe, false);
}

Utils.getRawType

static Class<?> getRawType(Type type) {
// 是具体类型
  if (type instanceof Class<?>) {
    // Type is a normal class.
    return (Class<?>) type;
  }

// 是带泛型的类型
  if (type instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) type;

    // 返回Observable
    Type rawType = parameterizedType.getRawType();
    if (!(rawType instanceof Class)) throw new IllegalArgumentException();
    return (Class<?>) rawType;
  }

// 其他类型
  ...

Utils.getParameterUpperBound

static Type getParameterUpperBound(int index, ParameterizedType type) {
  Type[] types = type.getActualTypeArguments();
  Type paramType = types[index];
  return paramType;
}

createResponseConverter

HttpServiceMethod.createResponseConverter

private static <ResponseT> Converter<ResponseBody, ResponseT> createResponseConverter(Retrofit retrofit, Method method, Type responseType) {
  Annotation[] annotations = method.getAnnotations();
  try {
    return retrofit.responseBodyConverter(responseType, annotations);
  } catch (RuntimeException e) { // Wide exception range because factories are user code.
    throw methodError(method, e, "Unable to create converter for %s", responseType);
  }
}

retrofit.responseBodyConverter

public <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotations) {
  return nextResponseBodyConverter(null, type, annotations);
}

public <T> Converter<ResponseBody, T> nextResponseBodyConverter(@Nullable Converter.Factory skipPast, Type type, Annotation[] annotations) {
  int start = converterFactories.indexOf(skipPast) + 1;
  for (int i = start, count = converterFactories.size(); i < count; i++) {
    Converter<ResponseBody, ?> converter = converterFactories.get(i).responseBodyConverter(type, annotations, this);
    if (converter != null) {
      //noinspection unchecked
      return (Converter<ResponseBody, T>) converter;
    }
  }

  ...
  throw new IllegalArgumentException(builder.toString());
}

总的来说就是从我们之前设置的和自带的converterFactory中找到一个,然后获取具体的responseBodyConverter。

就用GsonConverterFactory.responseBodyConverter来说明:

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
  TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
  return new GsonResponseBodyConverter<>(gson, adapter);
}

GsonResponseBodyConverter

GsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
  this.gson = gson;
  this.adapter = adapter;
}

@Override
public RequestBody convert(T value) throws IOException {
  Buffer buffer = new Buffer();
  Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
  JsonWriter jsonWriter = gson.newJsonWriter(writer);
  adapter.write(jsonWriter, value);
  jsonWriter.close();
  return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}

loadServiceMethod(method).invoke

一圈分析后在返回上边的retrofit.create内部分invoke的最后

loadServiceMethod(method).invoke(args != null ? args : emptyArgs);

由上边可知loadServiceMethod方法返回的是CallAdapted,

而CallAdapted继承关系:

CallAdapted<ResponseT, ReturnT> extends HttpServiceMethod<ResponseT, ReturnT>

HttpServiceMethod<ResponseT, ReturnT> extends ServiceMethod<ReturnT>

调用invoke是调用到的HttpServiceMethod.invoke

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

注意此处的call都是retrofit的,不是okhttp的。

在其中创建了个OkHttpCall对象,顾名思义,里边肯定就是通过okhttp的call进行网络请求的,绕了一大圈终于找到实际请求的地方了。

接着看adapt

adapt实际调用的是CallAdapted.adapt

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

此处的callAdapter其实就是上边的RxJava2CallAdapter,

所以就去RxJava2CallAdapter中看看

@Override
public Object adapt(Call<R> call) {
  Observable<Response<R>> responseObservable = isAsync
      ? new CallEnqueueObservable<>(call)
      : new CallExecuteObservable<>(call);

  Observable<?> observable;
  if (isResult) {
    observable = new ResultObservable<>(responseObservable);
  } else if (isBody) {
    observable = new BodyObservable<>(responseObservable);
  } else {
    observable = responseObservable;
  }

  if (scheduler != null) {
    observable = observable.subscribeOn(scheduler);
  }

  if (isFlowable) {
    return observable.toFlowable(BackpressureStrategy.LATEST);
  }
  if (isSingle) {
    return observable.singleOrError();
  }
  if (isMaybe) {
    return observable.singleElement();
  }
  if (isCompletable) {
    return observable.ignoreElements();
  }
  return RxJavaPlugins.onAssembly(observable);
}

由上可知

  • isAsync,isResult,isBody为false,
  • scheduler = null
  • isFlowable,isSingle,isMaybe,isCompletable都为false

所以说最终返回就是new CallExecuteObservable<>(call);

而RxJavaPlugins.onAssembly(observable);中

public static <T> Observable<T> onAssembly(@NonNull Observable<T> source) {
    Function<? super Observable, ? extends Observable> f = onObservableAssembly;
    if (f != null) {
        return apply(f, source);
    }
    return source;
}

我们并没有对rxjava设置hook,所以返回的还是CallExecuteObservable,

CallExecuteObservable创建时传递的call就是OkHttpCall。

接着就是rxjava操作了

这里顺带把rxjava的一些源码也简单分析了。

    Observable<Response<IpModel>> ipMsg = ipServiceRx.getIpMsg(ip);
    ipMsg.throttleFirst(500, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Response<IpModel>>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {
                }

                @Override
                public void onNext(@NonNull Response<IpModel> ipModelResponse) {
                    IpModel ipModel = ipModelResponse.body();
                    if (ipModel == null) {
                        return;
                    }
                    IpData data = ipModel.getData();
                    if (data == null) {
                        return;
                    }
                    mEt.setText(getCSData(data));
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    mEt.setText(e.toString());
                    e.printStackTrace();
                }

                @Override
                public void onComplete() {
                }
            });

rxjava每次调用一个转换操作,都会返回一个不同的observable,这个observable会记录上层的observable,从而形成一个从上到下的链,所以也叫链式操作。

直到最后调用subscribe,此时会触发向上订阅,即下层都会调用上层的subscribe,当然每层observable都有不同的subscribeActual实现,所以每层其实是上层的observer,同时又是下层的observable。

直到调用到顶层层的subscribeActual,即本例中的CallExecuteObservable的subscribeActual:

@Override protected void subscribeActual(Observer<? super Response<T>> observer) {
  // Since Call is a one-shot type, clone it for each new observer.
// 就是OkHttpCall
  Call<T> call = originalCall.clone();
  CallDisposable disposable = new CallDisposable(call);
  observer.onSubscribe(disposable);
  if (disposable.isDisposed()) {
    return;
  }

  boolean terminated = false;
  try {
// 此处会去调用OkHttpCall的execute,里边肯定就是okhttp的call.execute
    Response<T> response = call.execute();
    if (!disposable.isDisposed()) {
// 开始往下层传递消息
      observer.onNext(response);
    }
    if (!disposable.isDisposed()) {
      terminated = true;
      observer.onComplete();
    }
  } catch (Throwable t) {
    ...
  }
}

OkHttpCall.execute

@Override
public Response<T> execute() throws IOException {
  okhttp3.Call call;

  synchronized (this) {
//正确性检查
    ...

    call = rawCall;
    if (call == null) {
      try {
// 创建一个新的网络请求,看下边代码
        call = rawCall = createRawCall();
      } catch (IOException | RuntimeException | Error e) {
        throwIfFatal(e); //  Do not assign a fatal error to creationFailure.
        creationFailure = e;
        throw e;
      }
    }
  }

  if (canceled) {
    call.cancel();
  }

// 解析 阻塞式call.execute() 返回的okhttp3.Response,看下边代码
  return parseResponse(call.execute());
}
private okhttp3.Call createRawCall() throws IOException {
  okhttp3.Call call = callFactory.newCall(requestFactory.create(args));
  return call;
}

此处的callFactory就是上边ServiceMethod.parseAnnotations中创建的RequestFactory,通过RequestFactory构建出来一个okhttp的request对象,

最后生成一个okhttp3.Call返回。

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) {
    rawBody.close();
    return Response.success(null, rawResponse);
  }

  ExceptionCatchingResponseBody catchingBody = new ExceptionCatchingResponseBody(rawBody);
  try {
// 此处会用我们之前设置的Converter(即GsonResponseBodyConverter)来解析出具体的bean对象,
    T body = responseConverter.convert(catchingBody);
    return Response.success(body, rawResponse);
  } catch (RuntimeException e) {
    // If the underlying source threw an exception, propagate that rather than indicating it was
    // a runtime exception.
    catchingBody.throwIfCaught();
    throw e;
  }
}

GsonResponseBodyConverter.convert

@Override public T convert(ResponseBody value) throws IOException {
  JsonReader jsonReader = gson.newJsonReader(value.charStream());
  try {
    T result = adapter.read(jsonReader);
    if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
      throw new JsonIOException("JSON document was not fully consumed.");
    }
    return result;
  } finally {
    value.close();
  }

observer.onNext(response);

向下传递,此时还是subscribeOn(Schedulers.io())指定的线程上操作的,

当传递到observeOn(AndroidSchedulers.mainThread())时,此observable会把线程转换成mainThread,

最后传递到subscribe传递的observer的onNext中

其他

返回值中带不带Response逻辑有什么区别

Observable<Response<IpModel>> getIpMsg(@Query("ip") String ip);

上边的分析都是基于带Response的,

那如果定义接口时不带呢,即

Observable<IpModel> getIpMsg(@Query("ip") String ip);

那么接着上边的createCallAdapter分析里的RxJava2CallAdapterFactory.get来说明:

@Override public @Nullable CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
// 我们的returnType是Observable<IpModel>的Type。
// 此方法返回Observable,具体看下边getRawType源码
  Class<?> rawType = getRawType(returnType);

// 显然下边都为false
  boolean isFlowable = rawType == Flowable.class;
  boolean isSingle = rawType == Single.class;
  boolean isMaybe = rawType == Maybe.class;
  if (rawType != Observable.class && !isFlowable && !isSingle && !isMaybe) {
    return null;
  }

  boolean isResult = false;
  boolean isBody = false;
  Type responseType;

// 返回泛型参数,即IpModel
  Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);

// 还是IpModel
  Class<?> rawObservableType = getRawType(observableType);
  if (rawObservableType == Response.class) {
    responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
  } else if (rawObservableType == Result.class) {
    if (!(observableType instanceof ParameterizedType)) {
      throw new IllegalStateException("Result must be parameterized"
          + " as Result<Foo> or Result<? extends Foo>");
    }
    responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
    isResult = true;
  } else {
// 此时会进入此逻辑,isBody为true了
    responseType = observableType;
    isBody = true;
  }

// 由上边可知,传递进构造函数的Boolean除了isBody为true,其他都是false,创建RxJava2CallAdapterFactory时scheduler为null,isAsync为false,
// responseType为IpModel
  return new RxJava2CallAdapter(responseType, scheduler, isAsync, isResult, isBody, isFlowable, isSingle, isMaybe, false);
}

然后接着loadServiceMethod(method).invoke里

RxJava2CallAdapter.adapt

@Override
public Object adapt(Call<R> call) {
  Observable<Response<R>> responseObservable = isAsync
      ? new CallEnqueueObservable<>(call)
      : new CallExecuteObservable<>(call);

  Observable<?> observable;
  if (isResult) {
    observable = new ResultObservable<>(responseObservable);
  } else if (isBody) {
    observable = new BodyObservable<>(responseObservable);
  } else {
    observable = responseObservable;
  }

  if (scheduler != null) {
    observable = observable.subscribeOn(scheduler);
  }

  if (isFlowable) {
    return observable.toFlowable(BackpressureStrategy.LATEST);
  }
  if (isSingle) {
    return observable.singleOrError();
  }
  if (isMaybe) {
    return observable.singleElement();
  }
  if (isCompletable) {
    return observable.ignoreElements();
  }
  return RxJavaPlugins.onAssembly(observable);
}

由上可知

  • isAsync,isResult,
  • isBody为true,
  • scheduler = null,
  • isFlowable,isSingle,isMaybe,isCompletable都为false

所以说最终返回就是new BodyObservable<>(responseObservable);

BodyObservable(Observable<Response<T>> upstream) {
  this.upstream = upstream;
}

@Override protected void subscribeActual(Observer<? super T> observer) {
  upstream.subscribe(new BodyObserver<T>(observer));
}

就是说最上层是responseObservable,

那么当responseObservable开始下传数据时,会调用BodyObserver的onNext:

@Override
public void onNext(Response<R> response) {
  if (response.isSuccessful()) {
// 会把body直接传递到下层,即IpModal
    observer.onNext(response.body());
  } else {
    terminated = true;
    Throwable t = new HttpException(response);
    try {
      observer.onError(t);
    } catch (Throwable inner) {
      Exceptions.throwIfFatal(inner);
      RxJavaPlugins.onError(new CompositeException(t, inner));
    }
  }
}

此处的response是retrofit的,

response会携带更多的此次网络请求的信息,如果只返回实际的bean/modal对象,那么就不能够有更多控制。

原文地址:https://www.cnblogs.com/muouren/p/11768914.html

时间: 2024-10-06 14:41:01

Retrofit2源码分析的相关文章

Retrofit2源码分析(一)

本文将顺着构建请求对象→构建请求接口→发起同步/异步请求的流程,分析retrofit2是如何实现的. 组成部分 Retrofit2源码主要分为以下几个部分: retrofit retrofit-adapters retrofit-converters 本篇先分析retrofit部分,也就是retrofit源码的主干部分. 下面顺着retrofit2的使用流程进行分析. Retrofit对象的构建 首先我们看看构建一个Retrofit对象,都需要或者可选哪些配置: 1234567891011121

Retrofit2源码解读

综述 Retrofit2的用法在Retrofit2.0使用详解这篇文章中已经详细介绍过了.那么在这就来看一下Retrofit2它是如何实现的.Retrofit2中它的内部网络请求是依赖于OKHttp,所以Retrofit2可以看做是对OKHttp的一次封装,那么下面就开看下Retrofit2是如何对OKHttp进行封装的. 回顾Retrofit2的使用 在这里首先来回顾一下Retrofit2的使用.对于Retrofit2的使用可以分为三步. 首先,我们创建一个Java接口GitHubServic

Android Retrofit源码分析(一边用一边侃)

这几天空余时间总是想着写点什么,所以紧跟着之前android盒子模型FlexBoxLayout之后有写下了这篇Retrofit源码分析使用篇. 前言: Retrofit由于简单与出色的性能,使其安卓上最流行的HTTP Client库之一. Android Studio开发相关配置如下: compile "com.squareup.retrofit2:retrofit:2.0.2" compile "com.squareup.retrofit2:converter-gson:2

Retrofit2源码解析

最近项目将网络框架换成Retrofit2.0.2,文中说的Retrofit都是指的Retrofit2这里要说明一下,毕竟和Retrofit1差别还是蛮大的,结合Okhttp,RxJava还是比较好用的,网上有很多前辈介绍过使用方法,本文是想研究一下Retrofit的源码.关于Retrofit的介绍可以查阅Retrofit的官方网站 直接进入主题:(注本文是结合RxJava介绍的,最好可以了解一下RxJava不了解也没有关系,大部分的思想是一样的) Retrofit的基本使用 Retrofit使用

Retrofit源码分析以及MVP框架封装使用

阅读此文前请先阅读Retrofit+okhttp网络框架介绍 从上文中我们已经了解通过如下代码即可得到返回给我们call 以及 response对象,今天我们通过源码来分析这个过程是如何实现的. /** * 获取天气数据 * @param cityname * @param key * @return */ @GET("/weather/index") Call<WeatherData> getWeatherData(@Query("format") S

Retrofit2 源码解析

原文链接:http://bxbxbai.github.io/2015/12/13/retrofit2-analysis/ 公司里最近做的项目中网络框架用的就是Retrofit,用的多了以后觉得这个框架真的非常好用,然后抽了点时间debug了一下源码,觉得不光代码写的非常好,而且设计这个框架的思路都非常特别,收获很多,决定记录下来 本文的源码分析基于Retrofit 2.0,和Retrofit 1.0有较大的不同, 本文主要分为几部分:0.Retrofit 是什么,1.Retrofit 怎么用,2

TeamTalk源码分析之login_server

login_server是TeamTalk的登录服务器,负责分配一个负载较小的MsgServer给客户端使用,按照新版TeamTalk完整部署教程来配置的话,login_server的服务端口就是8080,客户端登录服务器地址配置如下(这里是win版本客户端): 1.login_server启动流程 login_server的启动是从login_server.cpp中的main函数开始的,login_server.cpp所在工程路径为server\src\login_server.下表是logi

Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)

1 背景 还记得前一篇<Android触摸屏事件派发机制详解与源码分析一(View篇)>中关于透过源码继续进阶实例验证模块中存在的点击Button却触发了LinearLayout的事件疑惑吗?当时说了,在那一篇咱们只讨论View的触摸事件派发机制,这个疑惑留在了这一篇解释,也就是ViewGroup的事件派发机制. PS:阅读本篇前建议先查看前一篇<Android触摸屏事件派发机制详解与源码分析一(View篇)>,这一篇承接上一篇. 关于View与ViewGroup的区别在前一篇的A

HashMap与TreeMap源码分析

1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Java这么久,也写过一些小项目,也使用过TreeMap无数次,但到现在才明白它的实现原理).因此本着"不要重复造轮子"的思想,就用这篇博客来记录分析TreeMap源码的过程,也顺便瞅一瞅HashMap. 2. 继承结构 (1) 继承结构 下面是HashMap与TreeMap的继承结构: pu