java - 按下按钮时数据未写入 Firebase

标签 java android firebase firebase-realtime-database

好的,所以我希望有人可以帮助我解决这个问题,因为过去几天我一直在尝试多种不同的解决方案。在我的 Activity 中,我有一个 EditText 和一个 Button,我正在尝试将该用户输入写入 Firebase。运行应用程序时我没有收到任何错误,但是,当我按下按钮时,没有任何反应。

这是我的类(class):

private EditText userEmail;
Button sendInviteButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(this);
    setContentView(R.layout.activity_user_registration);

    userEmail = (EditText) findViewById(R.id.user_email);
    sendInviteButton = (Button) findViewById(R.id.sendInviteButton);

}

public void btn(View view) {
    sendInviteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Firebase ref = new Firebase("https://app-1318.firebaseio.com/clients");
            String enteredUserEmail = userEmail.getText().toString();
            ref.child("userBackgroundCheck").setValue(enteredUserEmail);
        }
    });
}

我在 XML 中确实有一个按钮的 onClick,所以我真的不知道为什么没有将此数据发送到 Firebase。

最后,我们是否应该为这部分使用 Firebase 的根 URL?:

Firebase ref = new Firebase("https://app-1318.firebaseio.com/clients");

这将是:

Firebase ref = new Firebase("https://app-1318.firebaseio.com");

最佳答案

您是否尝试在实时数据库下添加规则。当您转到实时数据库时,单击规则选项卡。添加以下行

// These rules give anyone, even people who are not users of your app,
// read and write access to your database
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

看看有没有效果!

另外,为什么要以旧方式存储数据。这是 new doc用于在实时数据库中保存数据。

关于java - 按下按钮时数据未写入 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546988/

相关文章:

JavaFX,使用箭头键移动 GridPane 中的节点

java - 使用许多 if-else 语句(多个条件)优化函数的最佳方法是什么?

firebase - 无法访问 Firebase 函数中自动填充的环境变量

swift - 获取 Firebase 文档作为数组

java - 玩2.2.2(Scala),如何处理HttpServletRequests

java - 用户输入显示最短路径

java - 设备旋转导致 Activity 被销毁和创建

java - 如何在我的 java 类中将 firebase 值存储为全局整数

java - c3p0 中的关闭连接

android - Eclipse Classic 版本差异?