Reputation: 2315
I was having some problem when trying to retrieve from Firebase. Here is my "Users" table in firebase:
I am trying to store like a user with multiple fields. Am I storing it wrong?
My 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;
}
The part when I try to retrieve the JSON from Firebase:
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());
}
});
}
});
However, I am getting these error messages:
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
Any ideas? Thanks in advance.
Upvotes: 1
Views: 831
Reputation: 598817
The error is explicitly mention in the stack trace:
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"])
Since your JSON doesn't have a property called gab
, it seems like you're simply reading the data at the wrong level in your JSON tree.
The solution is to attach your listener one level lower in the tree on the Users
node:
Firebase ref = new Firebase(Config.FIREBASE_URL);
//Value event listener for realtime data update
ref.child("Users").addValueEventListener(new ValueEventListener() {
...
Upvotes: 4
Reputation: 38289
Your Users
class needs a no-arg constuctor:
public Users() {
}
This is explained in the documentation.
Instead of dealing with the primitives types, we'll do what we did when saving users. We'll create a Java class that represents a Blog Post. Like last time, we have to make sure that our field names match the names of the properties in the Firebase database and give the class a default, parameterless constructor.
Upvotes: 2