我可以通过在 Firebase 控制台中导入 json 文件来更新整个数据库(真实数据库):
如何从服务器端以编程方式执行此操作(使用 Firebase Admin)?
I tried
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: { ...
解决了:
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)