'Android App won't write to Firebase Realtime Database
I'm using Firebase and Android Studio to build an app. I have a register screen that asks the user for things such as username and location etc. When the register button is clicked, the users' email and password work and show up in the authentication tab in the Firebase Console. However, I cannot get the users details such as username and location, to store in the Firebase Realtime Database.
package com.example.securityapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class register extends AppCompatActivity {
private FirebaseAuth mAuth;
EditText emailReg, passwordReg, password2, roleReg, officeReg;
Button regButton;
DatabaseReference databaseUsers;
EditText usernameReg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
emailReg = (EditText) findViewById(R.id.regEmail);
usernameReg = (EditText) findViewById(R.id.regUsername);
passwordReg = (EditText) findViewById(R.id.regPassword);
regButton = (Button) findViewById(R.id.register_Button);
password2 = (EditText) findViewById(R.id.confirm_password);
roleReg = (EditText) findViewById(R.id.regRole);
officeReg = (EditText) findViewById(R.id.regOffice);
databaseUsers = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
}
public void registerUser() {
final String email = emailReg.getText().toString();
final String password = passwordReg.getText().toString();
String pass2 = password2.getText().toString();
if(!pass2.equals(password))
{
Toast.makeText(register.this, "Passwords Do Not Match",
Toast.LENGTH_SHORT).show();
return;
}
//Code taken from https://firebase.google.com/docs/auth/android/start on 10/11/2019
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Intent intent_signup = new Intent(register.this, home.class);
startActivity(intent_signup);
finish();
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(register.this, "Authentication Successful!",
Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(register.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
public void registerOnClick(View view)
{
regButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
registerUser();
insertDataDB();
}
});
}
public void insertDataDB()
{
String username = usernameReg.getText().toString();
String office = officeReg.getText().toString();
String job = roleReg.getText().toString();
String email = emailReg.getText().toString();
String id = databaseUsers.push().getKey();
Users user1 = new Users(username, office, job, email);
databaseUsers.child(id).setValue(user1);
}
}
Users.java
package com.example.securityapp;
public class Users {
String username;
String job;
String email;
String office;
public Users()
{
}
public Users(String username, String job, String email, String office)
{
this.username = username;
this.job = job;
this.email = email;
this.office = office;
}
public String getUsername()
{
return username;
}
public String getJob() { return job; }
public String getEmail()
{
return email;
}
public String getOffice()
{
return office;
}
}
This is the notification I am receiving but I am not using Cloud Firestore, I am trying to use Realtime database
Solution 1:[1]
Change the security rules to the following :
{
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this ruleset in production; it allows
// anyone to overwrite your entire database.
"rules": {
".read": true
".write": true
}
}
Solution 2:[2]
Once you finished creating the account by using mAuth, you should store the data location, etc in the Firebase.
//Code taken from https://firebase.google.com/docs/auth/android/start on 10/11/2019
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
// Here we will get the user information, ex userUid (optional)
FirebaseUser user = mAuth.getCurrentUser();
//After that store the data before intent to next activity
insertDataDB();
//Then proceed to next activity
Intent intent_signup = new Intent(register.this, home.class);
startActivity(intent_signup);
finish();
Toast.makeText(register.this, "Authentication Successful!", Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Toast.makeText(register.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
}
// ...
}
});
UPDATED: Store method.
public void insertDataDB()
{
String username = usernameReg.getText().toString();
String office = officeReg.getText().toString();
String job = roleReg.getText().toString();
String email = emailReg.getText().toString();
String id = databaseUsers.push().getKey();
Users user1 = new Users(username, office, job, email);
databaseUsers.child(id).setValue(user1).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
//Success store data in database.
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}else {
//Something happened
Toast.makeText(getApplicationContext(),"Error: " + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
Solution 3:[3]
I think this is more of a CrossValidated question, but I'll give it a shot (it may be migrated).
tl;dr I don't think you should be trying to aggregate like this: this kind of unbalanced design is exactly where GLMMs are useful.
I think you should be using a negative binomial or other overdispersed model: the ratio of deviance()/df.residual() (a rough guide to overdispersion is greater than 2 (a Poisson model would be appropriate if the dispersion were close to 1).
The random effect of Region is estimated as zero — not surprising because your data set is fairly small, fairly noisy, and you have only 3 regions. You could simplify the model and replace (1|Region/SITE) with (1|SITE), if the differences among regions are not very interesting to you.
I fitted the model with (1|SITE) and family = "nbinom2", then I used the DHARMa package to explore residuals (plot(ss <- simulateResiduals(model)); plotResiduals(ss, df$Grass.perc)). I found there was some nonlinearity in the pattern. I also tried with nbinom1 rather than nbinom2 and found it improved the fit, so I ended up with:
glmmTMB(Ladybeetles ~ poly(Grass.perc,2) + (1|SITE) ,
data=df, family="nbinom1" )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Haddad |
| Solution 2 | |
| Solution 3 | Ben Bolker |

