Unable to create converter for my class in Android Retrofit library
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.
Api call failed Unable to create converter for class Retrofit/Moshi
I found the source of the error, when using moshi with retrofit, all data classes must have the annotation of @JsonClass(generateAdapter = true)
We must change the code:
@JsonClass(generateAdapter = true)
data class JConfig(
@Json(name = "data") val data:List<Config>
)
data class Config(
var codigo: Int,
var tipo: String,
var empresa: Int,
var sucursal: Int,
var esquema: Int,
var horainicio: String,
var horafinal: String,
var fecha: String,
var seguimiento: Int
)
For this:
@JsonClass(generateAdapter = true)
data class JConfig(
@Json(name = "data") val data:List<Config>
)
@JsonClass(generateAdapter = true)
data class Config(
var codigo: Int,
var tipo: String,
var empresa: Int,
var sucursal: Int,
var esquema: Int,
var horainicio: String,
var horafinal: String,
var fecha: String,
var seguimiento: Int
)
Unable to create converter for class..., Retrofit2, Realm4 and GSON
The limitation is for createObjectFromJson()
and createAllFromJson()
methods, it has nothing to do with how GSON attempts to (or doesn't, in this case) deserialize it.
You can most likely define a type adapter for your GSON instance and it would fix the problem.
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class); // is this still needed?
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.registerTypeAdapter(new TypeToken<RealmList<Integer>>() {}.getType(), new TypeAdapter<RealmList<Integer>>() {
@Override
public void write(JsonWriter out, RealmList<Integer> value) throws IOException {
// Ignore for now
}
@Override
public RealmList<Integer> read(JsonReader in) throws IOException {
RealmList<Integer> list = new RealmList<Integer>();
in.beginArray();
while (in.hasNext()) {
list.add(in.nextInt());
}
in.endArray();
return list;
}
})
.create();
Unable to create converter for class
Problem is you can't have a variable named id
in your class when using Sugar since by default it uses its own internal ids. Try renaming to userId
or something like that.
Why I get Unable to create converter exception from retrofit?
Finally I found the issue, I made a mistake, because I tried to use suspend fun and retrofit Call at the same time. I deleted the suspend keyword, and it works perfectly.
Related Topics
Position of Dialogfragment in Android
How to Add Bulleted List to Android Application
Adjust Layout When Soft Keyboard Is On
Getting Certificate Details from an APK
Failure [Install_Failed_Invalid_Apk]
How to Create a Multilingual Android Application
In What Period Does the Firebase's App Token Changes and How to Manage It
Disable Home Button in Android Toddler App
Play Sound on Phonegap App for Android
Endless Recyclerview with Progressbar for Pagination
Insertion of Thousands of Contact Entries Using Applybatch Is Slow
Menu Items Are Not Showing on Action Bar
Eclipse Ddms Error "Can't Bind to Local 8600 for Debugger"
Android Sdk Cut/Trim Video File
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory
Cannot Resolve Method 'Getsupportfragmentmanager ( )' Inside Fragment