Retrofit buildRetrofit(OkHttpClient okHttpClient,Retrofit oldRetrofit) {
Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl(oldRetrofit.baseUrl())
.callbackExecutor(oldRetrofit.callbackExecutor())
.client(okHttpClient);
for (Converter.Factory factory : oldRetrofit.converterFactories()) {
retrofitBuilder.addConverterFactory(factory);
}
for (CallAdapter.Factory factory : oldRetrofit.callAdapterFactories()) {
retrofitBuilder.addCallAdapterFactory(factory);
}
return retrofitBuilder.build();
}
代码如上所示,情况是:我不能更改旧的okHttpClient或改造,因为它们在库中。okHttpClient的配置可以通过调用newBuilder来重用,我向其中添加了一个拦截器。重用oldRetrofit的配置,因此我在阅读Retrofit.java之后写了以上代码。请告诉我这是重用Retrofit的配置还是给我更好的配置的好方法。谢谢。