1

I have created a normal Sign Up page in Android Studio and I have linked this to Firebase. The language used is java.

"Signup.java"

package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Pair;
import android.util.Patterns;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Signup extends AppCompatActivity {
TextInputLayout regName, regEmail, regPhoneNo, regPassword, regConfirmPassword;
Button regSignInBtn, regToLoginBtn;
FirebaseDatabase rootNode;
DatabaseReference reference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_signup);

    regName = findViewById(R.id.reg_name_txt);
    regEmail = findViewById(R.id.reg_mail_txt);
    regPhoneNo = findViewById(R.id.reg_phone_no_txt);
    regPassword = findViewById(R.id.reg_password_txt);
    regConfirmPassword = findViewById(R.id.reg_confirm_password);
    regSignInBtn = findViewById(R.id.reg_signin_btn);
    regToLoginBtn = findViewById(R.id.reg_login_btn);

    regSignInBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            rootNode = FirebaseDatabase.getInstance();
            reference = rootNode.getReference("Users");

            String name = regName.getEditText().getText().toString();
            String email = regEmail.getEditText().getText().toString();
            String phoneNo = regPhoneNo.getEditText().getText().toString();
            String password = regPassword.getEditText().getText().toString();
            String confirmPassword = regConfirmPassword.getEditText().getText().toString();

            if (!name.isEmpty() && !email.isEmpty() && !phoneNo.isEmpty() && !password.isEmpty() && !confirmPassword.isEmpty()) {
                /*&& phoneNo.length() == 10 && password == confirmPassword && Patterns.EMAIL_ADDRESS.matcher(email).matches()*/
                UserHelperClass helperClass = new UserHelperClass(name, email, phoneNo, password, confirmPassword);
                reference.setValue(helperClass);
                Toast.makeText(Signup.this, "User Created!!! Kindly Login!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Signup.this,"Kindly Check All the Values!!!",Toast.LENGTH_LONG).show();
            }
            }
        });
    }
 }

I have created a separate class to get the values and the code is below:

"Helperclass.java"

package com.example.test;

public class UserHelperClass {
String name, email, phoneNumber, password, confirmPassword;

public UserHelperClass() {
    // Empty Constructor for DB
}

public UserHelperClass(String name, String email, String phoneNumber, String password, String confirmPassword) {
    this.name = name;
    this.email = email;
    this.phoneNumber = phoneNumber;
    this.password = password;
    this.confirmPassword = confirmPassword;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

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

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getConfirmPassword() {
    return confirmPassword;
}

public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;
    }
}

Now when I click the Sign In button I see there is no errors and the code is able to get into the If-Else Loop created in the "Signup.java" file.

But then comes the issue. The user details are not getting into the Firebase I am using.

I have even checked the rules and I have added the dependencies.

Android Studio Dependencies:

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'com.google.firebase:firebase-database:20.0.5'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation platform('com.google.firebase:firebase-bom:30.1.0')
    implementation 'com.google.firebase:firebase-database'

Firebase Rules:

{
  "rules": {
    ".read": true,  
    ".write": true, 
  }
}

Sign Up confirmation from Android Studio when it enters If loop

No Data getting sent into Firebase

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Adur
  • 11
  • 1
  • There are *a lot* of things that could be going wrong here, so I always recommend simplifying the problem. For example, if you do `FirebaseDatabase.getInstance().getReference().child("test").setValue(true);` does it write a value to the database? If not, the problem is much simpler - and I actually recommend checking the logcat output for warnings when that statement executes. – Frank van Puffelen May 28 '22 at 22:51
  • You need to give a child name before saving in your case you directly doing `reference.setValue(helperClasss)` instead you can do something like this `reference.child('UserID_OR_RANDOM_KEY").setValue(helperClass)` – Vishal Beep May 29 '22 at 05:17
  • I have tried child name also, But it doesn't work.... I have given the below line.. reference.child(phoneNo).setValue(helperClass); – Adur May 29 '22 at 07:10
  • @FrankvanPuffelen, I have tried logcat and found the below message.. "2022-05-29 12:43:07.586 8029-8082/com.example.test W/PersistentConnection: pc_0 - Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Database lives in a different region" I think I know the issue now... Will try fixing that... I had given South Asia (Singapore) on my firebase... – Adur May 29 '22 at 07:14
  • I was able to check on the location and found that providing the database url will work. And it did work as needed. rootNode = FirebaseDatabase.getInstance("https://test-7ba58-default-rtdb.firebaseio.com/"); This was provided in the below link: https://stackoverflow.com/questions/68806876/firebase-realtime-database-connection-killed-different-region Thanks @FrankvanPuffelen as I see that you have answered even that question. – Adur May 29 '22 at 08:01

0 Answers0