我有一個問題,將pojo轉換爲gson,我的轉換方法凍結了應用程序,然後顯示一個對話框,顯示「應用程序未響應,是否要關閉它?」將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;
}
}
似乎你很好地理解了這個問題,希望你能指導我解決這個問題,有沒有我應該更改或提供部分代碼,以便可以解決。謝謝。 – SimpiMind
我正在使用改造,試圖在我的應用程序中創建一個類似而不同的功能,您使用Shared Preference的原因。 – SimpiMind
你應該在你的Application子類中創建一個** Gson的實例,然後在任何地方或者在'Utils'類中使用private static final Gson gson = new Gson();'。您應該使用Realm通過查詢來獲得您喜歡的和最喜歡的新聞,並且您應該完全離開SharedPreferences。 – EpicPandaForce