Unable to create converter for my class in Android Retrofit library
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 itsRequestBody
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