如何从响应中获取自定义 header(改造和 rxjava)
How to get custom header from response (retrofit & rxjava)
我正在尝试从我代码中的所有 REST API 响应中获取一些自定义 HTTP headers。
以下是我启动客户端的方式:
private fun initializeClient(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
.addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original = chain.request()
val request = original.newBuilder()
.header("Content-Type", "application/json")
.method(original.method, original.body)
.build()
return chain.proceed(request)
}
})
.addInterceptor(interceptor)
.build()
return httpClient
}
private fun initializeApi() {
api = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(initializeClient())
.build()
.create(RestApi::class.java)
}
这是改造实施的示例:
@POST("/register")
fun registerUser(@Body registerPostData: RegistrationForm): Single<RegistrationResponse>
和 ViewModel 部分:
disposable.add(
api.registerUser(form)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : DisposableSingleObserver<Member>() {
override fun onSuccess(value: RegistrationResponse?) {
// no headers in here
response.value = value
}
override fun onError(e: Throwable?) {
}
})
)
服务器 returns 在所有 API 响应中有一些 x-custom-header
,我需要在所有调用中捕获这些响应。
谢谢!
将改造 class 更改为 return a Response:
@POST("/register")
fun registerUser(@Body registerPostData: RegistrationForm): Single<Response<RegistrationResponse>>
然后:
override fun onSuccess(result: Response<RegistrationResponse>?) {
// get header from response.headers()
}
我正在尝试从我代码中的所有 REST API 响应中获取一些自定义 HTTP headers。 以下是我启动客户端的方式:
private fun initializeClient(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val httpClient = OkHttpClient.Builder()
.addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original = chain.request()
val request = original.newBuilder()
.header("Content-Type", "application/json")
.method(original.method, original.body)
.build()
return chain.proceed(request)
}
})
.addInterceptor(interceptor)
.build()
return httpClient
}
private fun initializeApi() {
api = Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(initializeClient())
.build()
.create(RestApi::class.java)
}
这是改造实施的示例:
@POST("/register")
fun registerUser(@Body registerPostData: RegistrationForm): Single<RegistrationResponse>
和 ViewModel 部分:
disposable.add(
api.registerUser(form)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : DisposableSingleObserver<Member>() {
override fun onSuccess(value: RegistrationResponse?) {
// no headers in here
response.value = value
}
override fun onError(e: Throwable?) {
}
})
)
服务器 returns 在所有 API 响应中有一些 x-custom-header
,我需要在所有调用中捕获这些响应。
谢谢!
将改造 class 更改为 return a Response:
@POST("/register")
fun registerUser(@Body registerPostData: RegistrationForm): Single<Response<RegistrationResponse>>
然后:
override fun onSuccess(result: Response<RegistrationResponse>?) {
// get header from response.headers()
}