java 的 Firebase admin sdk 无法连接
Firebase admin sdk with java doesn't connect
我正在尝试使用 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:无法加载 class "org.slf4j.impl.StaticLoggerBinder"。
SLF4J:默认为无操作 (NOP) 记录器实现
SLF4J:有关详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。
但有些事情告诉我,即使有这些错误,数据仍应保存到 firebase。
以下是 github link 代码和密钥 json。
https://github.com/bandarawaththa/Testing-Firebase-with-realtime-db.git
以下代码对我有用:
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials
.fromStream(new ClassPathResource("/firebase-authentication.json").getInputStream()))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (IOException e) {
e.printStackTrace();
}
“/firebase-authentication.json”在资源文件夹中。
我正在尝试使用 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:无法加载 class "org.slf4j.impl.StaticLoggerBinder"。
SLF4J:默认为无操作 (NOP) 记录器实现
SLF4J:有关详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。
但有些事情告诉我,即使有这些错误,数据仍应保存到 firebase。
以下是 github link 代码和密钥 json。
https://github.com/bandarawaththa/Testing-Firebase-with-realtime-db.git
以下代码对我有用:
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials
.fromStream(new ClassPathResource("/firebase-authentication.json").getInputStream()))
.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (IOException e) {
e.printStackTrace();
}
“/firebase-authentication.json”在资源文件夹中。