gpt4 book ai didi

Android Hilt 使用两个 Retrofit2 客户端

转载 作者:行者123 更新时间:2023-12-05 00:18:48 28 4
gpt4 key购买 nike

我想用 Dagger Hilt 设置两个 Retrofit2 客户端,因为我的应用程序从两个不同的 API 获取信息。但是当我运行我的代码时,会抛出以下异常 ->

App_HiltComponents.java:128: error: [Dagger/DuplicateBindings] retrofit2.Retrofit is bound multiple times:
public abstract static class SingletonC implements HiltWrapper_ActivityRetainedComponentManager_ActivityRetainedComponentBuilderEntryPoint,
^
@org.jetbrains.annotations.NotNull @Provides retrofit2.Retrofit de.kotlinbooks.di.NetworkBooksModule.retrofitBooks(okhttp3.OkHttpClient)
@org.jetbrains.annotations.NotNull @Provides retrofit2.Retrofit de.kotlinbooks.di.NetworkNYTModule.retrofitNYT(okhttp3.OkHttpClient)

我知道改造客户端是一个单例,所以我将我的模块分成两个文件用于不同的 api。这是我的代码 ->

NetworkBooksModule

@Module
@InstallIn(SingletonComponent::class)
class NetworkBooksModule {

@Provides
fun okHttpClient(): OkHttpClient {
val levelType: HttpLoggingInterceptor.Level = if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

val logging = HttpLoggingInterceptor()
logging.setLevel(levelType)

return OkHttpClient.Builder()
.addInterceptor(logging)
.build()
}

@Provides
fun retrofitBooks(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
}

@Provides
fun bookService(retrofit: Retrofit): BookService {
return retrofit.create(BookService::class.java)
}

@Provides
fun bookClient(bookService: BookService): BookClient {
return BookClient(bookService)
}
}

NetworkNYTModule

@Module
@InstallIn(SingletonComponent::class)
class NetworkNYTModule {

@Provides
fun okHttpClient(): OkHttpClient {
val levelType: HttpLoggingInterceptor.Level = if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE

val logging = HttpLoggingInterceptor()
logging.setLevel(levelType)

return OkHttpClient.Builder()
.addInterceptor(logging)
.build()
}

@Provides
fun retrofitNYT(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl("https://api.nytimes.com/svc/books/v3/")
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
}

@Provides
fun nytService(retrofit: Retrofit): NYTService {
return retrofit.create(NYTService::class.java)
}

@Provides
fun nytClient(nytService: NYTService): NYTClient {
return NYTClient(nytService)
}
}

最佳答案

您可以使用该接口(interface)来提供您的 retrofit ,而不是 retrofit 本身

@Module
@InstallIn(SingletonComponent::class)
class NetworModule {

@Provides
fun retrofitNYT(okHttpClient: OkHttpClient): NYTService{
return Retrofit.Builder()
.baseUrl("https://api.nytimes.com/svc/books/v3/")
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(NYTService::class.java)
}

@Provides
fun retrofitBooks(okHttpClient: OkHttpClient): BookService{
return Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.client(okHttpClient)
.addConverterFactory(MoshiConverterFactory.create())
.build()
.create(BookService::class.java)
}

}

关于Android Hilt 使用两个 Retrofit2 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66330447/

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