0

I am currently looking into implementing the Retrofit API (after using Volley) into my app and I have some questions that I cannot seem to find answers to anywhere else so I will ask here.

  1. How do I go about downloading images using Retrofit API? I am asking this because Volley has the ImageLoader class and NetworkedImageView etc. and was wondering if Retrofit has something similar?

    1. I read that using the RequestIntercepter, it can add a header to every request. How is this different from just adding a static (or dynamic) header (@Header) in the interface's abstract method

    2. How does Retrofit deal with nested JSON objects? I read it uses GSON to convert the JSON into java objects but the POJO class must have the same field names.

Thank you for reading

Ersen Osman
  • 7,067
  • 8
  • 47
  • 80

1 Answers1

0

For your first doubt:

Retrofit no have feature to manager image as Volley and UIL has. The same company that developer Retrofit too have a nice lib to manager images called Picasso.

For your second doubt:

Headers that need to be added to every request can be specified using a RequestInterceptor. The following code creates a RequestInterceptor that will add a User-Agent header to every request.

for instance:

I developed a client to Parse.com and to all request I need set my keys in the header: see here Android-Retrofit-Example

public class RestClient {

    private static RestClient mRestClient = null;
    private static RestAdapter restAdapter;
    private static RequestInterceptor requestInterceptor;

    public static RestClient getInstance(){

        if(mRestClient == null ){

            mRestClient = new RestClient();
            setup();
        }

        return mRestClient;
    }

    private static void setup(){

        requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {

                request.addHeader("X-Parse-Application-Id", "");
                request.addHeader("X-Parse-REST-API-Key", "");
            }
        };

        restAdapter = new RestAdapter.Builder()
                .setEndpoint(" https://api.parse.com/1/")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setRequestInterceptor(requestInterceptor)
                .build();
    }

    public RestAdapter getRestAdapter(){
        return restAdapter;
    }
}

The last :

JSON CONVERSION

Retrofit uses Gson by default to convert HTTP bodies to and from JSON. If you want to specify behavior that is different from Gson's defaults (e.g. naming policies, date formats, custom types), provide a new Gson instance with your desired behavior when building a RestAdapter. Refer to the Gson documentation for more details on customization.

To get this :

{
results: [3]
        {
            createdAt: "2015-03-07T20:43:44.107Z"
            objectId: "osCJ8PI65r"
            updatedAt: "2015-03-08T00:45:37.539Z"
            username: "Test 2"
        },
        {
            createdAt: "2015-03-07T21:42:38.591Z"
            objectId: "tkIi6Ll1Os"
            updatedAt: "2015-03-07T21:42:38.591Z"
            username: "Test 2"
        },
        {
            createdAt: "2015-03-08T01:13:21.188Z"
            objectId: "Cz0HqiYpwl"
            updatedAt: "2015-03-08T04:21:18.069Z"
            username: "Test 3"
        }
}

Pojo :

public class User {

    private String objectId;
    private String username;
    private String createdAt;
    private String updatedAt;

    //gerate getters and setters
}

public class WrappeUser {

    @SerializedName(value="results")
    List<User> results;

    public List<User> getResults() {
        return results;
    }

    public void setResults(List<User> results) {
        this.results = results;
    }
}
Anderson K
  • 5,445
  • 5
  • 35
  • 50
  • Thanks, that's great. In the github example you provided, is there a reason why in the MainActivity the call to the service occurs on a separate thread (async task) I thought the network tasks were by default on a separate thread but the call back occurs on the main thread? – Ersen Osman Mar 12 '15 at 22:38
  • 1
    Retrofit methods can be declared for either synchronous or asynchronous execution. Asynchronous execution requires the last parameter of the method be a callback that will be executed on the main thread =) – Anderson K Mar 13 '15 at 01:39
  • Thanks. One more question. How would you handle deeply nested JSON objects? Would it be better to just get the raw JSON and do it yourself? The JSON data I have has a lot of nested objects with objects in objects :S – Ersen Osman Mar 14 '15 at 10:51