// Make a defensive copy of the adapters and add the default Call adapter. List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories); // 除了在 build 过程中添加的自定义 CallAdapter,还要添加系统默认的 Adapter 。 callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));
// 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. // 内置的 Converter 主要是对返回的 Response 进行再次处理 converterFactories.add(new BuiltInConverters()); // build 阶段自定义添加的 Converter converterFactories.addAll(this.converterFactories); // 如果支持 Java,会添加支持 Optional 操作的 Converter converterFactories.addAll(platform.defaultConverterFactories());
/** * Adapts a {@link Call} with response type {@code R} into the type of {@code T}. Instances are * created by {@linkplain Factory a factory} which is {@linkplain * Retrofit.Builder#addCallAdapterFactory(Factory) installed} into the {@link Retrofit} instance. */ publicinterfaceCallAdapter<R, T> { /** * Returns the value type that this adapter uses when converting the HTTP response body to a Java * object. For example, the response type for {@code Call<Repo>} is {@code Repo}. This type is * used to prepare the {@code call} passed to {@code #adapt}. * * <p>Note: This is typically not the same type as the {@code returnType} provided to this call * adapter's factory. */ Type responseType();
/** * Returns an instance of {@code T} which delegates to {@code call}. * * <p>For example, given an instance for a hypothetical utility, {@code Async}, this instance * would return a new {@code Async<R>} which invoked {@code call} when run. * * <pre><code> * @Override * public <R> Async<R> adapt(final Call<R> call) { * return Async.create(new Callable<Response<R>>() { * @Override * public Response<R> call() throws Exception { * return call.execute(); * } * }); * } * </code></pre> */ T adapt(Call<R> call);
/** * Creates {@link CallAdapter} instances based on the return type of {@linkplain * Retrofit#create(Class) the service interface} methods. */ abstractclassFactory{ /** * Returns a call adapter for interface methods that return {@code returnType}, or null if it * cannot be handled by this factory. */ publicabstract@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}. */ protectedstatic 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}. */ protectedstatic Class<?> getRawType(Type type) { return Utils.getRawType(type); } } }
/** * Convert objects to and from their representation in HTTP. Instances are created by {@linkplain * Factory a factory} which is {@linkplain Retrofit.Builder#addConverterFactory(Factory) installed} * into the {@link Retrofit} instance. */ publicinterfaceConverter<F, T> { @Nullable T convert(F value)throws IOException;
/** Creates {@link Converter} instances based on a type and target usage. */ abstractclassFactory{ /** * 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) { returnnull; }
/** * 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) { returnnull; }
/** * 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) { returnnull; }
/** * 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}. */ protectedstatic 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}. */ protectedstatic Class<?> getRawType(Type type) { return Utils.getRawType(type); } } }
fungo2() { val retrofit = Retrofit.Builder() .baseUrl(baseUrl) .build() val service = retrofit.create(ApiService::class.java) println("service is $service") println("service is ${service.javaClass.name}") val call = service.getAccountJson() }
我们通过 create 创建了 ApiService 的实例。那么 service 到底是什么呢?我们可以看一下
1 2
service is retrofit2.Retrofit$1@14d3bc22 service is com.sun.proxy.$Proxy0