带有OkHttpOAuthConsumer的Android Hilt和翻新版

问题描述

我正在使用Hilt,并且试图创建和注入Retrofit客户端。

我的问题是我的一位改造客户需要一个SigningInterceptor(consumer),而我的消费者(OkHttpOAuthConsumer)需要一个setTokenWithSecret(token,tokenSecret)

所以我的问题是,在创建Retrofit客户端时如何将信息(令牌和tokenSecret)传递给Hilt

一些代码(可能更清楚):

我的ApiModule.kt

@InstallIn(ApplicationComponent::class)
@Module
object ApiModule {

    //used to change URL dynamically
    var url: String = "https://localhost"

    @Provides
    @Singleton
    @Named("unsigned")
    fun provideRetrofitClient(): Retrofit {
        val interceptor = HttpLoggingInterceptor()
        if (BuildConfig.DEBUG) {
            interceptor.level = HttpLoggingInterceptor.Level.BODY
        } else {
            interceptor.level = HttpLoggingInterceptor.Level.NONE
        }

        val client = OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .build()

        return Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    @Named("signed")
    // The problem is here with those String
    fun getClient(token: String,tokenSecret: String): Retrofit {

        val interceptor = HttpLoggingInterceptor()
        if (BuildConfig.DEBUG) {
            interceptor.level = HttpLoggingInterceptor.Level.BODY
        } else {
            interceptor.level = HttpLoggingInterceptor.Level.NONE
        }

        consumer = OkHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET)
        consumer.setTokenWithSecret(token,tokenSecret)
        
        val client = OkHttpClient.Builder()
            .addInterceptor(SigningInterceptor(consumer))
            .addInterceptor(interceptor)
            .build()

        return Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    fun provideLoginApi(
        @Named("unsigned") retrofit: Retrofit
    ): LoginApiInterface =
        retrofit.create(LoginApiInterface::class.java)


    @Provides
    @Singleton
    fun provideUpdateDeviceApiInterface(
        @Named("signed") retrofit: Retrofit
    ): UpdateDeviceApiInterface =
        retrofit.create(UpdateDeviceApiInterface::class.java)

}

我的Repository

class MyRepository @Inject constructor(
    ...
    private var updateDeviceApiInterface: UpdateDeviceApiInterface,...
) {
...
}

我的ViewModel

class MyViewModel @Inject constructor(
    private var myRepository: MyRepository
) :
    ViewModel() {
    ...
}

最后我的Fragment

@AndroidEntryPoint
class MyFragment : Fragment() {
    ...
    @Inject
    lateinit var myViewModel: MyViewModel
    ...
}

(我对第一个改造客户端@Named(unsigned)没问题,所以使用LoginApiInterface没问题)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...