0

I'm trying to add firebase database to my android studio project. when I'm running my app (on emulator) I'm getting the following error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.inbar.amit.ksharet, PID: 2778
              com.firebase.client.FirebaseException: Failed to bounce to type
                  at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
                  at com.inbar.amit.ksharet.updates$1.onChildAdded(updates.java:52)
                  at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
                  at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                  at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
               at [Source: java.io.StringReader@2729553; line: 1, column: 1]
                  at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
                  at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:46)
                  at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
                  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
                  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
                  at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
                    ... 11 more

that's my code(for reading data from the database):

public class updates extends AppCompatActivity {

    private Firebase mRef;
    private ArrayList<String> messages = new ArrayList<>();
    Random rnd = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updates);

        mRef = new Firebase("https://ksharet-d6a24.firebaseio.com/message");

        // Get ListView object from xml
        final ListView listView = (ListView) findViewById(R.id.ListView);

        // Create a new Adapter
        final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, messages);

        // Assign adapter to ListView
        listView.setAdapter(adapter);

        mRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String message = dataSnapshot.getValue(String.class);
                Log.v("E_CHILD_ADDED", message);
                messages.add(0, message);
                adapter.notifyDataSetChanged();
            }

            @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) {
            }
        });
    }
}

what should I do to fix it?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
A. Inbar
  • 80
  • 14
  • What does your database look like? What's at the "https://ksharet-d6a24.firebaseio.com/message" location? Failed to bounce is an error from from the Jackson library that Firebase uses for object serialization and deserialization. More information about that is here: http://stackoverflow.com/questions/32108969/why-do-i-get-failed-to-bounce-to-type-when-i-turn-json-from-firebase-into-java?rq=1 – Lyla Oct 25 '16 at 01:50
  • @Lyla my database is a real time database, that's the json of the database: '{ "message" : { "-KUTSl1Z-UPLM0lst2OL" : { "message" : "שלום וברוכים הבאים לאפליקציה החדשה של בית הספר ״יגאל אלון״", "title" : "שלום" } } }' – A. Inbar Oct 25 '16 at 08:34

1 Answers1

0

I believe if you use String message = dataSnapshot.child("message").getValue(String.class); it should work. Why? Well the structure of your data is like this:

 { "message" : // Listener attached here @ https://ksharet-d6a24.firebaseio.com/message
    { "-KUTSl1Z-UPLM0lst2OL" : // child here
        { 
            "message" : "שלום וברוכים הבאים לאפליקציה החדשה של בית הספר ״יגאל אלון״", 
            "title" : "שלום" 
        } 
    } 
 }

When you call dataSnapshot.getValue(String.class) it's trying to turn this:

    { 
        "message" : "שלום וברוכים הבאים לאפליקציה החדשה של בית הספר ״יגאל אלון״", 
        "title" : "שלום" 
    } 

into a String, which it is not (it's an object with two values, a message and a title). If you use the child method, you can get the message child of that object which is a String.

Note that you can also have the object deserialize into a POJO, if you make a class like this:

public class Message {

    public String message;
    public String title;

    public Message() {
        // Default constructor required for calls to DataSnapshot.getValue(Message.class)
    }

    public User(String message, String title) {
        this.message = message;
        this.title = title;
    }

}

And then have your ArrayList store message objects and use getValue(Message.class)

Lyla
  • 2,767
  • 1
  • 21
  • 23