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

情到浓时终转凉″ 提交于 2019-12-24 14:15:09

问题


I'm still getting an exception:

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

for following line

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

Peoples object

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;
     }
}

Data

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

Code

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:


Since you're listening to a value event, the onDataChange method can potentially be called with multiple matching items. Even though in your case there is only one match, your code will need to be prepared to handle multiple:

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());
  }
}

Alternatively, you can add a ChildEventListener. https://www.firebase.com/docs/android/guide/retrieving-data.html



来源:https://stackoverflow.com/questions/33921727/firebaseexception-failed-to-bounce-to-type-in-android-but-model-class-propertie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!