简体   繁体   English

firebase 实时数据库无法在移动设备上运行,但在模拟器上运行

[英]firebase realtime database does not work on mobile but is working on emulator

binding.btSave.setOnClickListener {
        val firstName = binding.etFirstName.text.toString()
        val lastName = binding.etLastName.text.toString()
        val age = binding.etAge.text.toString()
        val userName = binding.etUserName.text.toString()

        if (firstName.isNotEmpty() && age.isNotEmpty() && lastName.isNotEmpty() && userName.isNotEmpty()) {

            try {

                database =
                    FirebaseDatabase.getInstance("My Url")
                        .getReference("Users")

                val users = User(firstName, lastName, age, userName)

                database.child(userName).setValue(users).addOnSuccessListener {

                    binding.etAge.text.clear()
                    binding.etFirstName.text.clear()
                    binding.etLastName.text.clear()
                    binding.etUserName.text.clear()

                    Toast.makeText(this, "Successfully added", Toast.LENGTH_SHORT).show()

                }.addOnFailureListener {

                    Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()

                }

            } catch (e: Exception) {

                Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()

            } finally {
                Toast.makeText(this, "finished", Toast.LENGTH_SHORT).show()

            }
        }
    }

when I click the button it goes directly to "finally" of try catch in mobile but in emulator is ok and I give permission of internet and access network state to the app also the app is working for Authentication well当我单击按钮时,它直接进入“最终”在移动设备中尝试捕获但在模拟器中是可以的,并且我向应用程序授予互联网和访问网络 state 的权限,该应用程序也适用于身份验证

what should I do??我应该怎么办??

Did you add permissions to access the internet in Manifest file?您是否在 Manifest 文件中添加了访问 Internet 的权限? Debugger is working without this permissions, but when you want to build "production" application, it requires this permissions to work.调试器在没有此权限的情况下工作,但是当您想要构建“生产”应用程序时,它需要此权限才能工作。

You need to add those 2 lines:您需要添加这两行:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

reference: https://developer.android.com/training/basics/network-ops/connecting参考: https://developer.android.com/training/basics/network-ops/connecting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM