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()
最新文章
- 苹果允许预装软件可卸载 或因遭受安卓手机阵型冲击
- 成本高、厂商疑 微软Surface面临十大风险
- 微软推Surface平板 成硬件合作伙伴竞争对手
- 研究机构:平板四年内将成主流计算设备
- WHM API - start_autossl_check_for_one_user - Stack Overflow
- java - Custome Recipe Parsing Error when developing mod with fabric in Minecraft 1.21.4 - Stack Overflow
- responsive design - How achieve given UI using custom clipper in flutter - Stack Overflow
- reactjs - Next.js - I need static error page template in non-static app router based app - Stack Overflow
- tracking - Add custom hand gestures in three.js - Stack Overflow
- c# - I have a time clock connected to my local network and I have a Windows server on localweb and I can't get the clock
- amazon web services - How to create a CloudWatch alarm for an EventBridge Pipe's stopped state in AWS? - Stack Overflow
- youtube - How to disable the Page Visibility API of react native webview? - Stack Overflow
- amazon ses - Is it possible to send a RawMessage using Apache Camel AWS SES? - Stack Overflow
- Supabase - new row violates row-level security policy - Stack Overflow
- php - How to return blob in Joomla 5 API - Stack Overflow
- I can't receive messages through my webhook with Whatsapp Cloud API - Stack Overflow
- php - Add Algorithm to the XML tag generated by SOAP - Stack Overflow