首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想从下面的代码中从android添加实时数据库中的数据,但是它不应该工作

我想从下面的代码中从android添加实时数据库中的数据,但是它不应该工作
EN

Stack Overflow用户
提问于 2022-01-06 04:16:46
回答 2查看 56关注 0票数 0

注册代码

//用于插入学生数据的代码我没有添加导入部分,因为当时没有提交这个问题。

代码语言:javascript
复制
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();

    }
}

学生

代码语言:javascript
复制
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

//项目的分级设置

代码语言:javascript
复制
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven { url "https://maven.xyz.com" }
    }
}
rootProject.name = "Pariksha"
include ':app'

gradle-properties

//项目的分级属性

代码语言:javascript
复制
# 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=true

gradle-构建应用程序

app的//Gradle属性

代码语言:javascript
复制
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'

}

构建分级项目

代码语言:javascript
复制
// 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
}
EN

回答 2

Stack Overflow用户

发布于 2022-01-06 04:54:35

首先,请查看为Firebase实时数据库定义的规则。如果您不使用Firebase Auth,您可以像这样更新规则。

然后转到实时数据库,然后将选项卡更改为 rules 和update rules。

代码语言:javascript
复制
{  
 "rules": {
    ".read": true,
    ".write": true
  }
}

然后你就可以写数据了。

代码语言:javascript
复制
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();

        }
    });

}

还有一件事。您正在使用多个依赖项,请更改

代码语言:javascript
复制
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'

代码语言:javascript
复制
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'
票数 0
EN

Stack Overflow用户

发布于 2022-01-06 05:04:28

确保以下事项:

  1. Internet权限。
  2. 将Firebase实时数据库规则设置为用于测试设备上的私有
  3. 可用性的公共/身份验证用户,以将数据推入数据库。

设置因特网权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.INTERNET" />

将规则设置为公共

console

  • Realtime数据库规则

  • 将此代码粘贴到规则

**

代码语言:javascript
复制
{
 “rules”: {
 “.read”: true,
 “.write”: true
 }
}

**

最后,尝试使用下面的代码上传数据

代码语言:javascript
复制
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tasksRef = rootRef.child("Students").push();
Students students=new Students(name,email,phone,password);
tasksRef.setValue(student);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70602410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档