Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use retrofit as a singleton while using gson convertor?

From the @jake Wharton answer you should only ever call restAdapter.create once and re-use the same instance of MyTaskService every time you need to interact with. I cannot stress this enough. You can use the regular singleton pattern in order to ensure that there only is ever a single instance of these objects that you use everywhere. A dependency injection framework would also be something that could be used to manage these instances but would be a bit overkill if you are not already utilizing it.

this is my code

public class MusicApi {
private static final String API_URL = "https://itunes.apple.com";
private static MusicApiInterface sMusicApiInterface;

public static MusicApiInterface getApi() {
    if (sMusicApiInterface == null) {
        sMusicApiInterface = null;
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(API_URL)
                .build();

        sMusicApiInterface = restAdapter.create(MusicApiInterface.class);
    }
    return sMusicApiInterface;
}

public interface MusicApiInterface {
    @GET("/search?entity=musicVideo")
    NetworkResponse getMusic(@Query("term") String term);

    @GET("/search?entity=musicVideo")
    void getMusic(@Query("term") String term, Callback<NetworkResponse> networkResponseCallback);

    @GET("/search?entity=musicVideo")
    Observable<NetworkResponse> getMusicObservable(@Query("term") String term);
}

}

Everything works fine. I am using type adapter, For each request I need to create different type of gson parse and setting into adapter.

Gson gson = new GsonBuilder().registerTypeAdapter(DiscussionViewMoreContainer.class, new ExplorerDeserializerJson())
            .create();

it makes me that i have to create a new restadapter every time. In my app some requests are running parallely.is this correct way?

like image 503
Asthme Avatar asked Nov 03 '15 08:11

Asthme


People also ask

Is GSON a singleton?

As now, Gson instance is thread-safe so you can get single instance of Gson through Singleton pattern or through DI (dependency injection).

How do I use retrofit GSON?

The First thing we are going to do is add retrofit and GSON library to our build. gradle (Module:app) > dependencies. Then we will make a class for each movie object which looks like it's is inside an array with the key of “results”. So first we will make a class for the entire response.


1 Answers

you don't have to create it every time, but just once, when you create your RestAdapter:

public static MusicApiInterface getApi() {
    if (sMusicApiInterface == null) {
       Gson gson = new GsonBuilder()
           .registerTypeAdapter(DiscussionViewMoreContainer.class, new ExplorerDeserializerJson())
           .create();
       RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(API_URL)
            .setConverter(new GsonConverter(gson))
            .build();
       sMusicApiInterface = restAdapter.create(MusicApiInterface.class);
     }
     return sMusicApiInterface;
}

if you need to register more than one Deserializer, just call .registerTypeAdapter multiple times with the pair Class/TypeToken and instance of the custom Deserializer. Gson will call correct one depending on the return type of the retrofit's method you are calling. E.g

Gson gson = new GsonBuilder()
           .registerTypeAdapter(DiscussionViewMoreContainer.class, new ExplorerDeserializerJson())
           .registerTypeAdapter(OtherModelClass.class, new OtherModelClassDeserializerJson())
           .registerTypeAdapter(OtherModelClass3.class, new OtherModelClass3DeserializerJson())
like image 159
Blackbelt Avatar answered Sep 20 '22 05:09

Blackbelt