无法在Java Web应用程序中保存Firebase实时数据库数据

时间:2018-12-17 09:37:45

标签: java firebase firebase-realtime-database

我正在尝试使用主要方法将Java类中的数据保存在Firebase实时数据库中。但是它没有用。我按照firebase网站上的说明进行操作,但是实时数据库的数据没有一次更新。并且没有错误消息。我不知道为什么。

规则已经设置。

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

以下是我的代码,请给点建议,非常感谢!

public class Firebasetest {

    private static DatabaseReference database;

    public static void main(String[] args) throws InterruptedException {
      // Initialize Firebase
      final Semaphore semaphore = new Semaphore(0);
      FirebaseApp defaultApp = null;
      try {
        // [START initialize]
        FileInputStream serviceAccount = new FileInputStream("test-firebase.json");

        FirebaseOptions options =
            new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://test.firebaseio.com")
                .build();
        FirebaseApp.initializeApp(options);
        System.out.println("init");
        // [END initialize]
      } catch (IOException e) {
        System.out.println(e.getMessage());
        System.out.println("ERROR: invalid service account credentials. See README.");
        System.out.println(e.getMessage());
      }

      try {

        database = FirebaseDatabase.getInstance().getReference();

        ApiFuture<Void> future = database.setValueAsync("1234");
        try {
          //        future.get();
          future.get(20, TimeUnit.SECONDS);
        } catch (ExecutionException e) {
          e.printStackTrace();
        } catch (TimeoutException e) {
          e.printStackTrace();
        }

        final CountDownLatch latch = new CountDownLatch(1);

        database.setValue(
            "234343",
            new DatabaseReference.CompletionListener() {
              @Override
              public void onComplete(
                  DatabaseError databaseError, DatabaseReference databaseReference) {
                latch.countDown();
              }
            });
        System.out.println("done");
        latch.await();

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

0 个答案:

没有答案