无法在Firebase数据库中读取数据类型对象(反序列化时期望列表,但获得类java.lang.String)

时间:2017-02-24 01:50:40

标签: android firebase firebase-realtime-database

我的Firebase数据库结构:

-- users:

------ Id:

--------- studentInfo:

我希望得到一个studentInfo对象,然后获取favList

        String userUid = mFirebaseUser.getUid();
    studentInfoRef = mDatabase.child("users").child(userUid).child("studentInfo");

    studentInfoRef.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            StudentInfo student = dataSnapshot.getValue(StudentInfo.class);

            List<String> mFavRest = student.getFavRestaurants();

            if (mFavRest.contains(restaurantUId)) {
                //do something
            } else {
                mFavRest.add(restaurantUId);

                studentInfoRef.child("favRestaurants").setValue(mFavRest);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });

但是当我试图在

获得studentInfo时
StudentInfo student = dataSnapshot.getValue(StudentInfo.class);

我收到此错误:

com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.lang.String

完整的堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.unifood, PID: 30540
              com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.lang.String
                  at com.google.android.gms.internal.zzaln.zza(Unknown Source)
                  at com.google.android.gms.internal.zzaln.zza(Unknown Source)
                  at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzaln$zza.zze(Unknown Source)
                  at com.google.android.gms.internal.zzaln$zza.zzcc(Unknown Source)
                  at com.google.android.gms.internal.zzaln.zzd(Unknown Source)
                  at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzaln.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.example.unifood.activities.RestaurantActivity$7.onDataChange(RestaurantActivity.java:240)
                  at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                  at com.google.android.gms.internal.zzaie.zza(Unknown Source)
                  at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
                  at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
                  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:5290)
                  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:911)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)

我的StudentInfo课程:

public class StudentInfo {


private String campusId;
private List<String> favRestaurants;
private List<String> favProducts;

public StudentInfo() {
    this.favProducts = new ArrayList<String>();
    this.favRestaurants = new ArrayList<String>();
}

public StudentInfo(String campusId) {
    this();
    this.campusId = campusId;
}

// getters and setters
}

更新

我在firabase-database上清理了所有用户,创建了一个新用户进行测试并且运行良好:)

0 个答案:

没有答案