我是使用Firebase和Android的新手。我有一个项目,可以在一个孩子中保存订单和付款,并在适配器中显示所有内容。但是从模型类中获取postSnapshot
的数据时会显示错误。我不知道我项目的错误在哪里。像这样的错误:
com.firebase.client.FirebaseException:无法退回键入
和
引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段"付款"
我的firebase结构如下:
然后这是我的java代码:
MainActivity.java
String status = "orderSucces";
Firebase ref = new Firebase("https://myfirebase-d8a8a.firebaseio.com/order");
orderID = "-Kxi37Ro2oCPxQkb5L5u";
Query query = ref.child(status).orderByChild("orderID").equalTo(orderID);
query.addValueEventListener(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
progressBar.setVisibility(View.VISIBLE);
if (dataSnapshot.exists()) {
for (com.firebase.client.DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
OrderModel data = postSnapshot.getValue(OrderModel.class);
orderModel.add(data);
adapter = new Adapter(getApplication(), orderModel);
//adding adapter to recyclerview
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
}
} else {
progressBar.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
OrderModel.java
public class PemesananModel implements Serializable {
public String orderID, paymentID, buyerName, buyerPhone, paymentMethod;
PemesananModel() {}
public PemesananModel(String orderID, String paymentID, String buyerName, String buyerPhone, String paymentMethod) {
this.orderID = orderID;
this.paymentID = paymentID;
this.buyerName = buyerName;
this.buyerPhone = buyerPhone;
this.paymentMethod = paymentMethod;
}
public String getOrderID() {
return orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
public String getPaymentID() {
return paymentID;
}
public void setPaymentID(String paymentID) {
this.paymentID = paymentID;
}
public String getBuyerName() {
return buyerName;
}
public void setBuyerName(String buyerName) {
this.buyerName = buyerName;
}
public String getBuyerPhone() {
return buyerPhone;
}
public void setBuyerPhone(String buyerPhone) {
this.buyerPhone = buyerPhone;
}
public String getPaymentMethod() {
return paymentMethod;
}
public void setPaymentMethod(String paymentMethod) {
this.paymentMethod = paymentMethod;
}
}
答案 0 :(得分:2)
由于您的JSON没有名为payment
的属性,因此您似乎只是在JSON树中以错误的级别读取数据。
解决方案是将您的侦听器附加到树中的正确级别,如下所示:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference paymentRef = rootRef
.child("order")
.child("orderSucces")
.child(orderID)
.child("payment");