【发布时间】:2018-06-11 17:41:30
【问题描述】:
我正在关注instruction 来测试firebase 实时数据库,但是在保存数据后(运行下面的initialize() 和saveData() 方法)在控制台上看不到任何数据。 并且日志没有显示任何错误。 有什么问题?
public static void initialize() {
// Fetch the service account key JSON file contents
try {
FileInputStream serviceAccount = new FileInputStream(keyPath);
// Initialize the app with a service account, granting admin privileges
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://myprojectId.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveData() {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("test");
DatabaseReference usersRef = ref.child("vocabularies");
Map<String, Vocabulary> vocabularies = new HashMap<>();
Vocabulary v1 = new Vocabulary();
v1.setWord("hello");
v1.setDefinition("hello definition");
v1.setTranslation("hello translation");
Vocabulary v2 = new Vocabulary();
v2.setWord("world");
v2.setDefinition("world definition");
v2.setTranslation("world translation");
vocabularies.put("hello", v1);
vocabularies.put("world", v2);
usersRef.setValueAsync(vocabularies);
}
【问题讨论】:
-
您展示的功能是 Admin SDK 的一部分,而不是 Android。我看到这是用 Android 标记的,所以如果您尝试在 Android 应用程序中使用它,Alex 是正确的,该功能不存在。如果您尝试使用此服务器端,Louise 是正确的,程序在工作完成之前退出,您需要包含一些功能以保持线程处于活动状态,直到工作完成。
标签: android firebase firebase-realtime-database