我尝试使用下面的代码坚持一个自定义对象:com.google.firebase.database.DatabaseException:序列化数组不支持,请使用列表,而不是
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference curWorkoutExercisesRef = databaseReference.child("workouts")
.child(mCurrentWorkout.getId())
.child("workoutExercises");
WorkoutExercise we = new WorkoutExercise(exercise);
curWorkoutExercisesRef.push().setValue(we);
这是我的目标:
public class WorkoutExercise {
private String id;
private Exercise exercise;
public WorkoutExercise() {}
// getters, setters, other methods
// ...
}
public class Exercise {
private String id;
private String title;
private List<BodyPart> bodyParts;
public Exercise() {}
// getters, setters, other methods
// ...
}
public class BodyPart {
private String id;
private String name;
public BodyPart() {}
// getters, setters, other methods
// ...
}
而且每次我得到这个错误 - com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
。我的对象不包含任何数组,所以这个错误看起来很混乱。我找到了解决这个错误的方法 - 如果我将@Exclude
注释添加到我的Exercise
类的bodyParts
列表中,但一切正常,但显然不是我想要实现的。
看来Firebase无法坚持包含内部列表的对象?有没有简单的解决方法或最佳实践?谢谢!
P.S.我正在使用firebase-database:9.2.1
运行'firebase-database:9.2.1'时无法重现异常。你使用不同的版本? –
@qbix出于好奇:当你将一个'List'序列化到数据库时,你会得到什么JSON? –
@FrankvanPuffelen:列表被序列化为一个数组。 [JSON在这里](https://gist.github.com/Bob-Snyder/034685d998fddd8d1bcc3d52c93d1877) –