将Pojo转换为Gson

时间:2016-07-16 14:47:20

标签: android gson realm pojo

我遇到了将pojo转换为gson的问题,我的转换方法会冻结应用,然后会显示一个对话框,说明"应用无响应,是否要关闭它?"

以下是Pojo课程

NewsTrend.class

 public class NewsTrend extends RealmObject {

        private String title;
        private String href;
        private String content;
        private String image;
        private String timestamp;
        private Integer comments;
        private String type;
        private String flag;
        private String ext_date;
        private Integer like_status;
        private Integer like_count;
        private Integer read_count;
        private Integer dislike_count;
        private Integer read_status;
        private Integer news_id;


        /* Requires an empty constructor */
        public NewsTrend() {
        }

        public NewsTrend(NewsTrend postsData) {
            this.title = postsData.getTitle();
            this.href = postsData.getHref();
            this.timestamp = postsData.getTimestamp();
            this.type = postsData.getType();
            this.comments = postsData.getComments();
            this.image = postsData.getImage();
            this.content = postsData.getContent();
            this.flag = postsData.getFlag();
            this.ext_date = postsData.getExt_date();
            this.like_status = postsData.getLike_status();
            this.like_count = postsData.getLike_count();
            this.dislike_count = postsData.getDislike_count();
            this.read_status = postsData.getRead_status();
            this.news_id = postsData.getNews_id();
            this.read_count = postsData.getRead_count();
        }


        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getHref() {
            return href;
        }

        public void setHref(String href) {
            this.href = href;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public Integer getComments() {
            return comments;
        }

        public void setComments(Integer comments) {
            this.comments = comments;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }

        public String getExt_date() {
            return ext_date;
        }

        public void setExt_date(String ext_date) {
            this.ext_date = ext_date;
        }

        public Integer getLike_status() {
            return like_status;
        }

        public void setLike_status(Integer like_status) {
            this.like_status = like_status;
        }

        public Integer getLike_count() {
            return like_count;
        }

        public void setLike_count(Integer like_count) {
            this.like_count = like_count;
        }

        public Integer getRead_count() {
            return read_count;
        }

        public void setRead_count(Integer read_count) {
            this.read_count = read_count;
        }

        public Integer getDislike_count() {
            return dislike_count;
        }

        public void setDislike_count(Integer dislike_count) {
            this.dislike_count = dislike_count;
        }

        public Integer getRead_status() {
            return read_status;
        }

        public void setRead_status(Integer read_status) {
            this.read_status = read_status;
        }

        public Integer getNews_id() {
            return news_id;
        }

        public void setNews_id(Integer news_id) {
            this.news_id = news_id;
        }
    }

NewsTrendList.class

public class NewsTrendList extends RealmObject {

        private RealmList<NewsTrend> data;

        /**
         *
         * @return
         *     The data
         */
        public RealmList<NewsTrend> getData() {
            return data;
        }

        /**
         *
         * @param data
         *     The data
         */
        public void setData(RealmList<NewsTrend> data) {
            this.data = data;
        }
    }

以下是转换方法

public static String convertToJson(ArrayList<NewsTrend> newsItems) {
            Gson gson = new Gson();
            String json = gson.toJson(newsItems); //The App hangs on this line

            return json;
        }

以下是使用转化的完整课程。

public class Utils {

//public RealmResults<NewsTrend> realmResults;

public static boolean isNewsLikedOrFaved(Context context, NewsTrend newsItem, boolean isLiked) {

    ArrayList<NewsTrend> newsItems = getLikedOrFavedNews(context, isLiked);

    if (newsItems != null) {


        for (NewsTrend news : newsItems) {

            if (newsItem.getNews_id().equals(news.getNews_id())) {
                Log.d("logFavourite", "isNewsLikedOrFaved true newsItem.getId: " + newsItem.getNews_id() + ", news.getId: " + news.getNews_id());

                return true;
            }
        }
    }

    Log.d("logFavourite", "isNewsLikedOrFaved false");

    return false;

}


public static void likeOrFavNews(Context context, NewsTrend newsItem, boolean isLike) {

    Log.d("logFavourite", "likeNews");


    SharedPreferences preferences = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);

    String jsonLikedNews;
    if (isLike)
        jsonLikedNews = preferences.getString("likedNews", null);
    else
        jsonLikedNews = preferences.getString("favedNews", null);


    if (jsonLikedNews != null) {
        ArrayList<NewsTrend> newsItems = convertToModel(jsonLikedNews);
        newsItems.add(newsItem);

        Log.d("logFavourite", "size: " + newsItems.size());
        String jsonUpdatedLikedNews = convertToJson(newsItems);

        SharedPreferences.Editor editor = preferences.edit();
        if (isLike)
            editor.putString("likedNews", jsonUpdatedLikedNews);
        else
            editor.putString("favedNews", jsonUpdatedLikedNews);

        editor.commit();
    } else {

        ArrayList<NewsTrend> newsItems = new ArrayList<>();
        newsItems.add(newsItem);

        Log.d("logFavourite", "size: " + newsItems.size());
        String jsonUpdatedLikedNews = convertToJson(newsItems);


        SharedPreferences.Editor editor = preferences.edit();
        if (isLike)
            editor.putString("likedNews", jsonUpdatedLikedNews);
        else
            editor.putString("favedNews", jsonUpdatedLikedNews);

        editor.commit();


    }

}

public static void unlikeOrUnfavNews(Context context, NewsTrend newsItem, boolean isLike) {

    Log.d("logFavourite", "likeNews");

    SharedPreferences preferences = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);

    String jsonLikedNews;
    if (isLike)
        jsonLikedNews = preferences.getString("likedNews", null);
    else
        jsonLikedNews = preferences.getString("favedNews", null);

    if (jsonLikedNews != null) {
        ArrayList<NewsTrend> newsItems = convertToModel(jsonLikedNews);

        for (int i = 0; i < newsItems.size(); i++) {

            if (newsItems.get(i).getNews_id() == newsItem.getNews_id())
                newsItems.remove(i);

        }

        Log.d("logFavourite", "size: " + newsItems.size());


        String jsonUpdatedLikedNews = convertToJson(newsItems);

        SharedPreferences.Editor editor = preferences.edit();
        if (isLike)
            editor.putString("likedNews", jsonUpdatedLikedNews);
        else
            editor.putString("favedNews", jsonUpdatedLikedNews);

        editor.commit();
    }

}

