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
)