简体   繁体   中英

Basic Firebase / Java read/write

I'm feeling rather stupid at the moment. I have done quite a bit Android Studio/Java/Firebase coding before and am quite comfortable with it. Having just come back to it after a while off, I have updated everything to latest versions and I just cannot get my test app to talk to the database.

I have created a new Firebase project, downloaded the google-services file, used the Android Studio assistant - which tells me I'm connected - and used the simple read/write "hello world" code from the doc.

The code runs and I get the "read" output correctly in the log window, but I don't see the data on the Firebase console. If I then run the "read" code only I get nothing, so the "write" was only valid while the database reference was in scope it seems.

I've pretty much run out of things to change, so am very frustrated for something that should be so simple - and I may well be doing something stupid.

Any thoughts as to why this might be happening would be appreciated.

Android Studio 3.2.1, Firebase libs 16.0.4, SDK version 28, google services 4.2.0

Code is basically just taken straight from the doc.

TIA.

    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Message");

    // Write
    myRef.child("Monday").setValue("Hello, Lovely World!");

    // Read
    ValueEventListener listener;
    listener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            String key = dataSnapshot.getKey();

            Log.e(TAG, "Key is : " + key + " Value is: " + value);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.e(TAG, "Failed to read value.", error.toException());
        }
    };
    myRef.addValueEventListener(listener);

Basically you've done it right.

But remember that FirebaseDatabase.getInstance().getReference() representing the root of your database.

So you might wanna try myRef.child("sample_key").setValue("sample_value");

This will create a key-value pair under your database's root. You are trying to set a value to your database without give it a key, that makes the error.

And FYI, myRef.addValueEventListener(listener) means listen to the whole database.

myRef.child("sample_key").addValueEventListener(listener) means only listen to the value of sample_key .

And remember to call removeEventListener(listener) when you don't need it anymore.

https://firebase.google.com/docs/database/admin/save-data

  • Read the document carefully, it's using DatabaseReference usersRef = ref.child("users"); to save values to child, not the ref itself.

I have the same issue. I had to switch Firebase reference instead. My code looks something like:

Firebase myRef;

//On create method

myRef = new Firebase("insert url to firebase");

I don't know why, but I had use this reference as nothing else worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM