添加Interceptor并使用Dagger2注入后,翻新服务始终为空

时间:2019-04-09 07:58:50

标签: java android retrofit dagger-2 okhttp

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

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为空。我应该如何提供依赖关系?任何帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

据我所知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,它应该完成工作。