Using Firebase Library to send data to the server in the form Message(String, String)
added to the HashMap<String, Message>
Example:
Firebase fb = new Firebase(URL); Firebase msgRef = fb.child("finished"); HashMap<String, Message> msgList = new HashMap<>(); Message msg = new Message(m, n); msgList.put(HASHKEY, msg); msgRef.push().setValue(msgList);
While receiving data with Firebase method addValueEventListener()
getting String in this Form
{ key = finished, value = { -Js9Rn0uttjYIGdcv8I1={Moosa={message=email, name=Kamran}}, -Js9Vsmv6BnVzOzpl2L8={Moosa={message=msgs, name=Imran}}, -Js9WtQ8yeDwVxQMFCZb={Moosa={message=samsung, name=Samad}}, -Js9RarxoJPKn4RO2HaM={Moosa={message=Message, name=Moosa}}, -Js9b6f75lwwbsqQNJz0={Moosa={message=Qmobile, name=Bilal}}, -Js9aDxt8TlgTGUccuxu={Moosa={message=last, name=Moosa}}} }
How can I convert it into Message Object.....????
The java support for Firebase is intended to extends its server side functionality by using Firebase's sdk. To query on Firebase realtime database or Firestore, you should write your android/ios app and/o through a web app written on javascript.
All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree. Unlike a SQL database, there are no tables or records. When you add data to the JSON tree, it becomes a node in the existing JSON structure with an associated key.
public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.
There are two more way to get your data out of the Firebase DataSnapshot
that don't require using a Map<String, Object>
.
First appoach is to use the methods of DataSnapshot
to traverse the children:
ref = FirebaseDatabase.getInstance().getReference("messages").limitToLast(10); ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) { String name = (String) messageSnapshot.child("name").getValue(); String message = (String) messageSnapshot.child("message").getValue(); } } @Override public void onCancelled(FirebaseError firebaseError) { } });
In the above snippet we use getChildren()
to get an Iterable
of your messages. Then we use child("name")
to get each specific child property.
The second approach is to use the built-in JSON-to-POJO serializer/deserializer. When you're sending the message list, the Message
objects inside it are serialized to JSON and stored in Firebase.
To get them out of it again, you have to do the inverse:
ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) { Message message = messageSnapshot.getValue(Message.class); } } @Override public void onCancelled(FirebaseError firebaseError) { } });
In this second snippet, we're still using getChildren()
to get at the messages, but now we deserialize them from JSON straight back into a Message
object.
For a simple sample application using that last approach, have a look at Firebase's AndroidChat sample. It also shows how to efficiently deal with the list of messages (hint: FirebaseListAdapter
).
So if you wanna get the messages you can do the following:
for (DataSnapshot child : dataSnapshot.getChildren()){ //child is each element in the finished list Map<String, Object> message = (Map<String, Object>)child.getValue(); Message msg = new Message((String) message.getValue().get("message"), (String) message.get("name")); }
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