我正在尝试使用Java编写的桌面应用程序将数据保存到Firebase。但是,由于某些原因它不起作用,我一直在遵循这里提供的文档:https://firebase.google.com/docs/database/admin/save-data。我找不到任何关于此问题的视频教程,所有视频都与Web或移动相关。基本上,我想创建一个名为Venda的对象,该对象具有3个属性(ID、data、valor),然后将其保存到Firebase中。谢谢!
以下是主要方法:
Firebase连接:
以下是数据库的快照:
但是 onComplete 方法从未执行,程序也从未结束。至于在此提出的建议:java Firebase: delay exit until writes finish 也可能已被弃用,因为似乎 CompletionListener() 不再存在于 Firebase 中,但我可以在 DatabaseReference 中找到它,所以我将代码修改为以下内容:
与之前一样,onComplete方法从未被执行,程序也从未完成或将数据设置到数据库中。这是我正在尝试修改的帖子的快照:
这是数据库的快照,我试图更改的值是帖子ID为“adad2131”的作者字段。 我已经尝试使用“posts/adad2131/author”,“/posts/adad2131/author”或者仅仅是“adad2131/author”来引用该字段,也许我引用方式不正确吗?
以下是完整的代码:
以下是主要方法:
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("--------- Begin of Output ---------");
Firebase firebase = new Firebase();
VendasCRUD venda = new VendasCRUD(firebase);
venda.createVenda(new Venda(0, "23/05/1993", 45.67));
System.out.println("----------- End of Output -----------");
}
}
Firebase连接:
public class Firebase {
public Firebase() throws FileNotFoundException, IOException {
FileInputStream serviceAccount = new FileInputStream("C:\\Users\\Alex\\Documents\\NetBeansProjects\\SEMS\\src\\main\\java\\br\\com\\sems\\firebase\\sems-firebase.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://sems-firebase.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
}
public DatabaseReference getDataBaseReference() {
return FirebaseDatabase.getInstance().getReference();
}
}
销售类:
public class Venda {
public int ID;
public String data;
public double valor;
public Venda(int ID, String data, double valor) {
this.ID = ID;
this.data = data;
this.valor = valor;
}
@Override
public String toString() {
return ID + "," + data + "," + valor;
}
}
数据库信息:
目前规则已设置为公开,用于测试目的。
更新:
好的,在尝试了FrankvanPuffelen评论中提出的建议后,我仍然无法保存或读取Firebase中的数据。我编写了一个简单的测试代码。以下是代码:
public class Main {
private static final String DATABASE_URL = "https://sems-firebase.firebaseio.com/";
private static DatabaseReference database;
private static boolean finished = false;
public static void startListeners() {
database.child("posts").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Post post = dataSnapshot.getValue(Post.class);
System.out.println(post);
finished = true;
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
while(!finished);
}
public static void main(String[] args) throws FileNotFoundException {
try {
FileInputStream serviceAccount = new FileInputStream("sems-firebase.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp defaultApp = FirebaseApp.initializeApp(options);
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance(defaultApp);
System.out.println(defaultDatabase.getReference().toString());
} catch (IOException e) {
System.out.println("ERROR: invalid service account credentials. See README.");
System.out.println(e.getMessage());
System.exit(1);
}
// Shared Database reference
database = FirebaseDatabase.getInstance().getReference();
// Start listening to the Database
startListeners();
}
}
所以我一直在做的就是,运行代码然后转到数据库网址并手动编辑帖子,我期望在控制台上得到我刚刚编辑的帖子的打印输出,但是什么也没有发生。以下是数据库的快照:
![enter image description here](https://istack.dev59.com/4U7q0.webp)
更新2
我认为这里提出的建议:为什么Firebase Java SDK无法在设置后终止?已经过时,因为如果我尝试使用以下代码:
我尝试将它修改为这样:
public static void startListeners() throws InterruptedException {
DatabaseReference database = FirebaseDatabase.getInstance().getReference("posts/adad2131/author");
CountDownLatch done = new CountDownLatch(1);
database.setValue("Test", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError de, DatabaseReference dr) {
done.countDown();
}
});
done.await();
}
但是 onComplete 方法从未执行,程序也从未结束。至于在此提出的建议:java Firebase: delay exit until writes finish 也可能已被弃用,因为似乎 CompletionListener() 不再存在于 Firebase 中,但我可以在 DatabaseReference 中找到它,所以我将代码修改为以下内容:
private static final String DATABASE_URL = "https://sems-firebase.firebaseio.com/";
public static void startListeners() throws InterruptedException {
DatabaseReference database = FirebaseDatabase.getInstance().getReference("posts/adad2131/author");
final AtomicBoolean done = new AtomicBoolean(false);
database.setValue("Test", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError de, DatabaseReference dr) {
done.set(true);
}
});
while (!done.get());
}
与之前一样,onComplete方法从未被执行,程序也从未完成或将数据设置到数据库中。这是我正在尝试修改的帖子的快照:
更新 3
为了方便使用,我简化了代码。为了确保,我从项目的服务帐户面板生成了一个新的私钥,这是我初始化应用程序的方法:
try {
FileInputStream serviceAccount = new FileInputStream("sems-firebase-firebase-adminsdk-gamnp-874087bbd1.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://sems-firebase.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
System.out.println("ERROR: invalid service account credentials.");
System.out.println(e.getMessage());
System.exit(1);
}
这是数据库的快照,我试图更改的值是帖子ID为“adad2131”的作者字段。 我已经尝试使用“posts/adad2131/author”,“/posts/adad2131/author”或者仅仅是“adad2131/author”来引用该字段,也许我引用方式不正确吗?
以下是完整的代码:
public class Main {
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
try {
FileInputStream serviceAccount = new FileInputStream("sems-firebase-firebase-adminsdk-gamnp-874087bbd1.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://sems-firebase.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
} catch (IOException e) {
System.out.println("ERROR: invalid service account credentials.");
System.out.println(e.getMessage());
System.exit(1);
}
//Try to change data in Firebase
CountDownLatch done = new CountDownLatch(1);
FirebaseDatabase.getInstance().getReference("posts/adad2131/author").setValue("Test", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError de, DatabaseReference dr) {
done.countDown();
}
});
done.await();
}
}
Ps. 我也正在使用管理员 SDK 5.9