0

I am trying to save data on Firebase from Java class with a main method. But it doesn't works.

I read a lot of posts but I can't get a solution.

My code is:

   public static void main(String[] args) throws IOException {

    // Init...
    // 
    //

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

    User user = new User("mariano");

    CountDownLatch done = new CountDownLatch(1);

    ref.child("users").child("3").setValue(user, new 
    DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError de, DatabaseReference dr) {
            done.countDown();
        }
    });
    try {
        done.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(new App().getGreeting());
}

UPDATE:

If I try with this code:

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

    Map<String, User> users = new HashMap<>();

    User user = new User("juan");
    users.put("user", user);

    CountDownLatch done = new CountDownLatch(1);

    ref.child("users").setValueAsync(users, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError de, DatabaseReference dr) {
            System.out.println("COMPLETEEEE");
            done.countDown();
        }
    });

    try {
        done.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

The code throws this error:

 Exception in thread "main" com.google.firebase.database.DatabaseException: 

 Failed to parse node with class class prueba.App$1

Some suggest? Help?

Regards

Emiliano

0 Answers0