Dagger2,同时为不同的API提供Retrofit实例

时间:2016-10-06 17:57:29

标签: android retrofit mvp dagger-2

在我的项目中,我使用Retrofit并尝试使用Dagger注入依赖项。我还有2个不同API的Retrofit服务。我需要同时使用2个不同的API和不同的baseUrls。我坚持到这里,不知道接下来该做什么。

我的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;
}
}

我的申请:

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

这就是我在My片段中解析依赖关系的方法。

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

问题是我需要在Fragment中注入2个API,以便从这些API中获取数据。

更新: 我创建了注释:

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

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

我的联系人模块:

@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;
}
}

我总是收到错误"如果没有@Provides或@ Produces-annotated方法"

,则无法提供retrofit2.retrofit。

ApplicationComponent

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

    Retrofit exposeRetrofit();

    Context exposeContext();
}

2 个答案:

答案 0 :(得分:7)

您只需使用@Inject注释以及@Named()注释,如下所示:

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

或者您甚至可以直接注入Retrofit服务:

@Provides @Singleton
public CustomService provideCustomService(@Named("provideRetrofit") Retrofit retrofit) {
    return retrofit.create(CustomService.class);
}

答案 1 :(得分:0)

将@Named注释添加到您提供的改造实例中,如下所示:

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

然后在需要的地方注入两个实例,例如:

@Inject @Named("retrofit_one") Retrofit mRetrofitOne;   
@Inject @Named("retrofit_two") Retrofit mRetrofitTwo;