gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:19 25 4
gpt4 key购买 nike

在我的项目中,我使用 Retrofit 并尝试使用 Dagger 来注入(inject)依赖项。我还有 2 个具有不同 API 的 Retrofit 服务。我需要同时使用 2 个具有不同 baseUrls 的不同 API。我卡在这里,不知道下一步该做什么。

我的应用模块:

@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 fragment 中解决依赖关系的方式。

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

问题是我需要在 Fragment 中使用 2 个 API 进行注入(inject),以从这些 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 cannot be provided without and @Provides or @Produces-annotated method”

应用组件

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

Retrofit exposeRetrofit();

Context exposeContext();
}

最佳答案

您只需将 @Inject 注释与 @Named() 注释一起使用,如下所示:

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

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

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

关于android - Dagger2,同时提供不同API的Retrofit实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39902710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com