cannot retrieve data from realtime firebase database

I want to retrieve data from realtime firebase database into my android studio app. I am using TextView to display the data however it gives me an error. The data from firebase won't display in the TextView. can someone help me with this error..tqq

this is my error message

2021-06-09 03:01:09.992 11837-11837/com.fyp.airpollutiondetection E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.fyp.airpollutiondetection, PID: 11837
    com.firebase.client.FirebaseException: Failed to bounce to type
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
        at com.fyp.airpollutiondetection.reading$1.onDataChange(reading.java:30)
        at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
        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:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
     at [Source: [email protected]; 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

I also get this message

2021-06-09 03:00:57.215 11837-11837/? E/lutiondetectio: Unknown bits set in runtime_flags: 0x8000

This is my code

package com.fyp.airpollutiondetection;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class reading extends AppCompatActivity {
    private TextView nilai;
    private Firebase mRef;

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

        nilai = (TextView)findViewById(R.id.nilai);
        mRef = new Firebase("https://air-pollution-7917c-default-rtdb.firebaseio.com/CARBON");
        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String CARBON = dataSnapshot.getValue(String.class);
                nilai.setText(CARBON);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
    }
}

Answers 0

Related Articles