public static ArrayList<NewsTrend> getLikedOrFavedNews(Context context, boolean isLike) {

    SharedPreferences preferences = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
    String jsonLikedNews;
    if (isLike)
        jsonLikedNews = preferences.getString("likedNews", null);
    else
        jsonLikedNews = preferences.getString("favedNews", null);

    ArrayList<NewsTrend> newsItems;
    if (jsonLikedNews != null) {
        newsItems = convertToModel(jsonLikedNews);
    } else {
        newsItems = new ArrayList<>();
    }
    return newsItems;


}

        public static String convertToJson(ArrayList<NewsTrend> newsItems) {
            Gson gson = new Gson();
            Log.d("erroFav", "some better error" + newsItems.toString());
            String json = gson.toJson(newsItems);

            return json;
        }

public static ArrayList<NewsTrend> convertToModel(String jsonString) {
    Gson gson = new Gson();
    NewsTrend[] newsItems = gson.fromJson(jsonString, NewsTrend[].class);
    ArrayList<NewsTrend> newsItemList = new ArrayList<>();

    for (int i = 0; i < newsItems.length; i++) {
        newsItemList.add(newsItems[i]);
    }
    return newsItemList;
}





public static String convertToNewsJson(NewsTrend news) {
    Gson gson = new Gson();
    String json = gson.toJson(news);
    return json;
}

public static NewsTrend convertToNewsModel(String jsonString) {
    Gson gson = new Gson();
    NewsTrend news = gson.fromJson(jsonString, NewsTrend.class);
    return news;
}

}

2 个答案:

答案 0 :(得分:1)

因为您每次都在创建new Gson()实例 ,所以您正在将JSON元素转换为对象,而不是只有一个 Gson实例

你也可能没有在背景线程上进行解析,如果你使用的是Volley,这是一个常见的错误。

(而且,如果您直接创建写入共享首选项的巨大String对象,避免使用实际数据库,为什么甚至在应用程序中都有Realm?您不应该有{{1}如果数据集太大,则在内存中存在。)

答案 1 :(得分:0)

你可以使用http://www.jsonschema2pojo.org/将pojo转换为gson 站点。