【问题标题】:Why my database is not working, I am using ways in official documentation?为什么我的数据库不工作,我在使用官方文档中的方法?
【发布时间】:2020-06-04 18:20:32
【问题描述】:

我有一个问题: Firebase 数据库不工作,我不明白为什么会发生这种情况(无法将数据发布到服务器,当我按下按钮发送数据时 - 没有任何反应,数据没有保存在服务器)。

看看我的代码:

private var db = FirebaseDatabase.getInstance()                                               
private lateinit var dbReference: DatabaseReference                                           

override fun onCreate(savedInstanceState: Bundle?) {                                          
 super.onCreate(savedInstanceState)                                                        
 setContentView(R.layout.activity_main)                                                    
 setSupportActionBar(toolbar)                                                              

 dbReference = db.reference.child("member")                                                

 add_item_to_db_button.setOnClickListener {                                                 
     dbReference.push()                                                                    
         .setValue(Member(item_name.text.toString(), item_description.text.toString()))    

 }                                                                                         
}  

我的构造函数类将数据发送到数据库:

 class Member {
 lateinit var itemName: String
 lateinit var itemDescription: String

constructor()
constructor(itemName: String, itemDescription: String) {
    this.itemName = itemName
    this.itemDescription = itemDescription
  }

} 

我的依赖项(google()jcenter() 在顶级 Gradle 中启用):

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


// Firebase
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-core:17.2.2'
implementation 'com.firebaseui:firebase-ui:0.6.2'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.3.1'

}

apply plugin: 'com.google.gms.google-services'   

我的XML 文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:padding="40dp"
 android:orientation="vertical"
 android:gravity="center"
 android:layout_height="match_parent">

 <ImageView
    android:layout_width="200dp"
    android:src="@color/cardview_dark_background"
    android:layout_marginBottom="30dp"
    android:layout_height="200dp"/>

 <EditText
    android:layout_width="match_parent"
    android:id="@+id/item_name"
    android:text="Description"
    android:layout_height="wrap_content"/>

 <EditText
    android:text="Name of product"
    android:layout_width="match_parent"
    android:id="@+id/item_description"
    android:layout_height="wrap_content"/>

 <Button
    android:id="@+id/add_item_to_db_button"
    android:layout_width="match_parent"
    android:text="Add"
    android:layout_height="wrap_content"/>

</LinearLayout>

最后是数据库规则:

"rules": {
   ".read": true,
   ".write": true
 }           

我不明白为什么 db 不工作。尝试了几种方法发布在StackOverflow - 没有帮助。

【问题讨论】:

  • 水不工作
  • 抱歉,我更新了一个问题。数据不会发布到数据库。单击按钮后 - 没有任何反应。
  • 你的项目连接到firebase了吗?
  • 当然。我已连接我的项目。
  • 你确定你没有错过FirebaseApp.initializeApp(this) 吗?如果您只是将其粘贴为 OnCreate 或 Application Class 中的第一行。

标签: android firebase android-studio kotlin firebase-realtime-database


【解决方案1】:

我遇到了这个问题。但是找不到永久的解决方案。 我收到了来自 firebase 社区的邮件。

我们收到了几份关于印度不同 ISP 提供商的相同问题的报告,但这是 ISP 网络的问题,超出了我们的范围和控制范围,不过我们正在与当地提供商合作,试图解决这个问题尽快恢复服务,因为我们正在尽可能多地收集有关该问题的信息,如果您向我们提供您的 ISP 的名称,我们将不胜感激。

我们建议您与您的 ISP 联系,以获取有关故障排除的更多详细信息和信息,以找到此问题的根本原因,因为我们可以确认问题不在我们这边。

很抱歉无法为您提供进一步的帮助,但请确保我们仍在努力使其恢复正常运行。

【讨论】:

    【解决方案2】:

    好吧,所有尝试帮助我的人 - 非常感谢;)在我的情况下,所有方法都没有帮助我,所以我尝试将 Firebase DB 连接到另一个 Google account,然后连接 Firebase Database,结果- 一切正常。

    【讨论】:

      【解决方案3】:

      将您的类更改为数据类:

      data class Member(val itemName : String, val itemDescription : String) {
      constructor() : this("", "")
      }
      

      【讨论】:

      • 试过了。没有帮助。在这种情况下,非常有趣的情况,因为我在之前的项目中使用了类似于我现在的构造函数,并且一切都很好。
      【解决方案4】:

      您的清单文件中需要有效的网络和此权限。交叉检查您的清单文件。

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

      【讨论】:

      • 抱歉,没用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2013-05-21
      相关资源
      最近更新 更多