预计BEGIN_ARRAY但是BEGIN_OBJECT

时间:2016-03-14 20:48:10

标签: java android gson android-sharedpreferences

我有以下用于管理共享首选项的类。这完全没问题,但现在我添加了两个方法getStoredPhotos()setStoredPhotos()以及变量STORED_PHOTOSstored_photos。现在我收到以下错误。 logCat引用的行就是这个followed_cities = session.getFollowedCities(); 但是在我添加我提到的代码之前这完全正常,我根本没有改变getFollowedCities()方法,因此我的困惑:

FATAL EXCEPTION: main
                                                                 Process: citylife.com.city, PID: 5583
                                                                 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
                                                                     at com.google.gson.Gson.fromJson(Gson.java:822)
                                                                     at com.google.gson.Gson.fromJson(Gson.java:775)
                                                                     at com.google.gson.Gson.fromJson(Gson.java:724)
                                                                     at citylife.com.city.SessionManager.getFollowedCities(SessionManager.java:68)
                                                                     at citylife.com.city.FragmentCTesting.onAttach(FragmentCTesting.java:114)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
                                                                     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846)
                                                                     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860)
                                                                     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1103)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
                                                                     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
                                                                     at android.view.View.measure(View.java:17590)
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
                                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                     at android.view.View.measure(View.java:17590)
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
                                                                     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                     at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                     at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                     at android.view.View.measure(View.java:17590)
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)
                                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748)
                                                                     at android.view.View.measure(View.java:17590)
                                                                     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)
                                                                     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174)
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5871)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5536)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)
                                                                  Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
                                                                     at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
                                                                     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
                                                                     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
                                                                     at com.google.gson.Gson.fromJson(Gson.java:810)
                                                                     at com.google.gson.Gson.fromJson(Gson.java:775) 
                                                                     at com.google.gson.Gson.fromJson(Gson.java:724) 
                                                                     at citylife.com.city.SessionManager.getFollowedCities(SessionManager.java:68) 
                                                                     at citylife.com.city.FragmentCTesting.onAttach(FragmentCTesting.java:114) 
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907) 
                                                                     at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846) 
                                                                     at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860) 
                                                                     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130) 
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1103) 
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:951) 
                                                                     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473) 
                                                                     at android.view.View.measure(View.java:17590) 
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
                                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
                                                                     at android.view.View.measure(View.java:17590) 
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
                                                                     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
                                                                     at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
                                                                     at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
                                                                     at android.view.View.measure(View.java:17590) 
                                                                     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
                                                                     at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
                                                                     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748) 
                                                                     at android.view.View.measure(View.java:17590) 
                                                                     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030) 
                                                                     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174) 
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395) 
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062) 
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5871) 
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:550) 
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
                                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5536) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192) 

public class SessionManager {
private static String TAG = SessionManager.class.getSimpleName();
SharedPreferences pref;

SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "Login";
private static final String KEY_IS_LOGGEDIN = "isLoggedIn";
public static final String USERNAME = "username";
public static final String MY_LIST = "my_list";
public static final String MY_LIST2 = "my_list";
private static final Type FOLLOWED_CITIES_TYPE = new TypeToken<ArrayList<CityShort>>() {}.getType();
public static final String EMAIL = "email";
private static final Type STORED_PHOTOS = new TypeToken<HashMap<String,ArrayList<ImageDisplayer>>>() {}.getType();
public static final String TESTSTRING = "test_string";
private ArrayList<CityShort> followed_cities = new ArrayList<>();
private HashMap<String, ArrayList<ImageDisplayer>> stored_photos = new HashMap<>();

public SessionManager(Context context) {
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}

public void setLogin(boolean isLoggedIn, String username, String email) {

    editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn);
    editor.putString(USERNAME, username);
    editor.putString(EMAIL, email);

    // commit changes
    editor.commit();

    Log.d(TAG, "User login session modified!");
}
public void logOut() {
    editor.putBoolean(KEY_IS_LOGGEDIN,false);
    editor.clear();
    editor.commit();
}

public void setTestString(String test) {
    editor.putString(TESTSTRING, test);
    editor.commit();
}
public ArrayList<CityShort> getFollowedCities() {
    followed_cities = new Gson().fromJson(pref.getString(MY_LIST, null), FOLLOWED_CITIES_TYPE);
    if (followed_cities == null) {
        followed_cities = new Gson().fromJson(pref.getString(MY_LIST, null), FOLLOWED_CITIES_TYPE);
        if(followed_cities == null){
            followed_cities = new ArrayList<CityShort>();
        }
    }
    return followed_cities;
}
public void setFollowedCities(ArrayList<CityShort> listwriter) {
    this.followed_cities = listwriter;
    editor.putString(MY_LIST, new Gson().toJson(listwriter));
    editor.commit();
}
public HashMap<String,ArrayList<ImageDisplayer>> getStoredPhotos() {
    stored_photos = new Gson().fromJson(pref.getString(MY_LIST2, null), STORED_PHOTOS);
    if (stored_photos == null) {
        stored_photos = new Gson().fromJson(pref.getString(MY_LIST2, null), STORED_PHOTOS);
        if(stored_photos == null){
            stored_photos = new HashMap<>();
        }
    }
    return stored_photos;
}
public void setStoredPhotos(String cityId, ArrayList<ImageDisplayer> images) {
    stored_photos.put(cityId,images);
    editor.putString(MY_LIST2, new Gson().toJson(stored_photos));
    editor.commit();
}
public HashMap<String, String> getTestString(){
    HashMap<String, String> user = new HashMap<String, String>();
    // user name
    user.put(TESTSTRING, pref.getString(TESTSTRING, null));

    // return user
    return user;
}
public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    // user name
    user.put(USERNAME, pref.getString(USERNAME, null));

    // return user
    return user;
}
public boolean isLoggedIn(){
    return pref.getBoolean(KEY_IS_LOGGEDIN, false);
}}

1 个答案:

答案 0 :(得分:0)

问题在于:

public static final String MY_LIST = "my_list";
public static final String MY_LIST2 = "my_list";

必须更改其中一个的名称,否则它们的名称都相同。