我正在尝试按照 Cloud Firestore 上的文档将文档保存在集合中。我可以读取我在 firebase 控制台上手动创建的集合/文档,但尝试从我的 Android 应用程序存储是行不通的。
控制台/Logcat 没有显示任何错误,应用程序也没有崩溃。 两个听众都没有接到电话。 我错过了什么吗?
class UserActivity : AppCompatActivity() {
private val db = FirebaseFirestore.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
save_button.setOnClickListener { storeUser() }
}
private fun storeUser(){
val user = HashMap<String,Any>()
user.put("first", "Ada")
user.put("last", "Lovelace")
user.put("born", 1815)
db.collection("users")
.add(user)
.addOnSuccessListener(OnSuccessListener<DocumentReference> { documentReference -> Log.d("Storing User", "DocumentSnapshot added with ID: " + documentReference.id) })
.addOnFailureListener(OnFailureListener { e -> Log.w("Storing User", "Error adding document", e) })
}
}
编辑:
当试图读取文档的更新抛出以下异常
com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline.
最佳答案
看来我需要将以下权限添加到 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
但我觉得这很奇怪,因为我可以在没有它的情况下使用 firebase 身份验证方法登录。
关于android - 无法在 Android 上使用 Cloud Firestore 保存文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51624263/