无法将数据从android studio上传到firebase实时数据库

时间:2021-04-03 18:05:40

标签: android firebase firebase-realtime-database

我的依赖列表是

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'

    implementation 'com.android.support:design:28.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.0'

    // Displaying images
    implementation 'com.github.bumptech.glide:glide:3.6.1'
    implementation platform('com.google.firebase:firebase-bom:26.8.0')
    implementation 'com.google.firebase:firebase-database:19.7.0'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-analytics'
}

我还连接到了 Firebase 并创建了实时数据库。 我还将 firebase 身份验证规则设置为 true

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

首先在 MainActivity 中声明了 2 个变量

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mMessagesDatabaseReference;

然后在 OnCreate 方法中我发布了这个

mFirebaseDatabase=FirebaseDatabase.getInstance();
mMessagesDatabaseReference=mFirebaseDatabase.getReference();

之后我调用了数据库引用来在数据库上设置字符串值。

 FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, null);
                String data=friendlyMessage.getText();
                mMessagesDatabaseReference.setValue(data);

这是我的 FriendlyMessage.java 类

package com.google.firebase.udacity.friendlychat;

public class FriendlyMessage {

    private String text;
    private String name;
    private String photoUrl;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

但是每次 firebase 都显示这个东西

This is my Firebase Realtime database

请有人帮我

2 个答案:

答案 0 :(得分:1)

您的安全规则允许任何人在数据库中的 /Users 节点下写入他们想要的任何数据。但是您的代码试图直接在根目录下编写,没有人被授予任何权限。

您很可能希望在数据库的根目录下允许所有读写操作,这可以通过以下方式完成:

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

在您确认此方法有效后,我强烈建议您阅读有关 security rules 的文档,以了解如何(以及为什么)编写完全符合您需求的规则。

答案 1 :(得分:0)

没有参考如何在实时数据库中存储数据的根引用在哪里

请仔细阅读这些文档 - https://firebase.google.com/docs/database/android/read-and-write

另见这些 - http://prntscr.com/113qtu5

在你的代码结构中这些代码在哪里

相关问题