【发布时间】:2019-12-27 13:50:10
【问题描述】:
我正在尝试使用 firebase 数据库来创建 java 桌面 RMI 数据管理系统。我的第一步是使用 admin sdk 确认 firebase 的连接性。我完全按照指南中的步骤进行操作,但没有任何进展。程序执行,但我的 firebase 控制台中没有数据修改。
以下是我到目前为止的代码..
public class Main{
public static class User2 {
public String date_of_birth;
public String full_name;
public String nickname;
public User2(String dateOfBirth, String fullName) {
this.date_of_birth = dateOfBirth;
this.full_name = fullName;
}
public User2(String dateOfBirth, String fullName, String nickname) {
this.date_of_birth = dateOfBirth;
this.full_name = fullName;
this.nickname = nickname;
}
}
public static void main(String[] args) {
FileInputStream serviceAccount = null;
try {
serviceAccount = new FileInputStream("rathnapuralabs-firebase-adminsdk-okzic-f6313557b4.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FirebaseOptions options = null;
try {
options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://rathnapuralabs.firebaseio.com")
.build();
} catch (IOException e) {
e.printStackTrace();
}
FirebaseApp.initializeApp(options);
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference();
DatabaseReference usersRef = ref.child("users2");
Map<String, User2> users = new HashMap<>();
users.put("alanisawesome", new User2("June 23, 1912", "Alan Turing"));
users.put("gracehop", new User2("December 9, 1906", "Grace Hopper"));
usersRef.setValueAsync(users);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object document = dataSnapshot.getValue();
System.out.println(document);
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
这段代码给了我三个 3 行的错误。
SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。
SLF4J:默认为无操作 (NOP) 记录器实现
SLF4J:请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder 了解更多详情。
但有件事告诉我,即使出现这些错误,数据仍应保存到 firebase。
以下是代码和密钥 json 的 github 链接。
https://github.com/bandarawaththa/Testing-Firebase-with-realtime-db.git
【问题讨论】:
-
错误是sl4j logger配置不当,与实际操作无关,暂时可以忽略
-
@Kushan 我也是这么想的……谢谢
标签: java database firebase firebase-realtime-database google-admin-sdk