Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit/Robospice: get response headers from successful request?

I am using Retrofit/Robospice to make api calls in an app I've built, with a RetrofitGsonSpiceService. All responses are converted into POJOs using a GSON converter, however there is some information I need to retrieve from the response header. I cannot find any means to get the headers (I can only get the headers if the request is unsuccessful because the raw response is sent in the error object!) how can I intercept the response to grab the headers before it is converted?

like image 490
AndroidNoob Avatar asked May 27 '14 18:05

AndroidNoob


1 Answers

It took me a few minutes to figure out exactly what @mato was suggesting in his answer. Here's a concrete example of how to replace the OkClient that comes with Retrofit in order to intercept the response headers.

public class InterceptingOkClient extends OkClient
{
    public InterceptingOkClient()
    {
    }

    public InterceptingOkClient(OkHttpClient client)
    {
        super(client);
    }

    @Override
    public Response execute(Request request) throws IOException
    {
        Response response = super.execute(request);

        for (Header header : response.getHeaders())
        {
            // do something with header
        }

        return response;
    }
}

You then pass an instance of your custom client to the RestAdapter.Builder:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setClient(new InterceptingOkClient())
    ....
    .build();
like image 151
moswald Avatar answered Nov 03 '22 20:11

moswald