2016-07-30 25 views
8

我尝试使用下面的代码坚持一个自定义对象: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

+3

运行'firebase-database:9.2.1'时无法重现异常。你使用不同的版本? –

+1

@qbix出于好奇:当你将一个'List '序列化到数据库时,你会得到什么JSON? –

+1

@FrankvanPuffelen:列表被序列化为一个数组。 [JSON在这里](https://gist.github.com/Bob-Snyder/034685d998fddd8d1bcc3d52c93d1877) –

回答

7

我已经设法找到导致此崩溃的原因。我安装另一台设备上的应用程序运行Android 5.x和火力地堡投掷了一个更令人困惑的异常有:

com.google.firebase.database.DatabaseException: No properties to serialize found on class android.graphics.Paint$Align 

看来,火力地堡(不像GSON)尝试序列,即使他们不”所有可能的干将与全局变量(类成员/域)直接相关,在我的具体情况下,我的一个对象包含一个返回Drawable - getDrawable()的方法。显然,Firebase不知道如何将drawable转换成json。

有趣的时刻(可能在火力地堡SDK中的错误):我运行旧设备上的Android 4.4.1我仍然得到我的原始异常在同一项目和配置:

Caused by: com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead 
+0

这是正确的答案..伟大 –

1

我有一个类似的问题,我试图使用groovy而不是Java,我想groovy正在为我生成其他get方法。我将我的领域类从波戈中(src /主/常规)到POJO的中(src /主/ JAVA),现在一切工作正常

0

添加@IgnoreExtraProperties到类

@IgnoreExtraProperties 
public class YourPojo { 
    public String name; 

    // Default constructor for Firebase 
    public YourPojo(){} 

    // Normal constructor 
    public YourPojo(String name) { 
     this.name = name; 
    } 
} 
1

添加到@exclude FB试图解释的功能,如返回Drawable的功能。

@Exclude 
public Drawable getUnitColorImage(){ 
    return TextDrawable.builder().buildRound("", prefColor); 
} 
0

下面是我如何解决这个问题。 当我试图更新数据库中的int字段时,发生了问题。我从编辑文本字段获取文本,并忘记将其转换为int。这导致了失败。因此,如果发生此问题,请尝试检查发送到数据库的数据类型。

1

如果这可以帮助其他人都收到了同样的错误,我得到了同样的错误和解决方案结束了对我下面的:

发生变化:

databaseReference.child("somechild").setValue(etName.getText()); 

要:

databaseReference.child("somechild").setValue(etName.getText().toString()); 

正如@fraggjkee指出的,Firebase尝试串行ize getters,这与在尝试序列化.getText()的结果的firebase时类似的问题。

希望这可以帮助别人!

相关问题