简体   繁体   中英

Retrofit 2/Rxjava - retrieve headers from response

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

Call method to retrieve data:

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

                }, throwable ->
                {
                     /*...............*/
                });

I can get reposne eaisly. But I also need to know response headers. How to access them in my code? Any tips? I'm getting only body, how to get headers also?

You can get response headers this way:

Set response type as Observable<Response<MyDictionary>> as following:

In Service Interface:

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

and while calling api: you would be able to get headers by :

response.getHeaders() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM