Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否使用新令牌请求改装重试?_Java_Kotlin_Retrofit_Retrofit2 - Fatal编程技术网

Java 是否使用新令牌请求改装重试?

Java 是否使用新令牌请求改装重试?,java,kotlin,retrofit,retrofit2,Java,Kotlin,Retrofit,Retrofit2,当任何请求返回令牌\未\激活时,我获取新令牌,然后获取新令牌 在拦截器中 override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() var response = chain.proceed(request) val bodyString = response.peekBody(AppConstants.BUFFER_SIZE_2MB)?.string

当任何请求返回令牌\未\激活时,我获取新令牌,然后获取新令牌

在拦截器中

override fun intercept(chain: Interceptor.Chain): Response {
    val request = chain.request()
    var response = chain.proceed(request)
    val bodyString = response.peekBody(AppConstants.BUFFER_SIZE_2MB)?.string()

    val json = JSONObject(bodyString)
//after get new token retry with the failedRequest
    if(json.has("token"){
         failedRequest=failedRequest.newBuilder().removeHeader("Authorization").removeHeader("")
                                    .addHeader("Authorization", "bearer " +json.getString("access_token"))
                                    .addHeader("source_id", Math.random().toString()

                                    ).build()

                            isNewTokenAfterFaild=false
                            return chain.proenter code hereceed(failedRequest)
}


if (resultObj.code == AppConstants.TOKEN_IS_NOT_ACTIVE )
{

  failedRequest=chain.request()
  getTokenOnce() -> make retrofit HTTP request
}

     return response // this line make a problem for me because this will proceed the chain,
                     //and callback will be notified with the failed request 



}
是否有任何方法阻止返回新令牌的代码,并对此进行处理

令牌码

fun getToken(context: Context) = CoroutineScope(Dispatchers.IO).async {

         val requestMap = prepareTokenHeader(context)
        val tokenEndpoint = RetrofitClientInstance.buildService(TokenService::class.java)

            val token = tokenEndpoint.getToken(
                    AppConstants().tokenBaseUrl,
                    requestMap
            )