Dagger2, providing Retrofit instances with different APIs same time
Asked Answered
E

2

6

In my project I use Retrofit and trying to use Dagger for injecting dependencies. I also have 2 Retrofit services with different APIs. I need to use 2 different APIs with different baseUrls at the same time. I stucked here, and dont know what to do next.

My ApplicationModule:

@Module
public class ApplicationModule {

private String FIRST_API_URL = "https://first-api.com";
private String SECOND_API_URL = "https://second-api.com";

private String mBaseUrl;
private Context mContext;

public ApplicationModule(Context context) {
    mContext = context;
}

@Singleton
@Provides
GsonConverterFactory provideGsonConverterFactory() {
    return GsonConverterFactory.create();
}

@Singleton
@Provides
@Named("ok-1")
OkHttpClient provideOkHttpClient1() {
    return new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .build();
}

@Singleton
@Provides
@Named("ok-2")
OkHttpClient provideOkHttpClient2() {
    return new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();
}

@Singleton
@Provides
RxJavaCallAdapterFactory provideRxJavaCallAdapterFactory() {
    return RxJavaCallAdapterFactory.create();
}

@Singleton
@Provides
@FirstApi
Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory converterFactory, RxJavaCallAdapterFactory adapterFactory) {
    return new Retrofit.Builder()
            .baseUrl(FIRST_API_URL)
            .addConverterFactory(converterFactory)
            .addCallAdapterFactory(adapterFactory)
            .client(client)
            .build();
}

@Singleton
@Provides
@SecondApi
Retrofit provideRetrofit2(@Named("ok-1") OkHttpClient client, GsonConverterFactory converterFactory, RxJavaCallAdapterFactory adapterFactory) {
    return new Retrofit.Builder()
            .baseUrl(SECOND_API_URL)
            .addConverterFactory(converterFactory)
            .addCallAdapterFactory(adapterFactory)
            .client(client)
            .build();
}

@Provides
@Singleton
Context provideContext() {
    return mContext;
}
}

My Application:

public class MyApplication extends Application {

private ApplicationComponent mApplicationComponent;

@Override
public void onCreate() {
    super.onCreate();
    initializeApplicationComponent();
}

private void initializeApplicationComponent() {
    mApplicationComponent = DaggerApplicationComponent
            .builder()
            .applicationModule(new ApplicationModule(this, Constant.BASE_URL))   // I think here needs to do something to use different URLs
            .build();
}

public ApplicationComponent getApplicationComponent() {
    return mApplicationComponent;
}

@Override
public void onTerminate() {
    super.onTerminate();
}
}

This is how I am resolving dependencies in My fragment.

    protected void resolveDependency() {
    DaggerSerialComponent.builder()
            .applicationComponent(getApplicationComponent())
            .contactModule(new ContactModule(this))
            .build().inject(this);
}

The problem is that I need to do injection with 2 APIs in Fragment, to obtain data from these APIs.

UPDATED: I have created annotations:

@Qualifier
@Retention(RUNTIME)
public @interface FirstApi{}

@Qualifier
@Retention(RUNTIME)
public @interface SecondApi{}

My Contact Module:

@Module
public class ContactModule {

private ContactView mContactView;

public ContactModule(ContactView contactView) {
    mContactView = contactView;

}

@PerActivity
@Provides
FirstContactService provideFirstContactService(@FirstApi Retrofit retrofit) {
    return retrofit.create(FirstContactService.class);
}

@PerActivity
@Provides
SecondContactService provideSecondContactService(@SecondApi Retrofit retrofit) {
    return retrofit.create(SecondContactService.class);
}

@PerActivity
@Provides
ContactView provideContactView() {
    return mContactView;
}
}

I always get error "retrofit2.retrofit cannot be provided without and @Provides or @Produces-annotated method"

ApplicationComponent

@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {

    Retrofit exposeRetrofit();

    Context exposeContext();
}
Ellipsoid answered 6/10, 2016 at 17:57 Comment(1)
where is your retrofit injected? There should be an injected retrofit instance in the fragment, and you can probably name it tooSwordplay
C
13

You just use the @Inject annotation along with the @Named() annotation, like so:

@Inject @Named("provideRetrofit") Retrofit mRetrofit;
@Inject @Named("provideRetrofit2") Retrofit mRetrofit2;

Or you could even inject the Retrofit services directly:

@Provides @Singleton
public CustomService provideCustomService(@Named("provideRetrofit") Retrofit retrofit) {
    return retrofit.create(CustomService.class);
}
Consentient answered 7/10, 2016 at 16:51 Comment(2)
When I add Named annotation to rertofit, I got an error "retrofit2.Retrofit cannot be provided without an @Inject constructor or from an Provides-annotated method. retrofit2.Retrofit is provided at com.myapp.dependencies.component.ApplicationComponent.exposeRetrofit()"Ellipsoid
@Ellipsoid Did you try cleaning and rebuilding your project? The dependencies will not get updated until Dagger generates your object graph, which won't happen until a new build is executed.Consentient
H
0

add @Named annotation to your provided retrofit instances like below:

@Named("retrofit_one")
Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory converterFactory, RxJavaCallAdapterFactory adapterFactory) {
return new Retrofit.Builder()
.baseUrl(FIRST_API_URL) 
.addConverterFactory(converterFactory) 
.addCallAdapterFactory(adapterFactory) 
.client(client) 
.build();
 }



@Named("retrofit_two")
    Retrofit provideRetrofit(@Named("ok-1") OkHttpClient client, GsonConverterFactory converterFactory, RxJavaCallAdapterFactory adapterFactory) {
    return new Retrofit.Builder()
    .baseUrl(SECOND_API_URL) 
    .addConverterFactory(converterFactory) 
    .addCallAdapterFactory(adapterFactory) 
    .client(client) 
    .build();
     }

and then wherever you need, inject the two instances like:

@Inject @Named("retrofit_one") Retrofit mRetrofitOne;   
@Inject @Named("retrofit_two") Retrofit mRetrofitTwo;
Handwork answered 18/3, 2019 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.