I know how to parse a simple DataSnapshot object to any Java class using public T getValue (Class<T> valueType)
. But after the Firebase 3.0 I am not able to parse the following data to my Java Class, as it contains a custom type instance for which I'm receiving NULL.
NOTE: The same logic was working fine before Firebase 3.0. I suppose its because now Firebase is using GSON instead of JACKSON. Please correct me if I'm 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 read:
Group group = datasnapshot.getValue(Group.class);
It doesn't cast LogoImage
part of the database into the logoimage
object. We always retrieve null in the logoimage
object.
getKey() returns the key (last part of the path) of the location of the Snapshot. getReference() returns the Reference for the location that generated this Snapshot. getValue() returns the data contained in this Snapshot. hasChild() returns true if the specified child path has (non-null) data.
public DataSnapshot child (String path)Get a DataSnapshot for the location at the specified relative path. The relative path can either be a simple child key (e.g. 'fred') or a deeper slash-separated path (e.g. 'fred/name/first'). If the child location has no data, an empty DataSnapshot is returned.
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.
public T getValue(Class valueType)
1.The class must have a default constructor that takes no arguments
2.The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Check it from: this source It'll help you
detail
I'm not sure why that is causing problem for you. This code works fine for me with the data you provided:
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) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With