【发布时间】:2019-10-24 05:27:10
【问题描述】:
由于我的 API 请求都包含一些共同的 json 字段,我想在拦截器中添加这些字段,但我正在努力修改拦截器中的 OkHttp3 RequestBody
这是我的改造构建器:
private val retrofitBuilder by lazy {
val client = OkHttpClient.Builder().apply {
addInterceptor(MyInterceptor())
}.build()
Retrofit.Builder()
.baseUrl("https://placeholder.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
这里是拦截器:
class MyInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
//Is it possible to change it in JSON? Or how do I add paramenters to this body?
val body: RequestBody? = chain.request().body()
return chain.proceed(chain.request())
}
}
如何将“traceId”:“abc123”添加到拦截器内的所有请求正文中?
【问题讨论】:
标签: android kotlin retrofit interceptor okhttp3