Upgrade / Robospice: get response headers from a successful request?

I am using Retrofit / Robospice to make api calls in the application I created with RetrofitGsonSpiceService. All responses are converted to POJO using the GSON converter, however there is some information that I need to get from the response header. I can’t find any means to get the headers (I can only get the headers if the request is unsuccessful because the original response was sent to the error object!), How can I intercept the answer to capture the headers before converting it ?

+4
source share
2 answers

It took me a few minutes to figure out exactly what @mato suggested in his answer. Here is a concrete example of how to replace OkClientthat comes with Retrofit to intercept 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;
    }
}

Then you pass the instance of your user client to RestAdapter.Builder:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setClient(new InterceptingOkClient())
    ....
    .build();
+16
source

RoboSpice , HTTP-, . HTTP-. Retrofit Apache, OkHttp HTTP Android, , . , Retrofit HTTP- (. , ), .

Retrofit Client. , , : ApacheClient, OkClient UrlConnectionClient. , , , , .

, Client .

+3

Source: https://habr.com/ru/post/1542219/


All Articles