在检索时,Firebase无法跳转到键入

时间:2016-07-01 03:38:41

标签: java android json firebase firebase-realtime-database

尝试从Firebase检索时遇到了一些问题。这是我在firebase中的“用户”表:

我试图像具有多个字段的用户一样存储。我存储错了吗? enter image description here

我的Users.java:

public class Users {
public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public String getAnswer() {
    return answer;
}

public void setAnswer(String answer) {
    this.answer = answer;
}

private String userName;
private String password;
private String address;
private String phone;
private String email;
private String question;
private String answer;

}

尝试从Firebase检索JSON时的部分:

btnSignin.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Creating firebase object
            Firebase ref = new Firebase(Config.FIREBASE_URL);
            //Value event listener for realtime data update
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Users user = postSnapshot.getValue(Users.class);
                        System.out.println(user.getUserName()); //Testing

                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });
        }
    });

但是,我收到以下错误消息:

07-01 11:32:50.735 1025-1025/com.apps E/AndroidRuntime: FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to bounce to type
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
    at com.gabrielheng.zhapalangapps.Login$1$1.onDataChange(Login.java:49)
    at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4512)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "gab" (class Entity.Users), not marked as ignorable (7 known properties: , "address", "question", "userName", "password", "email", "answer", "phone"])
    at [Source: java.io.StringReader@416d8540; line: 1, column: 9] (through reference chain: Entity.Users["gab"])
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
    at com.gabrielheng.zhapalangapps.Login$1$1.onDataChange(Login.java:49) 
    at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45) 
    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 
    at android.os.Handler.handleCallback(Handler.java:605) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4512) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 
    at dalvik.system.NativeStart.main(Native Method) 
07-01 11:32:50.790 1025-1026/com.apps D/dalvikvm: GC_CONCURRENT freed 456K, 13% free 9663K/11015K, paused 21ms+7ms

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:2)

你的Users类需要一个无参数的构造函数:

public Users() {
}

the documentation中解释了这一点。

  

我们将完成我们所做的事情,而不是处理基元类型   保存用户时我们将创建一个代表Blog的Java类   帖子。像上次一样,我们必须确保我们的字段名称匹配   Firebase数据库中的属性名称和给出   class a default,无参数构造函数

答案 1 :(得分:2)

在堆栈跟踪中明确提到错误:

  

引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“gab”(类Entity.Users),未标记为可忽略(7个已知属性:,“address”,“question”,“userName “,”“密码”,“电子邮件”,“回答”,“电话”])

由于您的JSON没有名为gab的属性,因此您似乎只是在JSON树中以错误的级别读取数据。

解决方案是将您的侦听器附加到Users节点上树下一级:

Firebase ref = new Firebase(Config.FIREBASE_URL);
//Value event listener for realtime data update
ref.child("Users").addValueEventListener(new ValueEventListener() {
    ...