嘿,我在KotlinMutliplatorm moblie工作的Android和iOS项目。当我使用Retrofit时,我可以很容易地创建拦截器来查看调用:
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
val httpClient = OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(HeadersInterceptor())
报头拦截器
import okhttp3.Interceptor
import okhttp3.Response
class HeadersInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()
requestBuilder.addHeader("Client-Version", getClientVersion())
val request = requestBuilder.build()
return chain.proceed(request)
}
private fun getClientVersion(): String {
var versionName = BuildConfig.HEADER_VERSION
return "Android $versionName"
}
}
然后
val retrofit = retrofit2.Retrofit.Builder()
.client(httpClient.build())
现在我想切换Ktor。我的问题是我可以在ktor中重用okhttp3HeadersInterceptor()
吗?如果可以,那么我如何使用请给予我举例。如果没有,请告诉我替代方法,因为我有这么多拦截器,如AuthorizationInterceptor,拦截器自己的实现。谢谢
1条答案
按热度按时间hwamh0ep1#
只需将拦截器添加到Ktor OkHttp客户端构建器