writing/reading 次 firebase 实时数据库尝试没有结果 - java 服务器桌面

Attempts of writing/reading firebase realtime database with no results - java server desktop

我一整天都在尝试了解我的代码发生了什么,这些代码应该写入和读取 Firebase 实时数据库,但没有成功,也没有问题、错误、异常或任何类型的可以帮助我弄清楚的提示。我在 java 中为桌面 编写代码,而不是为 Android 编写代码,因为我搜索中的所有事件 return 很多,尽管我也为 Android 使用相同的数据库进行相同的操作,并且运行良好。我在这上面花费了太多的时间和精力,而且我的截止日期太短,无法将其作为我正在做的学科的项目提交,所以如果有人能提供帮助,我将不胜感激!!

因为在Android中完全可以读写数据,所以我排除了问题出在数据库中的可能性。我正在按照 Firebase Admin page 中提供的说明进行操作。正如我所说,没有错误,但也没有结果。使用相同的配置和从 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);
                }
            }
        });
    }
}

数据库是那个:

Link to print of the firebase database I'm using

当我 运行 代码时,我可以使用 println 从下面的行中获取数据(即来自数据库的 url):

        DatabaseReference ref = FirebaseDatabase.getInstance()

设置为maven项目,所以pom.xml的写法是这样的:

<?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.

伙计们,就是这样。即使整天搜索并尝试了很多选项,我也毫无头绪,我真的需要帮助。我希望有人能给我很大的帮助。

如果有人通过同样的困难,问题是我没有使用服务器来保持它的工作,所以当我 运行 它时,没有足够的时间来接收回馈。一旦我保留它 运行ning,它就能够显示来自 firebase 的所有通信结果。