【发布时间】:2014-05-22 02:57:06
【问题描述】:
我有一个非通用类型的List,我想将其存储在SharedPreferences 中。为此,我使用 Gson 将List 转换为字符串,然后将其存储到SharedPreferences,如下所示:
Gson gson = new Gson();
Type fooType = new TypeToken<ArrayList<UserFeedMaster>>() {}.getType();
// feedTempList is the ArrayList of type UserFeedMaster class
String gsonFeeds = gson.toJson(feedTempList,fooType);
prefs.edit().putString("recent_feedlist", gsonFeeds).commit();
稍后我试图以这种方式取回 ArrayList<UserFeedMaster> 的值:
String gsonFeed = prefs.getString("recent_feedlist", null);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<UserFeedMaster>>(){}.getType();
List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type);
对于序列化数据,它工作正常。但这里的问题是,当我对其进行反序列化时,我在List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type); 行收到错误java.lang.IllegalArgumentException: invalid value for field
错误日志:
java.lang.IllegalArgumentException: invalid value for field
at java.lang.reflect.Field.setField(Native Method)
at java.lang.reflect.Field.set(Field.java:588)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:245)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:206)
at com.google.api.client.util.GenericData.put(GenericData.java:103)
at com.google.api.client.util.GenericData.put(GenericData.java:47)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:189)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.myapp.sample.RecentFeedsFragment.onActivityCreated(RecentFeedsFragment.java:178)`
UserFeedMaster.java
public class UserFeedMaster {
@Id
private String feedBlobKey;
private Date feedDateTime;
private int feedLikes;
private boolean feedIsPrivate;
private boolean userIsAnonymous;
private String feedTags;
private boolean isFeedDeleted;
private double latitude;
private double longitude;
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getFeedBlobKey() {
return feedBlobKey;
}
public void setFeedBlobKey(String feedBlobKey) {
this.feedBlobKey = feedBlobKey;
}
public Date getFeedDateTime() {
return feedDateTime;
}
public void setFeedDateTime(Date feedDateTime) {
this.feedDateTime = feedDateTime;
}
public int getFeedLikes() {
return feedLikes;
}
public void setFeedLikes(int feedLikes) {
this.feedLikes = feedLikes;
}
public boolean isFeedIsPrivate() {
return feedIsPrivate;
}
public void setFeedIsPrivate(boolean feedIsPrivate) {
this.feedIsPrivate = feedIsPrivate;
}
public boolean isUserIsAnonymous() {
return userIsAnonymous;
}
public void setUserIsAnonymous(boolean userIsAnonymous) {
this.userIsAnonymous = userIsAnonymous;
}
public String getFeedTags() {
return feedTags;
}
public void setFeedTags(String feedTags) {
this.feedTags = feedTags;
}
public boolean isFeedDeleted() {
return isFeedDeleted;
}
public void setFeedDeleted(boolean isFeedDeleted) {
this.isFeedDeleted = isFeedDeleted;
}
}
JSON 响应:
[
{
"feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
"feedDateTime": {
"value": 1396953609433,
"tzShift": 0,
"dateOnly": false
},
"feedDeleted": false,
"feedIsPrivate": false,
"feedLikes": 0,
"kind": "userfeedmasterendpoint#resourcesItem"
},
{
"feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
"feedDateTime": {
"value": 1396953609433,
"tzShift": 0,
"dateOnly": false
},
"feedDeleted": false,
"feedIsPrivate": false,
"feedLikes": 0,
"kind": "userfeedmasterendpoint#resourcesItem"
}
]
我不知道出了什么问题。请帮助我了解为什么会出现此错误以及如何解决它。 提前致谢。
【问题讨论】:
-
发布完整的堆栈跟踪。理想情况下,使用您的类和 JSON 发布一个可重现的小示例。
-
@SotiriosDelimanolis 当然。请稍等。
-
RecentFeedsFragment.java,行号 178 ??
-
@TheLittleNaruto
List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type); -
我有一种感觉,你需要调试类型和gsonfeed,然后再将它们传递给fromJson函数,检查它是否有数据或空值??
标签: java android json arraylist gson