0

I am having an issue converting pojo to gson, my conversion method freeze the app, then shows a dialog saying "app not responding, do you want to close it?"

Below is the Pojo class

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

Below is the conversion method

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

            return json;
        }

Below is full class for the use of the conversion.

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

}

SimpuMind
  • 365
  • 8
  • 22

2 Answers2

1

Because you're creating a new Gson() instance every single time you're converting a JSON element into an object, instead of having just one Gson instance.

It's also likely you aren't actually doing the parsing on a background thread, a common mistake if you're using Volley.

(And also, why do you even have Realm in your app if you're directly creating gigantic String objects that you write to Shared Preference, avoiding the usage of your actual database? You shouldn't have a String lying around in memory if the data set is too large.)

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Seems you understand the problem well, am hoping you can guide me through solving this, is there any part of the code i should change or provide so it can be solved. Thanks. – SimpuMind Jul 16 '16 at 15:04
  • I am using retrofit, am trying to create a like and unlike feature in my app, the reason for you using Shared Preference. – SimpuMind Jul 16 '16 at 15:17
  • You should create only **one** instance of `Gson` in your Application subclass, then use that everywhere, or in your `Utils` class as `private static final Gson gson = new Gson();`. You should use Realm to obtain your liked and fav news with a query, and you should leave SharedPreferences out of this completely. – EpicPandaForce Jul 16 '16 at 15:29
  • i don't know if this is too much too ask, i am really new to Realm, i can't really think of a way to approach the problem of removing shared preference and adding query from Realm if you can help please. – SimpuMind Jul 16 '16 at 15:54
  • Your object needs to know if the item is liked/favorited, and then you can query for it based on `realm.where(NewsTrend.class).equalTo("isLiked", true).findAllSorted(timestamp, Sort.DESCENDING);` to receive a list of it which you can use in an [adapter](https://github.com/realm/realm-android-adapters) – EpicPandaForce Jul 16 '16 at 16:36
  • I am very much confused on what to do now, tried few thing and i can't seem to figure out the logic – SimpuMind Jul 16 '16 at 17:51
  • You need to utilize Realm as intended: https://realm.io/docs/java/latest/#queries – EpicPandaForce Jul 16 '16 at 19:35
0

U can convert pojo to gson using http://www.jsonschema2pojo.org/ site.

Lovekesh
  • 125
  • 11