@GET("/city/{cityId}/category/all")
Observable<MyDictionary> getDictionaries(@Path(value = "cityId", encoded = true) String cityId, @HeaderMap Map<String, String> headers);
调用方法来检索数据:
service.getDictionaries(cityId, headersMap)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(resp ->
{
/*...............*/
}, throwable ->
{
/*...............*/
});
我可以轻松获得回复。但我还需要知道响应头。如何在我的代码中访问它们?有小费吗?我只获取正文,如何获取标题?
最佳答案
您可以得到回复headers
这样:
将响应类型设置为 Observable<Response<MyDictionary>>
如下:
在服务接口(interface)中:
@GET("/city/{cityId}/category/all")
Observable<Response<MyDictionary>> getDictionaries(@Path(value = "cityId", encoded = true) String cityId, @HeaderMap Map<String, String> headers);
调用 api 时:您可以通过以下方式获取 header :
response.getHeaders()
方法。
关于java - Retrofit 2/Rxjava - 从响应中检索 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54092234/