Retrofit 2/Rxjava - 从响应中检索标头

use*_*216 3 java http-headers retrofit2 rx-java2

@GET("/city/{cityId}/category/all")
Observable<MyDictionary> getDictionaries(@Path(value = "cityId", encoded = true) String cityId, @HeaderMap Map<String, String> headers);
Run Code Online (Sandbox Code Playgroud)

调用方法检索数据:

service.getDictionaries(cityId, headersMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                     /*...............*/

                }, throwable ->
                {
                     /*...............*/
                });
Run Code Online (Sandbox Code Playgroud)

我可以轻松得到答复。但我还需要知道响应头。如何在我的代码中访问它们?有小费吗?我只获取正文,如何获取标题?

Ank*_*hta 5

您可以通过以下方式获得回复headers

设置响应类型如下Observable<Response<MyDictionary>>

服务接口中:

@GET("/city/{cityId}/category/all")
Observable<Response<MyDictionary>> getDictionaries(@Path(value = "cityId", encoded = true) String cityId, @HeaderMap Map<String, String> headers);
Run Code Online (Sandbox Code Playgroud)

在调用 api: 时,您可以通过以下方式获取标头:

response.getHeaders()方法。