Я пытаюсь сохранить данные в базе данных realetime Firebase из класса Java с помощью метода main.Но это не работает. Я следовал инструкциям на веб-сайте 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();
}
}
}