注册代码
//用于插入学生数据的代码我没有添加导入部分,因为当时没有提交这个问题。
public class StudentregisterActivity extends AppCompatActivity {
EditText stname;
EditText stphone;
EditText stemail;
EditText stpassword;
Button stbtn;
DatabaseReference studentref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.studentregister);
stname=findViewById(R.id.editTextTextPersonName);
stemail=findViewById(R.id.editTextTextEmailAddress);
stphone=findViewById(R.id.editTextPhone);
stpassword=findViewById(R.id.editTextTextPassword);
stbtn=findViewById(R.id.button7);
studentref=FirebaseDatabase.getInstance().getReference().child("Students");
stbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InsertStudentData();
}
});
}
private void InsertStudentData(){
String name=stname.getText().toString();
String email=stemail.getText().toString();
String phone=stphone.getText().toString();
String password=stpassword.getText().toString();
Students students=new Students(name,email,phone,password);
studentref.push().setValue(students);
Toast.makeText(StudentregisterActivity.this,"Register Successfully",Toast.LENGTH_SHORT).show();
}
}学生
package com.example.pariksha;
public class Students {
String name;
String email;
String phone;
String password;
public Students(String name, String email, String phone, String password) {
this.name = name;
this.email = email;
this.phone = phone;
this.password = password;
}
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}setting-gradle
//项目的分级设置
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://maven.xyz.com" }
}
}
rootProject.name = "Pariksha"
include ':app'gradle-properties
//项目的分级属性
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=truegradle-构建应用程序
app的//Gradle属性
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.pariksha"
minSdk 19
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-database:20.0.3'
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-analytics:20.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}构建分级项目
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
classpath 'com.google.gms:google-services:4.3.10'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}发布于 2022-01-06 04:54:35
首先,请查看为Firebase实时数据库定义的规则。如果您不使用Firebase Auth,您可以像这样更新规则。
然后转到实时数据库,然后将选项卡更改为 rules 和update rules。
{
"rules": {
".read": true,
".write": true
}
}然后你就可以写数据了。
private void InsertStudentData(){
String name=stname.getText().toString();
String email=stemail.getText().toString();
String phone=stphone.getText().toString();
String password=stpassword.getText().toString();
Students students=new Students(name,email,phone,password);
studentref.setValue(students).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(StudentregisterActivity.this,"Register Successfully",Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(StudentregisterActivity.this,"Register failed",Toast.LENGTH_SHORT).show();
}
});
}还有一件事。您正在使用多个依赖项,请更改
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-database:20.0.3'
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-analytics:20.0.2'至
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database'
implementation 'com.google.firebase:firebase-analytics'发布于 2022-01-06 05:04:28
确保以下事项:
设置因特网权限
<uses-permission android:name="android.permission.INTERNET" />将规则设置为公共
console
中
**
{
“rules”: {
“.read”: true,
“.write”: true
}
}**
最后,尝试使用下面的代码上传数据
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tasksRef = rootRef.child("Students").push();
Students students=new Students(name,email,phone,password);
tasksRef.setValue(student);https://stackoverflow.com/questions/70602410
复制相似问题