'Firebase sync problem with Android Studio app

I have a problem with Firebase: I would like to associate an app created with Android Studio, an archive created with the room class. I followed all the instructions, inserted the .json file, inserted all dependencies in the gradle, but when I insert a new item through my app, Realtime Database doesn't update. I have updated everything, also tried with a physical device, but nothing, it doesn't work.


I followed the wizard in Android Studio but I get this error when trying to connect to Firebase:

Connection failed

Firefox cannot establish a connection with the server localhost: 56495.

The site may be unavailable or overloaded. Try again in a few moments. If no pages can be loaded, check the computer's network connection. If your computer or network is behind a firewall or proxy, make sure Firefox has permissions to access the web.



Solution 1:[1]

If you have Android Studio, there is a very simple way of adding Firebase Functionality without all thoses steps (it worked for me)

Go to Android Studio, then Tools > Firebase > Realtime Database and follow the very simple steps. It will automatically download and setup everything you need automatically, you only have to log into Firebase when asked.

Then you can upload something with following code:

DatabaseReference myRef = database.getReference("/your_path/your_key");
myRef.setValue("your_value")

And if you want to get the updates in your app then following will do:

myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            // This will get called every time /your_path/your_key gets
            // changed so essetially every time you change your_value

            // do something. Here an example of how to get a String
            String str = snapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1