Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Gson converter in RetroFit?

I'm making a simple RetroFit application for educational purposes, and use IntelliJ IDEA as my IDE.

I have imported the Retrofit library properly (at least I think I have) but I'm not able to get the Gson Converter package. I have the gson.jar from google installed but nowhere in either of these libraries is there a class called "GsonConverterFactory", which is required for me to parse JSON.

Edit: I'm on Windows.

like image 206
vic.vele Avatar asked Sep 06 '15 21:09

vic.vele


People also ask

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.

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.

What is the use of GSON converter?

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

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

Call the created instance of that class (done via create() ) to your Retrofit instance with the method addConverterFactory() . After you've added the converter to Retrofit, it'll automatically take care of mapping the JSON data to your Java objects, as you've seen in the previous blog post.


3 Answers

Add compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' to your build.gradle file and get the dependency resolved or add corresponding jars to your bulid path.

then use GsonConverterFactory.create() to get the Converter Factory

I tried using 2.0.0-beta1 but it gave me an illegal type conversion error for factory as given below, so moved to 2.0.0-beta2

  error: method addConverterFactory in class Builder cannot be applied to   given types;
    required: Factory
    found: GsonConverterFactory
    reason: actual argument GsonConverterFactory cannot be converted to Factory by method invocation conversion

So my suggestion is to use 2.0.0-beta2

my build.gradle has following dependencies to resolve retrofit.

 compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'  
 compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
like image 58
manoj Avatar answered Oct 07 '22 08:10

manoj


If you are using retrofit 2, you need to include the convert-gson package. For gradle builds, you can add compile 'com.squareup.retrofit:converter-gson:2.0.0-beta3' to your dependencies section.

For other build systems, or to download the jar, checkout the Maven Central convert-gson page.

like image 24
iagreen Avatar answered Oct 07 '22 09:10

iagreen


in your Module:app build.gradle add

compile 'com.squareup.retrofit2:converter-gson:[retrofit2 version]'

the version above is same with your retrofit2 version, so for example your retrofit2 version is 2.1.0, than your build.gradle should be like this :

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
like image 15
faruk Avatar answered Oct 07 '22 08:10

faruk