0

I'm trying make desktop app for editing firebase database. I'm making it in java language but I can't make it work with database.

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.auth.FirebaseCredentials;

public class MainClass {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        FileInputStream serviceAccount = new FileInputStream("adminkey.json");


        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl("https://<database name>.firebaseio.com")
                .build();
        FirebaseApp app = FirebaseApp.initializeApp(options);

        System.out.println(app.getName());

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                System.out.println("Database works");
                Iterable<DataSnapshot> td = dataSnapshot.getChildren();
                if(dataSnapshot.hasChildren())
                    System.out.println("Have childrens");
                for(DataSnapshot key : td){
                String document = key.getKey();
                System.out.println(document);
                }
            }

            @Override
            public void onCancelled(DatabaseError arg0) {
                // TODO Auto-generated method stub
                System.out.println("Cancel");
            }
        }); 
    }    
}

When I run this code it didnt write to console "Database works" or "Canceled". It never go inside onDataChange() or onCanceled().

I get this code from https://firebase.google.com/docs/database/admin/start

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Šimon Marko
  • 69
  • 1
  • 11
  • 2
    Most likely your program simply exits before it's had a chance to read from the database. See http://stackoverflow.com/questions/31700830/is-it-possible-to-synchronously-load-data-from-firebase/31702957#31702957 and http://stackoverflow.com/questions/37098006/firebase-with-java-non-android-retrive-information/37100794#37100794 – Frank van Puffelen Apr 04 '17 at 23:53
  • Thank you so much. Thread.sleep() works. But it will be nice put this information to admin documentation on firebase webpage. – Šimon Marko Apr 05 '17 at 06:55
  • Good idea. Can you click the Send Feedback link on the page where you'd expect that? – Frank van Puffelen Apr 05 '17 at 14:21

0 Answers0