0

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

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

字段 Catagory.channeList 的类型为 RealmList 得到 ArrayList


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

4

1 回答 1

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;
}
}
于 2017-02-08T06:29:34.447 回答