Daniel CH
Daniel CH

Reputation: 27

I cannot retrieve my data from Firebase

This is my Firebase tree:

package com.example.android.frep;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class fHome extends AppCompatActivity {

List<resepNusantara> listResepNusantara;
ListView listViewResep;

DatabaseReference dbResepNusantara;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_f_home);
    listViewResep = (ListView) findViewById(R.id.resepList);
    dbResepNusantara = FirebaseDatabase.getInstance().getReference("resepNusantara");
}


@Override
protected void onStart() {
    super.onStart();

    dbResepNusantara.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clear the list
            //listResepNusantara.clear();

            //iterating all nodes
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                //getting resep
                //This is the ERROR START
                resepNusantara resepN = postSnapshot.getValue(resepNusantara.class);
                //adding resep to the list
                listResepNusantara.add(resepN);
            }

            //creating the adapter for the list
            ResepList resepListAdapter = new ResepList(fHome.this, listResepNusantara);
            //attaching adapter to the listView
            listViewResep.setAdapter(resepListAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

}

Hi, everyone I want to ask, i have a problem to retrieve data from firebase

In my model class which is resepNusantara, I've tried to change my properties type to Long but it stills error. The error line is on comment

I follow this guidelines: https://www.simplifiedcoding.net/firebase-realtime-database-crud/

This is the LogCat:

10-24 20:35:14.002 18631-18745/com.example.android.frep D/libc-netbsd: [getaddrinfo]:  hostname=frep-ffacb.firebaseio.com;  servname=(null);  app_pid=18631;  app_uid=10161;  ai_flags=1024;  ai_family=0; ai_socktype=1; netid=0; mark=0; from prox result 0
10-24 20:35:14.521 18631-18745/com.example.android.frep E/NativeCrypto: ssl=0x7f93ff4880 cert_verify_callback x509_store_ctx=0x7f6d5889f0 arg=0x0
10-24 20:35:15.610 18631-18631/com.example.android.frep E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.android.frep, PID: 18631
  com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
      at com.google.android.gms.internal.zzbqi.zzaD(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
      at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
      at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
      at com.example.android.frep.fHome$1.onDataChange(fHome.java:56)
      at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
      at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
      at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:815)
      at android.os.Handler.dispatchMessage(Handler.java:104)
      at android.os.Looper.loop(Looper.java:238)
      at android.app.ActivityThread.main(ActivityThread.java:6006)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)

Upvotes: 0

Views: 608

Answers (1)

James Poag
James Poag

Reputation: 2380

There is a data type mismatch when marshaling the resepNusantara class. Specifically, one of the members is stored as a long in the database and string in the class.

Dump the object to the console and inspect the database to make sure the types are all synced.

See this post for more information: Firebase DatabaseException: Failed to convert value of type java.lang.Long to String

Upvotes: 1

Related Questions