对于我的拦截器,我需要使用ifecycleOwner,我使用Hilt模块中的拦截器.但我不知道如何在Hilt模块中获得i



@模块@InstallIn(SingletonComponent::class(

对象AppEntryModule{

private val client = OkHttpClient.Builder().apply {
addInterceptor(MyInterceptor(  ?????   ))
}.build()
@Provides
@Singleton
fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(MOCK_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()

}

class MyInterceptor@Inject构造函数(private val view生命周期所有者:生命周期所有者):拦截器{{}

我找到了解决方案:

object AppEntryModule {
@Provides
@Singleton
fun provideRetrofit(@ApplicationContext appContext: Context): Retrofit {
val client = OkHttpClient.Builder().apply {
addInterceptor(MyInterceptor(MyPreferences(appContext)))
}.build()
return  Retrofit.Builder()
.baseUrl(MOCK_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}

最新更新