0

I'm working on a iot project and I want to transfer data from firebase to my android app's textview help me out guys!!

This is my code

code

Firebase JSON

{
  "Water_level" : 10,
  "valve_1" : 0,
  "valve_2" : 0
}

This is my logcat error

10-07 13:49:26.433 25077-25077/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.blogspot.techyfruit360.watercontroller, PID: 25077
                                                   com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
                                                       at com.google.android.gms.internal.zzelw.zzb(Unknown Source:663)
                                                       at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
                                                       at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                                                       at com.blogspot.techyfruit360.watercontroller.Main2Activity$1.onDataChange(Main2Activity.java:40)
                                                       at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
                                                       at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
                                                       at com.google.android.gms.internal.zzeig.run(Unknown Source:65)
                                                       at android.os.Handler.handleCallback(Handler.java:790)
                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6518)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
2
  • Are you getting the value in log? Oct 6, 2018 at 19:49
  • 2
    Please replace the links to image with the actual text from those images. You can then format it with the controls in the Stack Overflow editor. You can get the JSON as text by clicking the "Export JSON" link in your Firebase Database console. Oct 6, 2018 at 19:55

2 Answers 2

1

in onDataChange delete everything you have written an try the following

myRef.addListenerForSingleValueEvent( new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot){
        if(dataSnapshot.exists()){
            Map<String, Object> objectMap = (HashMap<String, Object>) dataSnapshot.getValue();
            String value = (String) objectMap.get( "Water_level" );
            Log.d("Water_level", "Value is: " + value);
            textView.setText(value);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
} );

This should solve your issue. Make sure your Database ref is set correctly.

0

Failed to convert value of type java.lang.Long to String

Inside your onDataChange(),

String value = datasnapshot.getValue(String.class)

It's converting a long value to String that's why you're getting the error.

Try this instead:

String waterLevel = dataSnapshot.child("Water_level").getValue(String.class);
textView.setText(waterLevel)

Or just use toString().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.