Mik*_*ail 7 java android dependency-injection dagger-2 retrofit2
就我而言,我需要动态更改 URL,但我不想创建改造客户端的 2 个实例。我正在尝试通过拦截器修改来更改基本 URL,但改造仍然使用旧值。我究竟做错了什么?
应用程序.java
public class App extends Application {
private static AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent =
DaggerAppComponent
.builder()
.appModule(new AppModule(this))
.build();
}
@NonNull
public static App get(@NonNull Context context) {
return (App) context.getApplicationContext();
}
public static AppComponent getAppComponent() {
return appComponent;
}
}
Run Code Online (Sandbox Code Playgroud)
应用模块.java
@Module
public class AppModule {
private Context context;
public AppModule(Context context) {
this.context = context;
}
@Provides
@Singleton
Context provideContext() {
return context;
}
}
Run Code Online (Sandbox Code Playgroud)
网络模块.java
@Module
public class NetModule {
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
MainInterceptor provideMyApiInterceptor() {
return MainInterceptor.get();
}
@Provides
@Singleton
OkHttpClient provideOkhttpClient() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(15, TimeUnit.SECONDS);
client.connectTimeout(20, TimeUnit.SECONDS);
return client.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("first.url.com")
.client(okHttpClient)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
网络组件
@Singleton
@Component(modules = NetModule.class)
public interface NetComponent {
Retrofit getRetrofit();
MainInterceptor getInterceptor();
}
Run Code Online (Sandbox Code Playgroud)
MainInterceptor.class
public class MainInterceptor implements Interceptor {
private static MainInterceptor sInterceptor;
private String mScheme;
private String mHost;
public static MainInterceptor get() {
if (sInterceptor == null) {
sInterceptor = new MainInterceptor();
}
return sInterceptor;
}
private MainInterceptor() {
}
public void setInterceptor(String url) {
HttpUrl httpUrl = HttpUrl.parse(url);
mScheme = httpUrl.scheme();
mHost = httpUrl.host();
}
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
if (mScheme != null && mHost != null) {
HttpUrl newUrl = original.url().newBuilder()
.scheme(mScheme)
.host(mHost)
.build();
original = original.newBuilder()
.url(newUrl)
.build();
}
return chain.proceed(original);
}
}
Run Code Online (Sandbox Code Playgroud)
这是在某个类中初始化组件的代码。
NetComponent component= DaggerNetComponent.create();
DataService service = component.getRetrofit().create(DataService.class);
MainInterceptor interceptor = component.getInterceptor();
interceptor.setInterceptor("second.url.com");
service.getSomeData();
Run Code Online (Sandbox Code Playgroud)
之后,网址仍然是“first.url.com”
简单的方法是使用@Named:
@Provides
@Singleton
@Named("retrofit_1")
Retrofit provideRetrofit1(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
...
.baseUrl("url_1.com")
...;}
@Provides
@Singleton
@Named("retrofit_2")
Retrofit provideRetrofit2(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
...
.baseUrl("url_2.com")
...;}
Run Code Online (Sandbox Code Playgroud)
然后使用相同的@Named:
@Provides
IApi1 provideApi_1(@Named("retrofit_1") RestAdapter adapter){....}
@Provides
IApi2 provideApi_2(@Named("retrofit_2") RestAdapter adapter){....}
Run Code Online (Sandbox Code Playgroud)
当您提供 时Retrofit
,您已经设置了 baseUrl“first.url.com”
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("first.url.com")
.client(okHttpClient) //<------ It not OkHttp with your MainInterCeptor
.build();
Run Code Online (Sandbox Code Playgroud)
有趣的是,您MainInterceptor
使用新网址设置的内容与您的无关Retrofit
,因为Retrofit
是使用OkHttp
@Provides
@Singleton
OkHttpClient provideOkhttpClient() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(15, TimeUnit.SECONDS);
client.connectTimeout(20, TimeUnit.SECONDS);
return client.build();
}
Run Code Online (Sandbox Code Playgroud)
如果您想动态更改 BASE_URL,有多种方法可以实现。
public class ApiConstant {
public static String BASE_URL = "yourUrl";
private ApiConstant() {
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个@Scope for Retrofit,因为它可以更改
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}
Run Code Online (Sandbox Code Playgroud)
然后在构建Retrofit时使用它
@Provides
@PerActivity
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("first.url.com")
.client(ApiConstant.BASE_URL)
.build();
}
Run Code Online (Sandbox Code Playgroud)
然后更改BASE_URL
when init DaggerActivityComponent
。(或者为新的url创建一个新的@Scope)
阅读此文档了解更多详细信息
希望这有帮助!
归档时间: |
|
查看次数: |
4540 次 |
最近记录: |