Connect to Firebase from spring boot and read callback method not executing

tan90 Source

I am trying to connect to firebase DB from the Spring Boot application using below code. But call back method is not executing and it never prints either in Success or Fail.

public void helper(){
    FileInputStream fileInputStream = null;

    try {
        // Fetch the service account key JSON file contents
        File file = new ClassPathResource("XXXXXXXX.json").getFile();
        fileInputStream = new FileInputStream(file);


        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(fileInputStream))
                .setDatabaseUrl("https://XXXXX.firebaseio.com")
                .build();
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("/XXX/status");

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String res = dataSnapshot.getValue().toString();
                System.out.println(res);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                String res = databaseError.toString();
                System.out.println(res);
            }
        });
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        if (fileInputStream != null) {
            //fileInputStream.close();
        }
    }
}   

Can you please let me know if I am doing anything wrong or something I need to change. I appreciate your help.

androidfirebasespring-bootfirebase-realtime-databasefirebase-storage

Answers

comments powered by Disqus