Analysis from the DataSnapshot class in Java in Firebase using getValue ()

I know how to parse a simple DataSnapshot object with any Java class using public T getValue (Class<T> valueType). But after Firebase 3.0, I cannot parse the following data in a Java class, since it contains an instance of a custom type, for which I get NULL.

NOTE. The same logic worked fine before Firebase 3.0. I suppose because now Firebase uses GSON instead of JACKSON. Please correct me if I am wrong

DATA:

{ 
  "address" : "DHA karachi", 
  "addresstitle" : "DHA karachi", 
  "logoimage" : {
    "bucketname" : "test2pwow",
    "id" : "zubairgroup",
    "mediaType" : "image/png",
    "source" : 1,
    "url" : "https://pwowgroupimg.s3.amazonaws.com/zubairgroup1173.png?random=727" 
  },
  "title" : "zubairghori" 
}

Group.java

public class Group {

    public String address;
    public String addresstitle;
    public LogoImage logoimage;

    public Group(){}

}

LogoImage.java

public class LogoImage {

    public String bucketname;
    public String id;

    public LogoImage(){}

}

Code that reads:

Group group = datasnapshot.getValue(Group.class); 

It does not drop part of the database LogoImageinto the object LogoImage. We always return zero to the object LogoImage.

+4
4

enter image description here public T getValue ( valueType )

1. ,

2. . ,

:

+8

, . :

    DatabaseReference ref = database.getReference("37830692");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Group group = dataSnapshot.getValue(Group.class);
            System.out.println(group);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Screenshot displayed in the debugger

+7

. , , , setter. , .

+2

I had the same problem and it was solved, making sure that the constructor arguments are written in the same way as the elements stored in Firebase. My mistake was that I installed a Firebase key with uppercase letters and object arguments in lowercase letters.

+1
source

All Articles