问题
Retrofit2 response
*@Override
public void onResponse(Call<List<Category>> call, final Response<List<Category>> response) {
switch (response.code()) {
case 200:
if (response.body() != null && !response.body().isEmpty()) {
final List<Category> categoryList = response.body();
Realm.getDefaultInstance().executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
for (Category category : categoryList) {
realm.insertOrUpdate(category);
}
}
});
}
RealmObjects
public class Category extends RealmObject {
private String catName;
@PrimaryKey
private int catId;
private RealmList<ChannelList> channelList;
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public int getCatId() {
return catId;
}
public void setCatId(int catId) {
this.catId = catId;
}
public RealmList<ChannelList> getChannelList() {
return channelList;
}
public void setChannelList(RealmList<ChannelList> channelList) {
this.channelList = channelList;
}
}
public class ChannelList extends RealmObject {
private String channelName;
private String outputUrl;
@PrimaryKey
private int channelId;
private String thumbnail;
public ChannelList() {
}
public ChannelList(String channelName, String outputUrl, String thumbnail, int channelId) {
this.channelName = channelName;
this.outputUrl = outputUrl;
this.thumbnail = thumbnail;
this.channelId = channelId;
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public String getOutputUrl() {
return outputUrl;
}
public void setOutputUrl(String outputUrl) {
this.outputUrl = outputUrl;
}
public int getChannelId() {
return channelId;
}
public void setChannelId(int channelId) {
this.channelId = channelId;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
}
This setup is working fine in debug build, but when I generate release build, this causes error
field Catagory.channeList has type RealmList got ArrayList
Don't know why its working in debug build. I have gone through some solutions but no luck in my case. Any help would be appreciated, thanks
回答1:
Finally found the solution using.
gsonBuilder.registerTypeAdapter(new TypeToken<RealmList<ChannelList>>(){}.getType(), new RealmStringDeserializer());
Gson gson = gsonBuilder.create();
retrofit = builder.client(httpClient.build()).addConverterFactory(GsonConverterFactory.create(gson)).build();
and
public class RealmStringDeserializer implements JsonDeserializer<RealmList<ChannelList>> {
@Override
public RealmList<ChannelList> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
RealmList<ChannelList> realmStrings = new RealmList<>();
JsonArray stringList = json.getAsJsonArray();
for (JsonElement stringElement : stringList) {
realmStrings.add(new ChannelList());
}
return realmStrings;
}
}
来源:https://stackoverflow.com/questions/42038147/realmgsonretrofit2-parsing