Retrofit callback get response body-open source projects square/retrofit

Michael Alan Huff

I recently encountered a similar problem. I wanted to look at some json in the response body but didn’t want to deal with the TypedByteArray from Retrofit. I found the quickest way to get around it was to make a Pojo(Plain Old Java Object) with a single String field. More Generally you would make a Pojo with one field corresponding to whatever data you wanted to look at.

For example, say I was making a request in which the response from the server was a single string in the response’s body called “access_token”

My Pojo would look like this:

public class AccessToken{
    String accessToken;

    public AccessToken() {}

    public String getAccessToken() {
        return accessToken;
    }
} 

and then my callback would look like this

Callback callback = new Callback() {
   @Override
   public void success(AccessToken accessToken, Response response) {
       Log.d(TAG,"access token: "+ accessToken.getAccessToken());
   }

   @Override
   public void failure(RetrofitError error) {
       Log.E(TAG,"error: "+ error.toString());
   }
};

This will enable you to look at what you received in the response.