gpt4 book ai didi

Android - Firebase 字符串返回 'null'

转载 作者:行者123 更新时间:2023-11-29 16:52:24 24 4
gpt4 key购买 nike

好的,

我觉得这真的很奇怪。我有一个带有开放式 Firebase 数据库(读/写:“true”)的 Android Studio 项目,我设法在其中写入和检索数据。

现在我开始了一个新项目,将其与我的 Android Studio 同步并下载了“google-service.json”。和以前一样。

现在我的 Android 项目可以写入数据但无法检索数据。奇怪的!我在网上搜索过,大部分都被重定向到 StackOverflow 并尝试了所有方法。一点都不难,就是不知道哪里不对。

  • 我的 gradle 有:

    repositories {
    maven {
    url "https://maven.google.com"
    }
    }

compile 'com.google.firebase:firebase-database:11.0.4'

  • 我的项目已同步:我已按照“工具”>“Firebase”>“实时数据库”的步骤操作;
  • Android Studio 已使用正确的帐户登录;
  • 我已经下载了“google-service.json”并将其放在正确的位置;
  • 我的 Firebase 具有以下格式:

currentVersion: "2.06"

没什么特别的。

  • 得到这段代码:

    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = firebaseDatabase.getReference("currentVersion");

    databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    // This method is called once with the initial value and again
    // whenever data at this location is updated.
    versionCheckFirebase = dataSnapshot.getValue(String.class);
    }

    @Override
    public void onCancelled(DatabaseError error) {
    // Failed to read value
    }
    });

    Toast.makeText(this, versionCheckFirebase, Toast.LENGTH_SHORT).show();

没有任何问题,我可以通过以下方式将数据写入此数据库:

databaseReference.setValue("2.06");

但是当我从数据库中读取这个值时,它是一个空字符串(null)。

如何?为什么?

-- 编辑--根据@Bob Snyder 的建议进行更新

    databaseReference = firebaseDatabase.getReference("currentVersion");
//databaseReference.setValue("2.06");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
versionCheckFirebase = dataSnapshot.getValue(String.class);
Toast.makeText(MainActivity.this, versionCheckFirebase, Toast.LENGTH_SHORT).show();

}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});

这仍然显示空 toast 消息。但是当我在 Firebase 中更新数据时,它直接在 toast 中显示数据。

我想要的是:- 当我调用这个方法时,它应该在此时获取数据并对其进行处理。但现在只有在我更新 Firebase 数据库时才会调用/更新/检索数据。

-- 编辑 -- 根据@Bob Snyder 的建议更新

解决方案

成功了:

public void getCurrentVersionFirebase(){
databaseReference = firebaseDatabase.getReference("currentVersion");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String dataFromFirebase = dataSnapshot.getValue(String.class);
checkAppForUpdate(dataFromFirebase);
}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}

和:

public void checkAppForUpdate(String dataFromFirebase){
// do your stuff
}

最佳答案

onDataChange() 回调是异步的。在您当前的代码中,当您执行显示 toast 的语句时,回调将不会运行。将你的 toast 移动到那个方法中:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
versionCheckFirebase = dataSnapshot.getValue(String.class);
Toast.makeText(this, versionCheckFirebase, Toast.LENGTH_SHORT).show();
}

关于Android - Firebase 字符串返回 'null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45963266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com