Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表

Java com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在尝试使用以下代码持久化自定义对象: DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference(); DatabaseReference curWorkoutExercisesRef = databaseReference.child("workouts") .child(mCurrentWorkout.getId()) .child("w

我正在尝试使用以下代码持久化自定义对象:

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 
    // ...
}
公开课练习{
私有字符串id;
私人健身;
公共练习(){}
//getter、setter和其他方法
// ...
}
公开课练习{
私有字符串id;
私有字符串标题;
私人物品清单;
公共演习(){}
//getter、setter和其他方法
// ...
}
公共级车身部件{
私有字符串id;
私有字符串名称;
公共BodyPart(){}
//getter、setter和其他方法
// ...
}
每次我收到此错误-
com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表
。我的对象不包含任何数组,因此此错误看起来非常混乱。我找到了修复此错误的方法-如果我在我的
练习
类中将
@Exclude
注释添加到
bodyParts
列表中,一切都会正常工作,但这显然不是我想要实现的

那么Firebase似乎无法持久化包含内部列表的对象?这里有什么简单的解决方法或最佳实践吗?谢谢


另外,我正在使用firebase数据库:9.2.1

我已经找到了导致这次崩溃的原因。我在另一台运行Android 5.x的设备上安装了该应用程序,Firebase抛出了一个不那么令人困惑的异常:

com.google.firebase.database.DatabaseException: No properties to serialize found on class android.graphics.Paint$Align
Firebase(不像Gson)试图序列化所有可能的getter,即使它们与全局变量(类成员/字段)没有直接连接,在我的特定情况下,我的一个对象包含一个返回
Drawable
-
getDrawable()
的方法。显然,Firebase不知道如何将drawable转换为json

有趣的时刻(可能是Firebase SDK中的一个bug):在我运行Android 4.4.1的旧设备上,我仍然在相同的项目和配置中得到我的原始异常:

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

我有一个类似的问题,我试图使用groovy而不是Java,我想groovy正在为我生成其他get方法。我将我的域类从Pogo(src/main/groovy)转换为Pojo(src/main/java),现在一切都正常运行了

@IgnoreExtraProperties
public class YourPojo {
    public String name;

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

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

将@Exclude添加到FB试图解释的函数中,比如返回可绘制的函数

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

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

如果这有助于其他接收到相同错误的人,我会收到相同的错误,解决方案如下:

更改:

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

databaseReference.child("somechild").setValue(etName.getText());
databaseReference.child("somechild").setValue(etName.getText().toString());
正如@fraggjkee所指出的,Firebase试图序列化getter,这与Firebase试图序列化
.getText()
的结果时遇到的问题类似

希望这能帮助别人

//这将由构造函数重新创建对象列表
//this will recreate your Object list by the constructor


private void handleData(){ 

    orderList = Db.getCarts();
    orders = new ArrayList<>();
    Double totalPrice = 0.0;

    for (Order order : orderList){
        // totalPrice = totalPrice + ( price * quantity )
        totalPrice += (Double.valueOf(order.getPrice())* Integer.valueOf(order.getQuantity()));
        orders.add(new Order(order.getProductId(),order.getProductName(),order.getQuantity(),order.getPrice(),order.getDiscount(),order.getImage()));
    }
}

 private void alertDialog() {

    AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
    alert.setTitle("One more step!");
    alert.setMessage("Enter your address");

    final EditText edtaddress = new EditText(getContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
    );

    edtaddress.setLayoutParams(layoutParams);
    alert.setView(edtaddress);
    alert.setIcon(R.drawable.ic_shopping_cart_black_24dp);

    alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Request request = new Request(
                    Common.currentUser.getPhone(),
                    Common.currentUser.getName(),
                    edtaddress.getText().toString(),
                    textTotal.getText().toString(),
                    orders
            );

            // Submit to firebase
            // We will using system.currentMillis to key
            requestReference.child(String.valueOf(System.currentTimeMillis()))
                    .setValue(request);
            //Delete cart
            Db.deleteCart();
            Toast.makeText(getContext(), "Thank you , Order Place", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        }
    });

    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    alert.show();

}
私有无效handleData(){ orderList=Db.getCarts(); orders=新的ArrayList(); 双倍总价=0.0; 对于(订单:订单列表){ //总价=总价+(价格*数量) totalPrice+=(Double.valueOf(order.getPrice())*Integer.valueOf(order.getQuantity()); 添加(新订单(Order.getProductId()、Order.getProductName()、Order.getQuantity()、Order.getPrice()、Order.Get折扣()、Order.getImage()); } } 私有void alertDialog(){ AlertDialog.Builder alert=新建AlertDialog.Builder(getContext()); 警报。setTitle(“多走一步!”); alert.setMessage(“输入您的地址”); 最终EditText-edtaddress=新的EditText(getContext()); LinearLayout.LayoutParams LayoutParams=新的LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_父级, LinearLayout.LayoutParams.MATCH\u父项 ); edtaddress.setLayoutParams(layoutParams); alert.setView(地址); 警报。设置图标(R.drawable.ic\购物车\黑色\ 24dp); alert.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ 请求=新请求( Common.currentUser.getPhone(), Common.currentUser.getName(), edtaddress.getText().toString(), textTotal.getText().toString(), 命令 ); //提交给firebase //我们将使用system.currentMillis键 requestReference.child(String.valueOf(System.currentTimeMillis())) .setValue(请求); //删除购物车 Db.deleteCart(); Toast.makeText(getContext(),“谢谢,订购地点”,Toast.LENGTH_SHORT.show(); getActivity().finish(); } }); alert.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){ @凌驾 public void onClick(DialogInterface dialog,int which){ dialog.dismise(); } }); alert.show(); }
当您使用hashmap设置值时,请使用此选项获取您正在上载的图像的url

task.getResult().getDownloadUrl().toString()

使用
firebase数据库9.2.1
运行时,我无法再现异常。您使用的是另一个版本吗?@qbix出于好奇:当您将
列表
序列化到数据库时,最终会得到什么JSON?@FrankvanPuffelen:列表被序列化为数组。在我的例子中,我从一个可编辑的字段中获取数据,并试图直接推送它。将其转换为字符串()有效