Here I see 2 options:
- Use the dagger as intended. For each
baseUrl your own Retrofit client or - Use the interceptor to modify the request before sending it.
Dagger approach
If you were to use brute force URLs, that would probably not be the right choice, as it relies on creating a new Retrofit instance for each.
Now every time the URL changes, you simply recreate the next UrlComponent demonstrated by providing it with a new UrlModule .
Cleaning
Clear the @Singleton module @Singleton that it provides GsonConverterFactory and RxJavaCallAdapterFactory to properly use the dagger and not recreate common objects.
@Module public class SingletonModule { @Provides @Singleton GsonConverterFactory provideOkHttpClient() {} } @Singleton @Component(modules = SingletonModule.class) interface SingletonComponent {
URL scope
Enter @UrlScope to span your Retrofit instances.
@Scope @Retention(RetentionPolicy.RUNTIME) public @interface UrlScope { }
Then create a subcomponent
@SubComponent(modules=UrlModule.class) public interface UrlComponent {}
And a module for him
@Module class UrlModule { private final String mUrl; UrlModule(String url) { mUrl = url; } @Provides String provideUrl() { return mUrl; } @Provides @UrlScope OkHttpClient provideOkHttpClient(String url) { return new OkHttpClient.Builder().build(); } @Provides @UrlScope Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder().build(); } }
Use Retrofit Scope
Create an instance of the component and use it.
class Dagger { public void demo() { UrlModule module = new UrlModule(); SingletonComponent singletonComponent = DaggerSingletonComponent.create(); UrlComponent urlComponent = singletonComponent.plus(module); urlComponent.getRetrofit();
OkHttp Approach
Provide the interceptor properly ( @Singleton in this case) and implement the appropriate logic.
@Module class SingletonModule { @Provides @Singleton GsonConverterFactory provideGsonConverter() { } @Provides @Singleton RxJavaCallAdapterFactory provideRxJavaCallAdapter() { } @Provides @Singleton MyApiInterceptor provideMyApiInterceptor() { } @Provides @Singleton OkHttpClient provideOkHttpClient(MyApiInterceptor interceptor) { return new OkHttpClient.Builder().build(); } @Provides @Singleton Retrofit provideRetrofit(OkHttpClient client) { return new Retrofit.Builder().build(); } } @Singleton @Component(modules = SingletonModule.class) interface SingletonComponent { Retrofit getRetrofit(); MyApiInterceptor getInterceptor(); }
todo MyApiInterceptor . You will need to set the setter for the base url and then just rewrite / modify the requests going through.
Then try using it again.
class Dagger { public void demo() { SingletonComponent singletonComponent = DaggerSingletonComponent.create(); MyService service = singletonComponent.getRetrofit().create(MyService.class); MyApiInterceptor interceptor = singletonComponent.getInterceptor(); interceptor.setBaseUrl(myUrlA); service.doA(); interceptor.setBaseUrl(someOtherUrl); service.doB(); } }
As a third approach, you can also use reflection to directly change the base URL: I added this last for completeness only.