4

I have no idea how i can render this JSON on logcat :enter image description here

The source code to get data from firebase realtime database :

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("hotel");

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String value = dataSnapshot.getValue(String.class);
        Log.d(TAG, "Value is: " + value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        Log.w(TAG, "Failed to read value.", error.toException());
    }
});

It says error like this :

com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String

I've tried some tutorial, but i can't find one. Also, i try this :

HashMap value = DataSnapshot.getValue(HashMap.class);

It ended up error as well. How should i render this HashMap data ?

2
  • What does myRef point to? Commented Nov 28, 2017 at 16:37
  • it is database reference, you can see it on my question. i've just edited it. Commented Nov 29, 2017 at 2:28

2 Answers 2

21

You are getting HashMap of String as key and Object as Value so map it into something like below.

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
             Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
            Log.d(TAG, "Value is: " + map);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });

or we can get this parsed into some Model instead of an object.

update use this to parse

 Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
Sign up to request clarification or add additional context in comments.

4 Comments

I still got error : com.google.firebase.database.DatabaseException: Class java.util.HashMap has generic type parameters, please use GenericTypeIndicator instead
Then I need you Json what you have posted in the string
i just want to read data from database. I didn't include post yet.
it's hard to tell what model you should follow without knowing which parent tag you are getting as database reference in code and full json
3

Assuming that the hotel node is a direct child of your Firebase root, to get those values, please use the following code

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference hotelRef = rootRef.child("hotel");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String address = ds.child("address").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", address + " / " + name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
hotelRef.addListenerForSingleValueEvent(eventListener);

The output will be:

Surubaya / Hotel Suridan
Bandung / Hotel Santika
//and so on

2 Comments

Thank you, i will try it later
Ok and keep me posted.

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.