简体   繁体   中英

Child List from Firebase with android

I'm trying to get the value of my firebase tree

在此处输入图片说明

I got a class get_categories

public class get_categories {
    private String Name;

    public get_categories(){

    }

    public String getName(){
        return Name;
    }
}

Then I declared the firebase

mRef = new Firebase("https://...../Users/Categories");

And try to retrieve my data with OnChildEventListener

mRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                Log.e("Count " ,""+dataSnapshot.getChildrenCount());
                //Log.e("VALUES " ,""+dataSnapshot.child("Details").getValue());
                get_categories getCatName = dataSnapshot.child("Details").getValue(get_categories.class);
                Log.e("NAME " ,""+getCatName.getName());
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

But I got an error

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Name" (class com.app.pierre.myapp.get_categories), not marked as ignorable (0 known properties: ]) at [Source: java.io.StringReader@9d0f705; line: 1, column: 10] (through reference chain: com.app.pierre.myapp.get_categories["Name"])

What am I doing wrong ?

I used this case as reference: https://www.firebase.com/docs/android/guide/retrieving-data.html

Well first change your Model like this,

public class GetCategories {
    private String Name;

    public GetCategories(){

    }

    public GetCategories(String name){
    this.Name = name;

    }

    public String getName(){
        return Name;
    }
}

and Push data using Model class Constructor..

GetCategories model = new GetCategories("yourValues");

mReference.child("Details").push().setValue(model);

and Now you can get it like this way..

mRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {

                GetCategories newPost = eventSnapshot.getValue(GetCategories.class);

                Log.e("NAME " ,""+ newPost.getName());   

            }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

The name of the property on your JSON starts with an uppercase letter. But by the rules of JavaBean properties, getName() correspond to a property called name (with a lowercase n). So the two don't match up.

One way to get this working is to not use a setter, but instead use a public field:

public class get_categories {
    public String Name;
}

With this you're bypassing the JavaBean logic and get a direct mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM