简体   繁体   English

在 Firebase 实时数据库中存储数据

[英]Storing data in Firebase Realtime Database

I have a problem with storing data in the Firebase Realtime Database.我在 Firebase 实时数据库中存储数据时遇到问题。 There are no errors but nothing is stored in the database.没有错误,但没有任何内容存储在数据库中。 I have tried with different guides, tutorial (on yt) posts (found here) but none of them worked.我尝试过使用不同的指南、教程(在 yt 上)帖子(在此处找到),但都没有奏效。 Is there a working way that allows me to solve this problem?有没有一种工作方式可以让我解决这个问题? Thanks in advance everyone and sorry if my English is not perfect.在此先感谢大家,如果我的英语不完美,我们深表歉意。

public void registration() {

        if(!validation())
            return;

        final ProgressDialog progressDialog = new ProgressDialog(RegisterActivity.this,
                R.style.AppTheme_Dark_Dialog);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Creating account...");
        progressDialog.show();

        mAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance();
        DatabaseReference reff = mDatabase.getReference();

        user = new User();
        String tableName = "Users";

        Map<String, String> userMap = new HashMap<String, String>();

        //register user in firebase
        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {

                    user.setUser(mFullName.getText().toString().trim());
                    user.setEmail(mEmail.getText().toString().trim());

                    userMap.put(fullName, email);
                    String currentUser = mAuth.getCurrentUser().getUid();

                    reff.child(tableName).child(currentUser).setValue(userMap);
//The following line make app crush

                    reff.setValue(userMap, new Firebase.CompletionListener() {
                        @Override
                        public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                            if (firebaseError != null) {
                                System.out.println("Data could not be saved. " + firebaseError.getMessage());
                            } else {
                                System.out.println("Data saved successfully.");
                            }
                        }
                    });

                    Toast.makeText(RegisterActivity.this, "User successful created", Toast.LENGTH_SHORT).show();

                    startActivity(new Intent(getApplicationContext(), MenuActivity.class));
                    finish();
                    progressDialog.dismiss();
                }
                else {
                    Toast.makeText(RegisterActivity.this, "Error! " +
                            task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

User.java用户.java

public class User {

    private String user, email, photoUrl, Uid;

    public User() { }

    public User(String uid, String user) {
        Uid = uid;
        this.user = user;
    }

    public User(String user) {
        this.user = user;
    }

    public User(String user, String email, String photoUrl, String uid) {
        this.user = user;
        this.email = email;
        this.photoUrl = photoUrl;
        Uid = uid;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

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

    public String getUid() {
        return Uid;
    }

    public void setUid(String uid) {
        Uid = uid;
    }
}

The error is:错误是:

com.google.firebase.database.DatabaseException: Failed to parse node with class class com.example.xxxxxxxx.activities.RegisterActivity$4$1
        at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:103)
        at com.google.firebase.database.snapshot.NodeUtilities.NodeFromJSON(NodeUtilities.java:28)
        at com.google.firebase.database.snapshot.PriorityUtilities.parsePriority(PriorityUtilities.java:39)
        at com.google.firebase.database.DatabaseReference.setValue(DatabaseReference.java:199)
        at com.example.xxxxxxxx.activities.RegisterActivity$4.onComplete(RegisterActivity.java:160)
        at com.google.android.gms.tasks.zzj.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

logcat:日志猫:

2021-01-02 17:07:36.549 20191-20213/com.example.inbioaiqua W/System: Ignoring header X-Firebase-Locale because its value was null.
2021-01-02 17:07:36.557 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.562 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.574 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.580 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.592 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.601 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.611 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.617 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.625 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.631 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.639 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.645 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.657 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.663 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.673 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.680 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.689 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.693 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.706 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.713 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.722 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.729 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.738 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:36.849 20191-20213/com.example.inbioaiqua D/FirebaseAuth: Notifying id token listeners about user ( 7LOLWRPcBhVqHm98BVHzpI5FkDG2 ).
2021-01-02 17:07:36.849 20191-20213/com.example.inbioaiqua D/FirebaseAuth: Notifying auth state listeners about user ( 7LOLWRPcBhVqHm98BVHzpI5FkDG2 ).
2021-01-02 17:07:36.874 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:37.046 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:37.260 20191-20214/com.example.inbioaiqua D/EGL_emulation: eglMakeCurrent: 0x9eb85360: ver 2 0 (tinfo 0x9eb831a0)
2021-01-02 17:07:37.260 20191-20214/com.example.inbioaiqua W/OpenGLRenderer: Points are too far apart 4.000001

Your DatabaseReference is not initialized, so use the below code on top.您的DatabaseReference未初始化,因此请在顶部使用以下代码。

mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
DatabaseReference reff = mDatabase.getReference();

And add onComplete listener to setValue().并将onComplete侦听器添加到 setValue()。

reff.child(tableName)
.child(currentUser)
.setValue(userMap, new Firebase.CompletionListener() {
    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
        if (firebaseError != null) {
            System.out.println("User data save failure." + firebaseError.getMessage());
        } else {
            System.out.println("User data save successful.");
            progressDialog.dismiss();
            startActivity(new Intent(getApplicationContext(), MenuActivity.class));
            finish();
        }
    }
});

So in your code:所以在你的代码中:

reff.child(tableName).setValue(user)

Here you are trying to set a value for a node that already has branches and Firebase database does not allow that, so what you can do is create another node in the "Users" table to store users.在这里,您尝试为已经有分支的节点设置一个值,而 Firebase 数据库不允许这样做,因此您可以在“用户”表中创建另一个节点来存储用户。 For example:例如:

reff.child(tableName).child("User1"). setValue(user);

Here "User1" is just to label the field and can be a simple integer such as "1" too.这里的“User1”只是 label 字段,也可以是简单的 integer,例如“1”。

Your code: reff.setValue(userMap, new Firebase.CompletionListener()您的代码: reff.setValue(userMap, new Firebase.CompletionListener()

Again, you are trying to set a value of a node with branches which in your case it's the main branch and Firebase database don't allow that.同样,您正在尝试设置具有分支的节点的值,在您的情况下它是主分支,而 Firebase 数据库不允许这样做。 So what you can do is create another node in the reff.因此,您可以做的是在 ref 中创建另一个节点。

reff.child(tableName).child(YOUR_DESIRED_CHILD).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>()

Here YOUR_DESIRED_CHILD is field name for your Users childs.这里YOUR_DESIRED_CHILD是您的Users子项的字段名称。

And it should work just fine.它应该工作得很好。

You are passing to the "setValue()" method a CompletionListener object, an option which is not available anymore.您正在将 CompletionListener object 传递给“setValue()”方法,该选项不再可用。 To add the user, please use the following lines of code:要添加用户,请使用以下代码行:

reff.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "User added successfully!");
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

And comment or remove the lines that you are using now.并评论或删除您现在正在使用的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM