0

I don't know why it doesn't work. I was just making my own application with this. I used some from quickstart repository in firebase github. Also, I added System.out.println in the listener at private static void get but that doesn't work too. I think that listener was not executed.

Here's my code:

package me.JacobLim.test;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.*;
import org.apache.log4j.BasicConfigurator;

import java.io.FileInputStream;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        //Log4j Configurator
        BasicConfigurator.configure();

        try {
            System.out.println("Started Main");
            FileInputStream serviceAccount = new FileInputStream("Json Path");
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://<My Project :)>-default-rtdb.firebaseio.com")
                    .build();
            FirebaseApp.initializeApp(options);

            Thread.sleep(2000);

            System.out.println("Finished Init");
        } catch (IOException e) {
            System.out.println("ERROR: invalid service account credentials. See README.");
            System.out.println(e.getMessage());

            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("a");
        System.out.println(reference.getPath());

        get(reference);
    }

    private static void get(DatabaseReference reference) {
        System.out.println("Start get");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                System.out.println(dataSnapshot.getValue(String.class));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("ERROR");
            }
        });
    }
}

Here's log:

Started Main Finished Init 0

[main] DEBUG com.google.firebase.FirebaseApp - Starting the proactive token refresher 2

[main] DEBUG com.google.firebase.FirebaseApp - Scheduling next token refresh in 0 milliseconds 9

[firebase-scheduled-worker] DEBUG com.google.firebase.FirebaseApp - Refreshing OAuth2 credential

/a Start get

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jalim
  • 45
  • 6
  • 1
    Is your onDataChange even triggered? – Alex Mamo Feb 20 '22 at 14:11
  • Most likely your app exits before the data is loaded from Firebase. If that is the cause, adding another `Thread.sleep(10*1000);` right after `get(reference);` might help. Also see: https://stackoverflow.com/questions/31700830/is-it-possible-to-synchronously-load-data-from-firebase/31702957#31702957 – Frank van Puffelen Feb 20 '22 at 16:13
  • @FrankvanPuffelen Thank you for the comment. I managed to fix the problem referring to your answer. – Jalim Feb 21 '22 at 01:56

0 Answers0