Issue
I'm trying to create Android Application using Android Studio and firebase. I connected my app to firebase and was trying to use Realtime database. Everything have seemed fine until I realized, that firebase was not creating any tables in the Realtime database. Authentication works without any problems and I can see the registered users on the Authentication Page on Firebase website. What's more, I can even use their data to log into application and everything works as it should. I guess, that if something However, nothing new appears in Realtime database. No Users table is created.
Realtime database rules:
{
"rules": {
".read": "true",
".write": "true"
}
}
RegisterUser Activity, which sends data to Firebase Realtime Database:
package com.kalarus.kaluzinski.kaczor;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
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.database.FirebaseDatabase;
public class RegisterUser extends AppCompatActivity implements View.OnClickListener{
private TextView banner;
private Button registerUserButton;
private EditText editTextFullNameRegisterPage, editTextAgeRegisterPage ,editTextEmailRegisterPage, editTextPasswordRegisterPage;
private ProgressBar progressBarRegisterPage;
private FirebaseAuth kinoAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
kinoAuth = FirebaseAuth.getInstance();
banner = (TextView) findViewById(R.id.banner);
banner.setOnClickListener(this);
registerUserButton = (Button) findViewById(R.id.registerUserButton);
registerUserButton.setOnClickListener(this);
editTextFullNameRegisterPage = (EditText) findViewById(R.id.editTextFullNameRegisterPage);
editTextAgeRegisterPage = (EditText) findViewById(R.id.editTextAgeRegisterPage);
editTextEmailRegisterPage = (EditText) findViewById(R.id.editTextEmailRegisterPage);
editTextPasswordRegisterPage = (EditText) findViewById(R.id.editTextPasswordRegisterPage);
progressBarRegisterPage = (ProgressBar) findViewById(R.id.progressBarRegisterPage);
progressBarRegisterPage.setVisibility(View.INVISIBLE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.banner:
startActivity(new Intent(this, LoginPage.class));
break;
case R.id.registerUserButton:
registerUser();
break;
}
}
private void registerUser()
{
String email = editTextEmailRegisterPage.getText().toString().trim();
String password = editTextPasswordRegisterPage.getText().toString().trim();
String fullName = editTextFullNameRegisterPage.getText().toString().trim();
String age = editTextAgeRegisterPage.getText().toString().trim();
if(fullName.isEmpty())
{
editTextFullNameRegisterPage.setError("Full name is required!");
editTextFullNameRegisterPage.requestFocus();
return;
}
if(age.isEmpty())
{
editTextAgeRegisterPage.setError("Age is required!");
editTextAgeRegisterPage.requestFocus();
return;
}
if(email.isEmpty())
{
editTextEmailRegisterPage.setError("Email is required!");
editTextEmailRegisterPage.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
editTextEmailRegisterPage.setError("Please provide valid email!");
editTextEmailRegisterPage.requestFocus();
return;
}
if(password.isEmpty())
{
editTextPasswordRegisterPage.setError("Password is required!");
editTextPasswordRegisterPage.requestFocus();
return;
}
if(password.length() < 6)
{
editTextPasswordRegisterPage.setError("Min password length should be 6 characters!");
editTextPasswordRegisterPage.requestFocus();
return;
}
progressBarRegisterPage.setVisibility(View.VISIBLE);
kinoAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
User user = new User(fullName, age, email);
FirebaseDatabase.getInstance().getReferenceFromUrl("https://console.firebase.google.com/project/kino-fcff3/database/kino-fcff3-default-rtdb/data/~2F")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
Toast.makeText(RegisterUser.this, "User has been registered successfully!", Toast.LENGTH_LONG).show();
progressBarRegisterPage.setVisibility(View.INVISIBLE);
}
else
{
Toast.makeText(RegisterUser.this, "Failed to register! Try again!", Toast.LENGTH_LONG).show();
progressBarRegisterPage.setVisibility(View.INVISIBLE);
}
}
});
}
else
{
Toast.makeText(RegisterUser.this, "Failed to register! Try again!", Toast.LENGTH_LONG).show();
progressBarRegisterPage.setVisibility(View.INVISIBLE);
}
}
});
}
}
User data is stored in User class:
package com.kalarus.kaluzinski.kaczor;
public class User {
public String fullName, age, email;
public User() {
}
public User(String fullName, String age, String email) {
this.fullName = fullName;
this.age = age;
this.email = email;
}
}
I tried many solutions, that I've found on the Internet but none of them solved the issue. Things I've done:
- created new realtime database in firebase
- enabled email/password authentication in firebase
- changed the rules of realtime database to "true"
- connected Android Studio project to Firebase and added appropriate SDKs (Authentication SDKs and realtime database SDKs)
- checked if gradle is using appropriate dependencies
- redownloaded google-services.json file and replaced the one in a project
- created new clean project and connected it to firebase realtime database
Solution
The URL in getReferenceFromUrl()
seems to be the problem. You are passing the console's URL instead of database URL. Try using getReference()
instead:
FirebaseDatabase.getInstance().getReference()
.child("data")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(...)
If you want to use getReferenceFromUrl()
then the URL must be:
https://<project_id><db-name?>.europe-west1.firebasedatabase.app/data
The domain might differ based on the database location but you can check the URL in console here followed by the path:
Answered By - Dharmaraj
Answer Checked By - Senaida (JavaFixing Volunteer)