FirebaseException:无法反弹以键入 Android,但模型 class 属性和 JSON 属性相同

FirebaseException: Failed to bounce to type in Android but Model class properties and JSON properties are same

我仍然遇到异常:

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) {

    }
  });

由于您正在侦听 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