获取令牌时,OkHttp拦截器链断开

时间:2019-06-14 10:41:45

标签: android kotlin retrofit retrofit2 okhttp

我有一个OkHttp拦截器,当请求收到401 HTTP错误时,它应该询问令牌。现在,登录服务的请求已完成,但是链断开了,并且原始请求未重试。 这是我的拦截器的拦截方法:

override fun intercept(chain: Interceptor.Chain): Response {
        logger.d("AuthenticationServiceHolder $authenticationServiceHolder")

        val originalRequest = chain.request()
        logger.d("Intercepting call to ${originalRequest.method()} ${originalRequest.url()}")

        val response: Response = chain.proceed(originalRequest)
        val successful = response.isSuccessful
        val code = response.code()
        logger.d("Response successful: $successful - code: $code")

        if (!successful && code == HttpURLConnection.HTTP_UNAUTHORIZED) {
            logger.d("Token is $token")

            val deviceUuid = deviceIdentificationManager.deviceUuid().blockingGet()
            logger.d("Device uuid $deviceUuid")
            if (deviceUuid != null) {
                val authenticationService = authenticationServiceHolder.get()
                if (authenticationService != null) {
                    token = reLogin(authenticationService, deviceUuid)
                    if (token != null) {
                        val headersBuilder = originalRequest.headers().newBuilder()
                        headersBuilder.removeAll(AUTHORIZATION_HEADER)
                        headersBuilder.add(AUTHORIZATION_HEADER, token!!)

                        val requestBuilder = originalRequest.newBuilder()
                        val request = requestBuilder.headers(headersBuilder.build()).build()
                        return chain.proceed(request)
                    } else {
                        logger.e("Token was not retrieved")
                    }
                } else {
                    logger.e("Authentication service is null!")
                }
            }
        }
        return response
}

reLogin()方法为:

private fun reLogin(authenticationService: AuthenticationService, deviceUuid: UUID): String? {
        logger.d("reLogin() - authenticationService $authenticationService")
        val blockingGet = authenticationService?.login(LoginRequest(deviceUuid, clock.currentTime()))?.blockingGet()
        logger.d("reLogin() - response $blockingGet")
        val response = blockingGet ?: return null
        logger.d("reLogin() - token ${response.token}")
        return response.token
}

新: 正如Mitesh Machhoya所说,我尝试了2种不同的改造实例,一种实例带有带有拦截器的okhttp客户端,另一种实例没有。 现在登录调用没有被拦截,但是拦截器的执行中断了,我的意思是该类的日志跟踪为:

 - AuthenticationServiceHolder XXXmypackageXXX.AuthenticationServiceHolder... 
 - Intercepting call to GET XXXmyInterceptedCallXXX 
 - Response successful: false - code: 401 
 - Token is null 
 - Device uuid XXX 
 - reLogin() - authenticationService retrofit2.Retrofit$1@a5c0a25 

仅此而已。我的意思是reLogin() - response.....未打印。我确定该登录呼叫有效,因为我在okhttp log中看到了登录响应。

2 个答案:

答案 0 :(得分:0)

在不附加拦截器的情况下与其他httpClient进行reLogin请求,那么它将按预期工作。

如果您使用相同的httpClient发出reLogin请求,则它将通过拦截器,并且每次都会覆盖请求,因此请尝试使用另一个httpClient进行请求

答案 1 :(得分:0)

粘贴的代码运行良好,登录请求正常运行,但是登录响应在服务器端发生了更改,反序列化导致崩溃并中断了链。