【问题标题】:Realm+Gson+Retrofit2 parsingRealm+Gson+Retrofit2 解析
【发布时间】:2017-02-04 08:21:11
【问题描述】:

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

                                        }

领域对象


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

此设置在调试版本中运行良好,但是当我生成发布版本时,这会导致错误

field Catagory.channeList 的类型为 RealmList 得到 ArrayList


不知道为什么它在调试版本中工作。 我已经经历了一些解决方案,但在我的情况下没有运气。 任何帮助将不胜感激,谢谢

【问题讨论】:

  • 不要在模型中使用类型 RealmList 而是使用 ArrayList。
  • 但我必须在类别列表和频道列表之间建立关系。我们可以用 ArrayList 做到这一点吗?为什么它只在调试版本中工作,知道吗?
  • 我没有在发布版本中尝试过。请尝试使用 ArrayList。让我知道。

标签: android retrofit retrofit2 realm-mobile-platform gson


【解决方案1】:

终于找到了解决办法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多