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

在我的项目中,我使用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;
}
}

我总是得到错误“没有提供retrofit2.retrofit和@Provides或@ Produces-annotated方法

ApplicationComponent

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

    Retrofit exposeRetrofit();

    Context exposeContext();
}

解决方法:

您只需使用@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);
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...