簡體   English   中英

嘗試寫入/讀取 Firebase 實時數據庫但沒有結果 - Java 服務器桌面

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

我一整天都在嘗試了解我應該從 Firebase 實時數據庫寫入和讀取的代碼發生了什么,但沒有成功,也沒有問題、錯誤、異常或任何可以幫助我的提示弄明白。 我正在用java for desktop編碼,而不是為 Android 編碼,因為我搜索中的所有出現都會返回很多,盡管我也使用相同的數據庫為 Android 編碼了相同的操作並且它工作得很好。 我在這上面花費了太多的時間和精力,而且我的截止日期太短,無法將它作為我正在做的學科的項目提交,所以如果有人能提供幫助,我將不勝感激!!

就像在 Android 中讀取和寫入數據是完全可能的一樣,我放棄了問題出在 DB 中的可能性。 我正在按照Firebase 管理頁面中的說明進行操作。 正如我所說,沒有錯誤,但也沒有結果。 使用相同的配置和從 Firebase 控制台獲取的 json 密鑰文件與 Firebase Cloud Messaging 一起正常工作,所以我認為密鑰也可以。

我正在嘗試的代碼如下,幾乎與 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);
                }
            }
        });
    }
}

數據庫是這樣的:

鏈接到我正在使用的 firebase 數據庫的打印

當我運行代碼時,我只能使用 println 從下面的行中獲取數據(這只是來自 DB 的 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 上

所以,這就是伙計們。 即使整天搜索並嘗試了許多選項,我也沒有任何線索,我真的需要幫助。 我希望有人能給我一個很大的幫助。

如果有人通過同樣的困難,問題是我沒有使用服務器來保持它的工作,所以當我運行它時,沒有足夠的時間來接收反饋。 一旦我保持運行,它就能夠顯示來自 firebase 的所有通信結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM