java在firebase实时数据库中的数据存储

6ljaweal  于 2021-06-29  发布在  Java
关注(0)|答案(4)|浏览(327)

我在firebase实时数据库中存储数据时遇到问题。没有错误,但数据库中没有存储任何内容。我尝试过不同的指南,教程(在yt上)的帖子(在这里找到),但都不管用。有没有一种工作方法可以让我解决这个问题?提前谢谢大家,如果我的英语不太好,我很抱歉。

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();
                }
            }
        });

    }

用户.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;
    }
}

错误是:

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)

日志:

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
4bbkushb

4bbkushb1#

你的 DatabaseReference 未初始化,因此请使用顶部的以下代码。

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

并添加 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();
        }
    }
});
ecfsfe2w

ecfsfe2w2#

所以在你的代码里: reff.child(tableName).setValue(user) 这里您试图为已经有分支的节点设置一个值,而firebase数据库不允许这样做,因此您可以在“users”表中创建另一个节点来存储用户。例如: reff.child(tableName).child("User1"). setValue(user); 这里的“user1”只是标记字段,也可以是一个简单的整数,比如“1”。

i86rm4rw

i86rm4rw3#

您正在向“setvalue()”方法传递一个completionlistener对象,这是一个不再可用的选项。要添加用户,请使用以下代码行:

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!
        }
    }
});

并注解或删除您现在使用的行。

5fjcxozz

5fjcxozz4#

您的代码: reff.setValue(userMap, new Firebase.CompletionListener() 同样,您正在尝试设置一个具有分支的节点的值,在您的示例中,它是主分支,而firebase数据库不允许这样做。因此,您可以在reff中创建另一个节点。 reff.child(tableName).child(YOUR_DESIRED_CHILD).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() 在这里 YOUR_DESIRED_CHILD 是您的 Users 孩子们。
它应该可以正常工作。

相关问题