/**
* Create the {@link Retrofit} instance using the configured values.
* <p>
* Note: If neither {@link #client} nor {@link #callFactory} is called a default {@link
* OkHttpClient} will be created and used.
*/
public Retrofit build() {
// 对baseUrl进行判空
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
// 网络请求执行器
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
// 初始化回掉方法执行器,主要用在异步请求
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
// 默认配置回掉方法
callbackExecutor = platform.defaultCallbackExecutor();
}
// Make a defensive copy of the adapters and add the default Call adapter.
List<CallAdapter.Factory> callAdapterFactories =
new ArrayList<>(this.callAdapterFactories);
callAdapterFactories.add(platform.defaultCallAdapterFactory(
callbackExecutor));
// Make a defensive copy of the converters.
List<Converter.Factory> converterFactories =
new ArrayList<>(1 + this.converterFactories.size());
// 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);
return new Retrofit(callFactory, baseUrl,
unmodifiableList(converterFactories),
unmodifiableList(callAdapterFactories), callbackExecutor,
validateEagerly);
}
Retrofit - 10 中build方法完成retrofit对象创建流程解析
最新推荐文章于 2022-02-10 18:02:51 发布