某些涉及GSON的Android设备出错

时间:2015-10-11 16:09:39

标签: android gson

问题似乎只发生在三星设备上。但它确实发生在一台非三星设备上。我一直试图在我的手机上复制这个问题(Nexus 5,香草棒棒糖)但它并没有发生。

这将保存BusEntrys的ArrayList的ArrayList的ArrayList

private static void saveDataList(ArrayList<ArrayList<ArrayList<BusEntry>>> list, String key1){
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(RO.mainActivity.getApplicationContext());
    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
    ArrayList<ArrayList<ArrayList<BusEntry>>> entries = list;
    Gson gson = new Gson();
    String jsonEntries = gson.toJson(entries);
    //Log.d("TAG", "jsonCars = " + jsonEntries);
    prefsEditor.putString(key1, jsonEntries);
    prefsEditor.commit();

}

这将加载BusEntrys

的ArrayList的ArrayList的ArrayList
private static Object getDataList(String key, int task){
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(RO.mainActivity.getApplicationContext());
    String jsonString = appSharedPrefs.getString(key, ""); // TODO use empty string as an if statement to determine if you need to recache
    Type type;
    type = new TypeToken<ArrayList<ArrayList<ArrayList<BusEntry>>>>(){}.getType();
    Gson gson = new Gson();
    return gson.fromJson(jsonString, type);
}

错误堆栈

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joltimate.umdshuttle/com.joltimate.umdshuttle.MainActivity}: com.google.a.aa: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 7 path $[0].b
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
   at android.app.ActivityThread.access$1000(ActivityThread.java:198)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6837)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: com.google.a.aa: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 7 path $[0].b
   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read()
   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write()
   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read()
   at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read()
   at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read()
   at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read()
   at com.google.gson.Gson.doubleAdapter()
   at com.google.gson.Gson.doubleAdapter()
   at com.google.gson.Gson.doubleAdapter()
   at com.joltimate.umdshuttle.Data.DataStorage.saveDataList()
   at com.joltimate.umdshuttle.Data.DataStorage.getAllData()
   at com.joltimate.umdshuttle.Data.DataStorage.saveDataList()
   at com.joltimate.umdshuttle.MainActivity.onCreate()

2 个答案:

答案 0 :(得分:0)

你有:

Type type;
type = new TypeToken<ArrayList<ArrayList<ArrayList<BusEntry>>>>(){}.getType();
Gson gson = new Gson();
return gson.fromJson(jsonString, type);

来自documentation

Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};

(Serialization)
gson.toJson(ints);     ==> prints [1,2,3,4,5]

(Deserialization)
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); 
==> ints2 will be same as ints

看起来你没有返回预期的类型。

我建议如下:

class SpecialArrayType{

    private ArrayList<ArrayList<BusEntry>> specialArrayType;
    public void setSpecialArrayType(ArrayList<ArrayList<BusEntry>> list){
        this.specialArrayType = list;
    }

    public ArrayList<ArrayList<BusEntry>> getSpecialArrayType(){
        return specialArrayType;
    }

}

// Get the array list as a special type, then deal with the nested lists.

ArrayList<SpecialArrayType> lists = new ArrayList<SpecialArrayType>();
Type type = new TypeToken<ArrayList<SpecialArrayType>>(){}.getType();
Gson gson = new Gson();
lists = gson.fromJson(jsonString, type);

ArrayList<ArrayList<ArrayList<BusEntry>>> returnList = new ArrayList<ArrayList<ArrayList<BusEntry>>>();

// Loop through special array list to populate info from nested array lists.
for(SpecialArrayType list1: lists){

    // We have these items in our outer array list, we want to add them to our
    // return list.
    returnList.Add(list1);

    // The next nested array list.
    list1 = new ArrayList<ArrayList<BusEntry>>();

    for(ArrayList<BusEntry> list2: list1){
        // Populate each specialArrayType with an array list of array list of BusEntries.
        list1.Add(list2);
        // Find each nested array list of bus entries.
        list2= new ArrayList<BusEntry>> ();

        for(BusEntry entry: list2){
            list2.Add(entry);
        }
    }

}       

return returnList;

所以你要返回完全嵌套的数据。

答案 1 :(得分:0)

在android studio项目中添加以下库,而不是将手动库放在库文件夹中。

 compile 'com.google.code.gson:gson:2.3.1'

如果您尝试使用GSON在共享偏好中存储arraylist或类对象,请尝试本教程:click here