FirebaseException:无法退回以在Android中键入,但Model类属性和JSON属性相同

时间:2015-11-25 16:32:08

标签: android json firebase firebase-realtime-database

我还有例外:

com.firebase.client.FirebaseException: Failed to bounce to type exception 

以下行

Peoples people = dataSnapshot.getValue(Peoples.class);

人民反对

public class Peoples {

    private String ambition;
    private String fname;
    private String lname;
    private String emailid;
     Peoples(){

     }
     Peoples(String ambition,String fname,String lname,String emailid){
        this.ambition=ambition;
        this.fname=fname;
        this.lname=lname;
        this.emailid=emailid;
     }


     public String getAmbition() {
        return ambition;
     }

     public String getFname() {
        return fname;
     }

     public String getLname() {
        return lname;
     }

     public String getEmailid() {
        return emailid;
     }
}

数据

peoples  
 -K3yJU5z646EZiIliPuJ
   ambition: "Doctor"
   emailid: "harshan@gmail.com"
   fname: "Mohammed"
   lname: "Thanish"
 -K3yJcoLNqY228wQO1AX
   ambition: "Doctor"
   emailid: "ndlhassan@gmail.com"
   fname: "Mohammed"
   lname: "Thanish"

代码

userRef
  .orderByChild("emailid")
  .equalTo("ndlhassan@gmail.com")
  .addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      System.out.println("USERRRRRRRRRRRRRRRRRRR are " + dataSnapshot.getChildrenCount() + " blog posts");
      System.out.println("USERRRRRRRRRRRRRRRRRRR" + dataSnapshot.getValue());
      Peoples people = dataSnapshot.getValue(Peoples.class);
      System.out.println("USERRRRRRRRRRRRRRRRRRR" + people.getEmailid());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
  });

1 个答案:

答案 0 :(得分:2)

由于您正在收听value事件,因此可能会使用多个匹配项调用onDataChange方法。即使在您的情况下只有一个匹配,您的代码将需要准备好处理多个:

public void onDataChange(DataSnapshot dataSnapshot) {
  System.out.println("There are " + dataSnapshot.getChildrenCount() + " children");
  for (DataSnapshot child: dataSnapshot.getChildren()) {
      Peoples people = child.getValue(Peoples.class);
      System.out.println("Child: " + people.getEmailid());
  }
}

或者,您可以添加ChildEventListener。 https://www.firebase.com/docs/android/guide/retrieving-data.html