我有这样的重新登录改造服务

interface TokenService {
    @GET("re-login")
    fun relogin(): Call<ReloginResponse>
}


然后我有一个NetModule,其中所有依赖项都这样注入

@Module
class NetModule(baseUrl: String) {
    var baseUrl = baseUrl
    @Provides
    @Singleton
    fun provideGson(): Gson {
        val gsonBuilder = GsonBuilder()
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        return gsonBuilder.create()
    }

    @Provides
    @Singleton
    fun provideOkhttp(tokenInterceptor: TokenInterceptor): OkHttpClient {

        val logging = HttpLoggingInterceptor()
        logging.setLevel(HttpLoggingInterceptor.Level.BODY)
        val client = OkHttpClient.Builder()
//                .authenticator(tokenAutheticator)
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(logging)
                .addInterceptor(tokenInterceptor)

                .build()

        return client
    }

    @Provides
    @Singleton
    fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build()
    }
    @Provides
    @Singleton
    fun provideTokenHoler():TokenHolder{
        return TokenHolder()
    }
    @Provides
    @Singleton
    fun provideTokenService(retrofit: Retrofit,tokenHolder: TokenHolder):TokenService{
        val tokenService = retrofit.create(TokenService::class.java)
        tokenHolder.tokenService = tokenService
        return tokenService
    }

}


然后TokenHolder类是这样的

class TokenHolder {
    var tokenService:TokenService?=null
        get() = field
        set(value) {field=value}
}


这是TokenInterceptor类,我在TokenService部分中获得null

@Singleton
class TokenInterceptor @Inject constructor(private val tokenHolder: TokenHolder) :Interceptor{
    var token:String=""
    override fun intercept(chain: Interceptor.Chain): Response {

        val original = chain.request()
        val originalHttpUrl = original.url()

        val url = originalHttpUrl.newBuilder()
                .addQueryParameter("token", token)
                .build()

        // Request customization: add request headers
        val requestBuilder = original.newBuilder()
                .url(url)

        val request = requestBuilder.build()
        val response = chain.proceed(request)

        if(response.code()==401){

            val newToken = tokenHolder.tokenService?.relogin()?.execute()?.body()?.token

            val url1 = originalHttpUrl.newBuilder()
                    .addQueryParameter("token", newToken)
                    .build()
            println("########## new new token ########## "+newToken)//this is null
            // Request customization: add request headers
            val requestBuilder1 = original.newBuilder()
                    .url(url1)

            val request1 = requestBuilder1.build()
            val response1 = chain.proceed(request1)
            return response1
        }

        return chain.proceed(request)
    }
}


收到401响应后,我尝试点击Relogin,但我总是得到TokenService null。我应该如何提供依赖关系?任何帮助,将不胜感激。谢谢

最佳答案

据我所知,TokenHolder具有TokenService的空实例,因为没有办法将其传递到那里。您正在通过Dagger创建TokenService实例,并将其传递给TokenHolder,但与在拦截器中获得的实例不同。您应该反转依赖关系。将模块方法更新为此,并告诉我它是否有效。

@Provides
@Singleton
fun provideTokenHolder(service:TokenService): TokenHolder{
    return TokenHolder(service)
}

@Provides
@Singleton
fun provideTokenService(retrofit: Retrofit): TokenService{
    return retrofit.create(TokenService::class.java)
}


TokenService不应为null,因此您应按以下方式声明TokenHolder。

class TokenHolder(val service: TokenService)


此外,打破您的依赖圈。您必须更改TokenInterceptor声明。

class TokenInterceptor @Inject constructor(private val tokenHolder: Lazy<TokenHolder>) : Interceptor


用Dagger.Lazy包裹TokenHolder,它应该可以完成工作。

08-05 19:11