如何使用 dagger2 在改装模块中添加动态基础 url
How to add dynamic base url in retrofit module with dagger2
我正在为我的应用程序使用 dagger2。我有一个模块提供了一些依赖项,例如 Retrofit
、Gson
等
NetModule.java
@Module
public class NetModule {
private String mBaseUrl;
public NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newBuilder()
//.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
return okHttpClient;
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
}
NetComponent.java
@Singleton
@Component(modules = {AppModule.class, NetModule.class, Validator.class})
public interface NetComponent {
void inject(AuthenticationActivity authenticationActivity);
void inject(PaymentActivity paymentActivity);
}
AppApplication.java
@Override
public void onCreate() {
super.onCreate();
mNetComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.netModule(new NetModule("https://corporateapiprojectwar.mybluemix.net/corporate_banking/mybank/"))
.build();
}
这种方法一直有效,直到我的整个应用程序只有一个基础 url。现在我对 AuthenticationActivity
和 PaymentActivity
有不同的基础 Url 所以我不能在 NetModule
的 onCreate
的 [=20] 的构造函数中发送 Url =]
任何人都可以帮助我如何使用 dagger2 添加动态基础 Url 改造。
您可以使用 @Named
注释 Dagger2 user guide(参见 'Qualifiers' 部分'):
在你的NetModule.java中:
@Provides
@Singleton
@Named("authRetrofit")
public Retrofit provideAuthRetrofit() {
// setup retrofit for authentication
return retrofit;
}
@Provides
@Singleton
@Named("paymentRetrofit")
public Retrofit providePaymentRetrofit() {
// setup retrofit for payments
return retrofit;
}
在你的AuthenticationActivity
中:
@Inject
@Named("authRetrofit")
Retrofit retrofit;
最后在你的 PaymentActivity.java
:
@Inject
@Named("paymentRetrofit")
Retrofit retrofit;
然后 dagger 应自动将为支付配置的 Retrofit
注入 PaymentActivity 并将为身份验证配置的 Retrofit
注入 AuthenticationActivity
我正在为我的应用程序使用 dagger2。我有一个模块提供了一些依赖项,例如 Retrofit
、Gson
等
NetModule.java
@Module
public class NetModule {
private String mBaseUrl;
public NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newBuilder()
//.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.cache(cache)
.build();
return okHttpClient;
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
}
NetComponent.java
@Singleton
@Component(modules = {AppModule.class, NetModule.class, Validator.class})
public interface NetComponent {
void inject(AuthenticationActivity authenticationActivity);
void inject(PaymentActivity paymentActivity);
}
AppApplication.java
@Override
public void onCreate() {
super.onCreate();
mNetComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this))
.netModule(new NetModule("https://corporateapiprojectwar.mybluemix.net/corporate_banking/mybank/"))
.build();
}
这种方法一直有效,直到我的整个应用程序只有一个基础 url。现在我对 AuthenticationActivity
和 PaymentActivity
有不同的基础 Url 所以我不能在 NetModule
的 onCreate
的 [=20] 的构造函数中发送 Url =]
任何人都可以帮助我如何使用 dagger2 添加动态基础 Url 改造。
您可以使用 @Named
注释 Dagger2 user guide(参见 'Qualifiers' 部分'):
在你的NetModule.java中:
@Provides
@Singleton
@Named("authRetrofit")
public Retrofit provideAuthRetrofit() {
// setup retrofit for authentication
return retrofit;
}
@Provides
@Singleton
@Named("paymentRetrofit")
public Retrofit providePaymentRetrofit() {
// setup retrofit for payments
return retrofit;
}
在你的AuthenticationActivity
中:
@Inject
@Named("authRetrofit")
Retrofit retrofit;
最后在你的 PaymentActivity.java
:
@Inject
@Named("paymentRetrofit")
Retrofit retrofit;
然后 dagger 应自动将为支付配置的 Retrofit
注入 PaymentActivity 并将为身份验证配置的 Retrofit
注入 AuthenticationActivity