You cannot do that using the annotation method. Here's how I have done it in my projects.
class Authenticator : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
return response.request().newBuilder()
.header("Authorization", ConstantsEncrypted.usernamePassword()).build()
}
}
And don't forget to add it to your OkHttpClient.Builder()
instance using authenticator(Authenticator())
something like below:
OkHttpClient.Builder().apply {
writeTimeout(120, TimeUnit.SECONDS)
readTimeout(120, TimeUnit.SECONDS)
connectTimeout(120, TimeUnit.SECONDS)
callTimeout(120, TimeUnit.SECONDS)
// your code
// this is where you add your authenticator
authenticator(Authenticator())
// more code
}.build()
The Authenticator
is from package okhttp3
. You can find more info on class here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…