android - How call Authenticator.authenticate with custom respond in okhttp? - Stack Overflow
Request process
request:
url:http://host/test
method: post
body:{"token":"AAA"}
if token is invalid respond:
{"code":401,"msg":"unauth"}
Now,I hope Authenticator.authenticate
can be called then retry request:
url:http://host/test
method: post
body:{"token":"BBB"}
if token is invalid respond:
{"code":200,"msg":"ok"}
Kotlin Code
OkHttpClient.Builder()
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.callTimeout(10, TimeUnit.SECONDS)
.addInterceptor {
val request = it.request()
Log.d(Tags.HTTP, "address: ${request.url}")
val response = it.proceed(request)
response.body?.let { body ->
val bodyString = body.string()
if (bodyString.decodeJson<BaseResponse>().code == 401) {
response.newBuilder().code(401).message("Not Authorized").build()
} else {
response.newBuilder().body(bodyString.toResponseBody("text/plain".toMediaType())).build()
}
} ?: response
}
.authenticator(object : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
Log.d(TAG, "start retry auth")
return response.request.newBuilder().method(request.method,"{\"token\":\"BBB\"".toRequestBody("application/json".toMediaType()))
}
})
.build()
Why I send invalid token,Authenticator.authenticate
not be called.
Request process
request:
url:http://host/test
method: post
body:{"token":"AAA"}
if token is invalid respond:
{"code":401,"msg":"unauth"}
Now,I hope Authenticator.authenticate
can be called then retry request:
url:http://host/test
method: post
body:{"token":"BBB"}
if token is invalid respond:
{"code":200,"msg":"ok"}
Kotlin Code
OkHttpClient.Builder()
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.callTimeout(10, TimeUnit.SECONDS)
.addInterceptor {
val request = it.request()
Log.d(Tags.HTTP, "address: ${request.url}")
val response = it.proceed(request)
response.body?.let { body ->
val bodyString = body.string()
if (bodyString.decodeJson<BaseResponse>().code == 401) {
response.newBuilder().code(401).message("Not Authorized").build()
} else {
response.newBuilder().body(bodyString.toResponseBody("text/plain".toMediaType())).build()
}
} ?: response
}
.authenticator(object : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
Log.d(TAG, "start retry auth")
return response.request.newBuilder().method(request.method,"{\"token\":\"BBB\"".toRequestBody("application/json".toMediaType()))
}
})
.build()
Why I send invalid token,Authenticator.authenticate
not be called.
1 Answer
Reset to default 0I resolve the question by only Interceptor,but it is not best answer,so i watting a better answer.
OkHttpClient.Builder().addInterceptor {
val request = it.request()
val response = it.proceed(request)
response.body?.let { body ->
val bodyString = body.string()
// “code" in body
if (bodyString.decodeJson<BaseResponse>().code == 401) {
val copyRespond = response.newBuilder().build()
// not call authenticate by okhttp
// when you need just call it
// but it not try more times
val authorizedRequest = authenticate(copyRespond)
if (authorizedRequest != null) {
it.proceed(authorizedRequest)
} else {
copyRespond
}
} else {
response.newBuilder().body(bodyString.toResponseBody("text/plain".toMediaType())).build()
}
} ?: response
}
.build()
最新文章
- 谷歌继续封死华为后路,Mate 30无法安装谷歌服务
- IBM联合创新策略伙伴合作启动
- 移动互联网生态基石平台的吸引力分析
- 云计算是新的商业基础设施
- 微软反攻打响跨界战争:Win8移动领域伸橄榄枝
- c++ - Why is my OpenGL application rendering a 3D model with unexpected transparency? - Stack Overflow
- javascript - Chrome Extension with proxy, onAuthRequired never being triggered - Stack Overflow
- windows 10 - Gamemaker Mobile device Inconsistent Drag Speed Across Different Operating Systems (Win7 vs. Win10) - Stack Overflo
- android - How to reduce height of a composable to wrap content without padding - Stack Overflow
- mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
- typescript - How to correctly mock an express middleware with jest? - Stack Overflow
- How to log information in Spring application when request is received and before response is returned - Stack Overflow
- node.js - How to handle time taking processes in slack app view - Stack Overflow
- google cloud platform - Java application unable to find ADC when Workload Identity is enabled on GKE cluster - Stack Overflow
- I can't receive messages through my webhook with Whatsapp Cloud API - Stack Overflow
- Excel—Include LEN and HYPERLINK in One Cell - Stack Overflow
- node.js - Nestjs can't find build source after creating workspace - Stack Overflow