Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Header from Response (Retrofit / OkHttp Client)

I am using Retrofit with the OkHttp Client and Jackson for Json Serialization and want to get the header of the response.

I know that i can extend the OkClient and intercept it. But this comes before the deserialization process starts.

What i basically needs is to get the header alongside with the deserialized Json Object.

like image 630
dknaack Avatar asked Jun 11 '15 09:06

dknaack


People also ask

What is header in Okhttp?

The header fields of a single HTTP message. Values are uninterpreted strings; use Request and Response for interpreted headers. This class maintains the order of the header fields within the HTTP message. This class tracks header values line-by-line.


1 Answers

With Retrofit 1.9.0, if you use the Callback asynchronous version of the interface,

@GET("/user") void getUser(Callback<User> callback) 

Then your callback will receive a Response object

    Callback<User> user = new Callback<User>() {         @Override         public void success(User user, Response response) {          }          @Override         public void failure(RetrofitError error) {          }     } 

Which has a method called getHeaders()

    Callback<User> user = new Callback<User>() {         @Override         public void success(User user, Response response) {             List<Header> headerList = response.getHeaders();             for(Header header : headerList) {                 Log.d(TAG, header.getName() + " " + header.getValue());             }         } 

For Retrofit 2.0's interface, you can do this with Call<T>.

For Retrofit 2.0's Rx support, you can do this with Observable<Result<T>>

like image 163
EpicPandaForce Avatar answered Sep 28 '22 12:09

EpicPandaForce