1

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?

2
  • what your ref object is referring to or how have you created? Commented Apr 1, 2016 at 17:23
  • actually @mithun I don't know why newUser.getStatus(), .getName() & .getProvider() are returning null values Commented Apr 1, 2016 at 18:29

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.