代码中没有显示任何错误。我在build.gradle(project)和build.gradle中添加了一些代码(module:app). 我不知道到底是什么问题,为什么在android real设备中输入的数据仍然不能在firebase中显示。
下面是我的userhelperclass.java包com.example.bloodpressuremonitoring1的代码;
public class UserHelperClass {
String username, email, age, weight, password;
public UserHelperClass() {
}
public UserHelperClass(String username, String email, String age, String weight, String password) {
this.username = username;
this.email = email;
this.age = age;
this.weight = weight;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
register.java公共类register扩展appcompatactivity{
//Variables
TextInputLayout regUsername, regEmail, regAge, regWeight, regPassword;
Button regBtn, regToLoginBtn;
FirebaseDatabase rootNode;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Hooks to all xml elements in activity_register.xml
regUsername = findViewById(R.id.reg_username);
regEmail = findViewById(R.id.reg_email);
regAge = findViewById(R.id.reg_age);
regWeight = findViewById(R.id.reg_weight);
regPassword = findViewById(R.id.reg_password);
regBtn = findViewById(R.id.reg_btn);
regToLoginBtn = findViewById(R.id.reg_login_btn);
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (validateUsername() && validateEmail() && validateAge() && validateWeight() && validatePassword() == true) {
Intent intent = new Intent(Register.this, Login.class);
startActivity(intent);
} else {
validateUsername();
validateEmail();
validateAge();
validateWeight();
validatePassword();
}
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
//Get all the values
String name = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String age = regAge.getEditText().getText().toString();
String weight = regWeight.getEditText().getText().toString();
String password= regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, email, age, weight, password);
reference.child(age).setValue(helperClass);
}
});
regToLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Register.this,Login.class);
startActivity(intent);
}
});
}
private Boolean validateUsername() {
String val = regUsername.getEditText().getText().toString();
String noWhiteSpace = "\\A\\w{4,20}\\z";
if (val.isEmpty()) {
regUsername.setError("Field cannot be empty");
return false;
} else if (val.length() >= 15) {
regUsername.setError("Username too long");
return false;
} else if (!val.matches(noWhiteSpace)) {
regUsername.setError("White Spaces are not allowed");
return false;
} else {
regUsername.setError(null);
regUsername.setErrorEnabled(false);
return true;
}
}
private Boolean validateEmail() {
String val = regEmail.getEditText().getText().toString();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (val.isEmpty()) {
regEmail.setError("Field cannot be empty");
return false;
} else if (!val.matches(emailPattern)) {
regEmail.setError("Invalid email address");
return false;
} else {
regEmail.setError(null);
return true;
}
}
private Boolean validateAge() {
String val = regAge.getEditText().getText().toString();
if (val.isEmpty()) {
regAge.setError("Field cannot be empty");
return false;
} else {
regAge.setError(null);
return true;
}
}
private Boolean validateWeight() {
String val = regWeight.getEditText().getText().toString();
if (val.isEmpty()) {
regWeight.setError("Field cannot be empty");
return false;
} else {
regWeight.setError(null);
return true;
}
}
private Boolean validatePassword() {
String val = regPassword.getEditText().getText().toString();
String passwordVal = "^" +
"(?=.*[0-9])" + //at least 1 digit
"(?=.*[a-z])" + //at least 1 lower case letter
"(?=.*[A-Z])" + //at least 1 upper case letter
"(?=.*[a-zA-z])" + //any letter
"(?=.*[@#$%^&+=])" + //at least 1 special character
"(?=\\S+$)" + //no white spaces
".{4,}" + //at least 4 characters
"$";
if (val.isEmpty()) {
regPassword.setError("Field cannot be empty");
return false;
} else if (!val.matches(passwordVal)) {
regPassword.setError("Password is too weak");
return false;
} else {
regPassword.setError(null);
regPassword.setErrorEnabled(false);
return true;
}
}
public void registerUser(View view) {
if(!validateUsername() | !validateEmail() | !validateAge() | !validateWeight() | !validatePassword() ){
return;
}
//Get all the values in String
String name = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String age = regAge.getEditText().getText().toString();
String weight = regWeight.getEditText().getText().toString();
String password= regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, email, age, weight, password);
reference.child(name).setValue(helperClass);
}
}
build.gradle(血压监测1)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.google.gms:google-services:4.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir}
build.gradle(:app)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.bloodpressuremonitoring1"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:16.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.material:material:1.0.0'
implementation platform('com.google.firebase:firebase-bom:26.2.0')
implementation 'com.google.firebase:firebase-analytics'
}
暂无答案!
目前还没有任何答案,快来回答吧!