【问题标题】:Data not added into firebase database数据未添加到 Firebase 数据库中
【发布时间】:2016-09-08 16:24:49
【问题描述】:

我从here 获得了 Firebase Android 教程,并按照本教程中的每一步操作。但是,当我单击创建button 并刷新firebase 时,我得到的数据库数据始终为空。

有人可以帮我找出问题所在吗?

这是我的firebase

Config.java

public class Config {
    public static final String FIREBASE_URL = "https://healthy-app-30d65.firebaseio.com/";   
}

注册

public class Register extends AppCompatActivity {
    EditText editTextName;
    EditText editTextAddress;
    TextView textViewPersons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        Firebase.setAndroidContext(this);
        Button buttonSave = (Button) findViewById(R.id.buttonSave);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextAddress = (EditText) findViewById(R.id.editTextAddress);
        textViewPersons = (TextView) findViewById(R.id.textViewPersons);

        //Click Listener for button
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating firebase object
                Firebase ref = new Firebase(Config.FIREBASE_URL);

                //Getting values to store
                String name = editTextName.getText().toString().trim();
                String address = editTextAddress.getText().toString().trim();

                //Creating Person object
                Person person = new Person();

                //Adding values
                person.setName(name);
                person.setAddress(address);

                //Storing values to firebase
                ref.child("Person").setValue(person);
            }
        });
    }


}

【问题讨论】:

  • 这看起来根本不像我的 Firebase 代码,但是您是否已登录或更改了身份验证,所以您不需要?
  • 本教程适用于旧版 Firebase。考虑使用新的 Firebase SDK Setup guide here 重新开始。

标签: android firebase firebase-realtime-database


【解决方案1】:

您正在使用为旧版 Firebase 控制台 (firebase.com) 和 SDK 版本 (2.x) 编写的教程。但是您的 Firebase 项目是在新的 Firebase 控制台 (firebase.google.com) 上创建的。

虽然大部分 API 将继续工作,但这里有一些不同之处:

  1. 在新控制台上创建的项目配置为仅允许经过身份验证的用户读/写。 (临时)解决此问题的最简单方法是阅读first note in blue on this documentation page。这解释了如何使您的数据库世界再次可读/可写。

  2. 来自 Firebase 2.x SDK 的身份验证不适用于在新 Firebase 控制台上创建的项目。

所以#1 可能是一个很好的快速解决方法,但您确实应该学习更新的教程,例如其他答案中建议的教程。

【讨论】:

    【解决方案2】:

    我建议您关注最新的 firebase 示例。您可以在Google Firebase Codelab找到分步设置和示例项目

    例如,存储值到实时数据库被更新

    ref.child("Person").setValue(person);ref.child("Person").push().setValue(person);

    【讨论】:

      【解决方案3】:

      新的 Firebase 与旧代码 (https://wwww.firebase.com) 不兼容,您必须将 Android 项目设置为最新的 SDK。关于如何迁移数据库代码,您可以参考这个link

      【讨论】:

        猜你喜欢
        • 2021-10-04
        • 1970-01-01
        • 2019-10-19
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        • 1970-01-01
        相关资源
        最近更新 更多