Android:当应用程序转到后台时存储ParseObject数据

时间:2015-03-06 07:03:48

标签: android parse-platform

在我的代码中,我有这个。

public static List<MeetingApp> meetingAppList;

MeetingApp是一种Pa​​rseObject,代码是Fragment的一部分。 我遇到的问题是当我的应用程序转到后台时,似乎数据被破坏了。当我再次启动应用程序时,它会崩溃,因为列表丢失且片段需要数据。该片段是ViewPager的一部分,而setRetainInstance(true)显然不起作用;

我试图保存对象,但ParseObject不是Parcelable或Serializable。

我知道如何存储数据或保存片段状态然后恢复它吗?是否可以将其保存在JSON中?。

问候。

编辑1:如果我将数据保存为Json格式,这是崩溃的回溯。

Process: mcongress.mobicongress.com.mcongressapp, PID: 3922
java.lang.StackOverflowError
        at java.util.GregorianCalendar.computeYearAndDay(GregorianCalendar.java:798)
        at java.util.GregorianCalendar.fullFieldsCalc(GregorianCalendar.java:480)
        at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:573)
        at java.util.Calendar.complete(Calendar.java:830)
        at java.util.Calendar.setTimeInMillis(Calendar.java:1195)
        at java.util.Calendar.setTime(Calendar.java:1183)
        at java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:518)
        at java.text.SimpleDateFormat.format(SimpleDateFormat.java:818)
        at java.text.DateFormat.format(DateFormat.java:307)
        at com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:90)
        at com.google.gson.internal.bind.DateTypeAdapter.write(DateTypeAdapter.java:41)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
        at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
        at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
        at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
        at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)

现在我正在尝试使用引脚名称将列表保留在ParseLocalDatastore中。我认为这可能是一个简单的解决方案。

2 个答案:

答案 0 :(得分:0)

所以只需使用gson库将其解析为Json

(适用序列化)

List<MeetingApp> meetingAppList;
Gson gson = new Gson();
String json = gson.toJson(meetingAppList);  
//==> json is [{"value1":1,"value2":"abc"},...

请注意,您无法使用循环引用序列化对象,因为这会导致无限递归。

(适用反序列化)

Type listType = new TypeToken<List<YourClass>>() {
                }.getType();
List<MeetingApp> meetingAppList = gson.fromJson(json, listType);   
//==> obj2 is just like obj

答案 1 :(得分:0)

Parse具有将对象保存在本地数据库中的功能。它可以帮助您离线保存数据,但也可以用于存储应用程序的数据!

请查看有关Parse Local Datastore

的此链接