【问题标题】:KOTLIN/FIREBASE Error when I try get data from firebase database - Client is offline当我尝试从 firebase 数据库获取数据时出现 KOTLIN/FIREBASE 错误 - 客户端处于脱机状态
【发布时间】:2025-12-07 17:40:01
【问题描述】:

当我尝试通过他的 uid 获取用户信息时,这与 firebase 数据库中的相同,会显示此错误。

uid 以正确的方式传递,我已经检查过了。到数据库的路由很好,我也检查过。

这是我的代码,这可能没有任何错误,因为一切正常,只有 Firebase 有一些问题。

class Profil : AppCompatActivity() {

    private var fAuth: FirebaseAuth?=null
    private var database: DatabaseReference?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profil)

        fAuth= FirebaseAuth.getInstance()

        // ✋ Here I checked with uid and uid.toString()

        var userCheck = fAuth!!.currentUser?.uid.toString()


        getData(userCheck)

        // ✋ Here I checked that good uid is get

        //tv_firsName.text = userCheck
    }

    private fun getData(userCheck: String) {

        database = FirebaseDatabase.getInstance().getReference("Users")
        database!!.child(userCheck).get().addOnSuccessListener{

            if(it.exists()){
                val firstName = it.child("firstName").value
                val lastName = it.child("lastName").value
                val position = it.child("position").value
                val email = it.child("email").value

                tv_Imie.text = firstName.toString()
                tv_Nazwisko.text = lastName.toString()
                tv_Stanowisko.text = position.toString()
                tv_Email.text = email.toString()
            }else{
                Toast.makeText(this, "User does not exist", Toast.LENGTH_SHORT).show()
            }
        }.addOnFailureListener{
            Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show()

            Log.e("firebase", "Error getting data", it)
        }
    }
}

这是我的数据库结构

我检查了一些方法来解决这个问题。

我看到有人用addValueEventListener 提出了一个解决方案,但随后获取了列表。我希望将单个用户信息包含在我的 textViews 中。我读到这个问题可能与 Firebase 中的错误有关,而不是与代码中的错误有关。

【问题讨论】:

  • 请编辑您的问题并将您的数据库结构添加为 JSON 文件。您可以通过单击 Firebase Console 的溢出菜单 (⠇) 中的导出 JSON 来简单地获取它。
  • 除此之外,您确定您有互联网连接吗?
  • 如果get() 不起作用,我建议使用较旧的addListenerForSingleValueEvent 方法。两者(应该)返回完全相同的数据,所以不会有任何区别。

标签: android firebase kotlin firebase-realtime-database


【解决方案1】:

好的,问题出在 Firebase 而不是我的应用程序上。我不知道为什么,但是当我每次尝试启动应用程序时,Firebase 都会关闭我与数据库的连接。我尝试向 google.services.json 添加一些指向数据库的链接并更改一些设置,但这不起作用。毕竟我从firebase中删除了应用程序并删除了文件google.services.json。我在网站上再次添加了应用程序,然后我将应用程序与 firebase 连接并设置了新的 google.services.json。一切正常,感谢您的帮助。

【讨论】:

    最近更新 更多