简体   繁体   中英

How to add dynamic base url in retrofit module with dagger2

I am using dagger2 for my application. I have one module which provides some dependencies like Retrofit , Gson etc.

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();

}

This approach was working until I had only one base url for my complete application. Now I have different base Url for AuthenticationActivity and PaymentActivity so I can not send Url in constructor of NetModule in onCreate of Application

Can anyone help me how to add dynamic base Url of retrofit using dagger2.

You can use @Named annotation Dagger2 user guide (see 'Qualifiers' section'):

In your 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;
}

In your AuthenticationActivity :

@Inject
@Named("authRetrofit")
Retrofit retrofit;

And finally in your PaymentActivity.java :

@Inject
@Named("paymentRetrofit")
Retrofit retrofit;

Then dagger shall automatically inject Retrofit configured for payments into PaymentActivity and Retrofit configured for authentication into AuthenticationActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM