Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why addConverterFactory is need in Retrofit

My question is around Retrofit and GSON. I know GSON is used for JAVA object <-> JSON . I know that Retrofit parses our response. What i don't understand is why we need GsonConverterFactory .Also why is addConverterFactory needed in retrofit

like image 589
Ayush Bansal Avatar asked Mar 09 '19 08:03

Ayush Bansal


People also ask

What's the use of GsonConverterFactory?

Class GsonConverterFactory Because Gson is so flexible in the types it supports, this converter assumes that it can handle all types. If you are mixing JSON serialization with something else (such as protocol buffers), you must add this instance last to allow the other converters a chance to see their types.

What is use of GSON converter factory in retrofit?

Retrofit is a very popular HTTP client library for Android. Using Retrofit makes it easy to parse API response and use it in your application. It has built-in GSON converter that can automatically parse HTTP response into an Object or any other types in Java that can be used in your code.

Which method is used to add the converter to retrofit instance?

4.2. In order to add an adapter the retrofit2. Retrofit. Builder. addCallAdapterFactory(Factory) method has to be used.


Video Answer


3 Answers

UPDATE:

Pojo objects in Kotlin are data classes and annotation use is as in Java

ANSWER:

If your application is Restful, so gets and sends data from / to server

Converter factory need to be added, just for retrofit can convert JSON data (got from server) into java (model) objects (POJO), to use in Android Project.

There are some converter libraries for converting JSON to Java objects, (GSON, Jackson..etc) you have to decide which converter you want to use in your project and add same factory

Dependencies in app.gradle

implementation "com.squareup.retrofit2:converter-gson:VERSION"

and Factories in Retrofit settings

GsonConverterFactory or JacksonConverterFactory

Retrofit.Builder().addConverterFactory(GsonConverterFactory.create());

Also if the remote data type is XML, you need to add SimpleXmlConverterFactory

like image 186
Narek Hayrapetyan Avatar answered Sep 21 '22 00:09

Narek Hayrapetyan


if the json converters don't meet your needs you'll need to Add a customized converter factory for serialization and deserialization of your objects. consider this case. i wish you'll find this article helpful.

like image 24
med.Hamdan Avatar answered Sep 18 '22 00:09

med.Hamdan


The world is not just json and gson. There are other formats that you can use to implement Rest Apis, i.e. XML.

Also, in the world of json parsers there's not only gson but way more like Jackson and Moshi.

It would be extremely difficult to maintain all possible format converters inside Retrofit, so it offloads the parsing to classes that implement the interface for a converter. Putting this behind a factory lets Retrofit decouple even the creation of these converters, so it can have different ones for different responses and requests.

This also allows you to have multiple converters within the same Retrofit instance and it's also an easy way to let you implement your own converter.

All in all, this decoupling allows for way more flexibility than coupling it with specific libraries.

like image 42
Fred Avatar answered Sep 19 '22 00:09

Fred