Импорт файла json из SDK Firebase Admin - PullRequest
0 голосов
/ 01 июня 2019

Я могу обновить всю базу данных (Real Database), импортировав файл json в консоль Firebase:

enter image description here

Как я могу сделать это программно со стороны сервера (с Firebase Admin)?

Я пытался

private void uploadFirebaseDatabaseFile(JsonObject jsonObject) {
    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.setValue(jsonObject, (error, ref1) -> System.out.println(error + " " + ref1));
}

Но это бросает java.lang.RuntimeException: java.lang.reflect.InvocationTargetException ... Caused by: java.lang.IllegalStateException: Not a JSON Primitive: { ...

1 Ответ

0 голосов
/ 01 июня 2019

Решено:

private void uploadFirebaseDatabaseFile(Gson gson, JsonObject jsonObject) throws Exception {
    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    CountDownLatch done = new CountDownLatch(1);
    // we must convert our JsonObject and its all nested children to Map<String, Object>
    ref.setValue(gson.fromJson(jsonObject, Map.class), (error, ref1) -> {
            System.out.println(error + " " + ref1);
        done.countDown();
    });
    done.await();
}
...