【发布时间】:2017-10-31 23:03:12
【问题描述】:
这不是一个问题。它解释了另一种获取此异常的方法。
所以我使用 Retrofit 2 并且已经正确设置:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile "com.squareup.retrofit2:converter-scalars:2.3.0"
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
我的 ApiHelper 中有这个 Retrofit 配置:
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
领域 4.1.1:
dependencies {
classpath "io.realm:realm-gradle-plugin:4.1.1"
}
和我项目中的 GSON。
但是我遇到了异常:
java.lang.IllegalArgumentException: Unable to create converter for class<br>
在请求发生之前来自 ApiHelper。
解释:
这个例外实际上与改造无关(但大多数答案都与改造配置有关)。它关于领域。从 4.0 版开始,他们增加了对 RealmList of Primitives 的支持。我的响应类包含这样的字段:
@SerializedName("bad_habits")
private RealmList<Integer> badHabitIds;
实际上这就是异常的原因!领域文档说
原语列表不支持列表列表、查询和导入 使用 Realm.create*FromJson API。
但这对我来说似乎不清楚。因此,如果您遇到此类异常并且您已设置好所有内容(我的意思是 Retrofit/GSON/gradle.build),很可能是由于使用了原语列表。支持它们,但不支持反序列化。
【问题讨论】:
标签: android gson realm retrofit retrofit2