Firebase retrieve/read returns null values - Android

狂风中的少年 提交于 2019-12-23 02:43:09

问题


Using Firebase docs for retrieving data with:

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
}); 

Running the above produced:

{users={facebook:###############={posts={-KE8qg1VyPAwYj0uJDcn={status=update1}}, provider=facebook, displayName=Some Name}}}

Created class to represent users:

@JsonIgnoreProperties(ignoreUnknown=true) //to ignore mapping issues
public class Users {

String status;
String displayName;
String provider;

public Users() {
        // empty default constructor
}

public String getStatus() {
    return status;
}

public String getName(){
    return displayName;
}

public String getProvider(){
    return provider;
}
}

Using the following to attempt to retrieve values:

ref.addChildEventListener(new ChildEventListener() {
        // Retrieve new posts as they are added to the database
        @Override
        public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
            Users newUser = snapshot.getValue(Users.class);
            System.out.println("Status: " + newUser.getStatus());
            System.out.println("Display Name: " + newUser.getName());
            System.out.println("Provider: " + newUser.getProvider());
        }

Everything returns a null? Read over & tried several examples of using for loops to iterate over the database but those failed as well so I decided to come back to the first version. Not certain which way is best for this situation?


回答1:


Data is returned now. Realized in the end there would be too much nesting so now posts are in a separate node. Changed code from:

final Firebase userRef = rootRef.child("users/" + rootRef.getAuth().getUid());

to:

final Firebase userRef = rootRef.child("posts");


来源:https://stackoverflow.com/questions/36360779/firebase-retrieve-read-returns-null-values-android

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