Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create converter for my class in Android Retrofit library

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.

What is retrofit library in Android?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.


If anyone ever comes across this in the future because you are trying to define your own custom converter factory and are getting this error, it can also be caused by having multiple variables in a class with a misspelled or the same serialized name. IE:

public class foo {
  @SerializedName("name")
  String firstName;
  @SerializedName("name")
  String lastName;
}

Having serialized names defined twice (likely by mistake) will also throw this exact same error.

Update: Keep in mind that this logic also holds true via inheritance. If you extend to a parent class with an object that has the same Serialized name as you do in the sub-class, it will cause this same problem.


Prior to 2.0.0, the default converter was a gson converter, but in 2.0.0 and later the default converter is ResponseBody. From the docs:

By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body.

In 2.0.0+, you need to explicitly specify you want a Gson converter:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("**sample base url here**")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

You will also need to add the following dependency to your gradle file:

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

Use the same version for the converter as you do for your retrofit. The above matches this retrofit dependency:

compile ('com.squareup.retrofit2:retrofit:2.1.0')

Also, note as of writing this, the retrofit docs are not completely updated, which is why that example got you into trouble. From the docs:

Note: This site is still in the process of being expanded for the new 2.0 APIs.


just make sure that you are not using the same serialize name twice

 @SerializedName("name") val name: String
 @SerializedName("name") val firstName: String

just remove one of them


Based on top comment I updated my imports

implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

I've used http://www.jsonschema2pojo.org/ in order to create pojo's from Spotify json results and making sure to specify Gson format.

These days there are Android Studio plugins which can create the pojo's or Kotlin data models for you. One great option for mac is Quicktype. https://itunes.apple.com/us/app/paste-json-as-code-quicktype/id1330801220


In my case, I had a TextView object inside my modal class and GSON did not know how to serialize it. Marking it as 'transient' solved the issue.


@Silmarilos's post helped me solve this. In my case, it was that I used "id" as a serialized name, like this:

 @SerializedName("id")
var node_id: String? = null

and I changed it to

 @SerializedName("node_id")
var node_id: String? = null

All working now. I forgot that 'id' is a default attribute.


This may help someone

In my case mistakenly I wrote SerializedName like this

@SerializedName("name","time")
String name,time; 

It should be

@SerializedName("name")
String name;

@SerializedName("time")
String time;