java - 尝试写入/读取 firebase 实时数据库但没有结果 - java 服务器桌面
问题描述
我一整天都在尝试了解我的代码应该从 Firebase 实时数据库写入和读取的情况,但没有成功,也没有任何问题、错误、异常或任何可以帮助我的提示弄明白。我正在为桌面编写 java代码,而不是为 Android 编写代码,因为我的搜索中的所有出现都返回了很多,尽管我也使用相同的数据库为 Android 编写了相同的操作并且它工作得很好。我在这上面花费了太多的时间和精力,而且我的截止日期太短,无法将它作为我正在做的一门学科的项目提交,所以如果有人可以提供帮助,我将不胜感激!
与在 Android 中完全可以读取和写入数据一样,我放弃了问题出在 DB 中的可能性。我正在按照Firebase 管理员页面中的说明进行操作。正如我所说,没有错误,但也没有结果。使用从 Firebase 控制台获取的相同配置和 json 密钥文件与 Firebase 云消息传递正常工作,所以我认为密钥也可以。
我正在尝试的代码如下,与 Firebase 网页几乎相同:
package pt.ipp.estg.housecontrol;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.FileInputStream;
import java.io.IOException;
class StarterFRD {
public static void main(String[] args) throws IOException {
FileInputStream refreshToken = new FileInputStream("/Users/wdcunha/Development/Java/frdproj/src/main/resources/housecontrolmobile-firebase-adminsdk-qv0hl-f41a07409d.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.setDatabaseUrl("https://housecontrolmobile.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
// As an admin, the app has access to read and write all data, regardless of Security Rules
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users");
System.out.println("ref: "+ref);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object document = dataSnapshot.getValue();
System.out.println("document: "+document);
}
@Override
public void onCancelled(DatabaseError error) {
System.out.println("canceled: "+error.toException());
}
});
Utilizador utilz = new Utilizador("Euquipe", "eu@quipe.com");
System.out.println("utilz nome: "+utilz.getNome());
System.out.println("utilz email: "+utilz.getEmail());
ref.child("2").push().setValueAsync(utilz);
ref.child("3").setValue("I'm writing data", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully."+databaseReference);
}
}
});
}
}
数据库就是这样一个:
当我运行代码时,我可以使用 println 从下面的行中获取数据(这只是来自数据库的 url):
DatabaseReference ref = FirebaseDatabase.getInstance()
它被设置为maven项目,所以pom.xml writen是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pt.ipp.estg.housecontrol</groupId>
<artifactId>housecontrolserver</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
整个代码在Github here中。
所以,就是这样,伙计们。即使整天搜索并尝试了许多选项,我也不知道,我真的需要帮助。我希望有人能给我一个很大的帮助。
解决方案
如果有人遇到同样的困难,问题是我没有使用服务器来保持它的工作,所以当我运行它时,没有足够的时间来接收反馈。一旦我让它运行,它就能够显示来自 firebase 的所有通信结果。