I have a problem with storing data in the Firebase Realtime Database. 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. 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
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.
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance();
DatabaseReference reff = mDatabase.getReference();
And add onComplete
listener to 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. 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.
Your code: 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. So what you can do is create another node in the reff.
reff.child(tableName).child(YOUR_DESIRED_CHILD).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>()
Here YOUR_DESIRED_CHILD
is field name for your Users
childs.
And it should work just fine.
You are passing to the "setValue()" method a CompletionListener object, an option which is not available anymore. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.