Realm + Gson + Retrofit2解析 [英] Realm+Gson+Retrofit2 parsing

查看:207
本文介绍了Realm + Gson + Retrofit2解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

                 *@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



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

字段Catagory.channeList具有RealmList类型,具有ArrayList

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

推荐答案

最终找到了使用的解决方案.

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();

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;
}
}

这篇关于Realm + Gson + Retrofit2解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