Android setValue()未将数据保存到我的实时数据库

Android setValue()未将数据保存到我的实时数据库,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我试图使用setValue()方法将数据保存到实时数据库,但它没有保存我的对象 我试着用一个简单的字符串,但仍然不起作用 我的代码: FirebaseDatabase db = FirebaseDatabase.getInstance(); DatabaseReference ref = db.getReference(); ref.setValue("user"); Toast.makeText(RegisterApp.this, &

我试图使用setValue()方法将数据保存到实时数据库,但它没有保存我的对象

我试着用一个简单的字符串,但仍然不起作用

我的代码:

    FirebaseDatabase db = FirebaseDatabase.getInstance();
        DatabaseReference ref = db.getReference();
        ref.setValue("user");
Toast.makeText(RegisterApp.this, "Passe tranquille #1! Ref = " + ref.toString() + " | Db = " + db.toString(), Toast.LENGTH_LONG).show();
调用makeText并向我显示良好的信息,这些信息(应该)显示它已连接到数据库

但我的实时数据库仍然显示为null,而不是字符串“user”

格拉德尔

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.4.32'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.1'
        classpath 'com.google.gms:google-services:4.3.8'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
app/build.graddle,版本由我自己设置

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.projetl3"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    configurations {
        compile.exclude group: "guava", module: "guava"
        implementation.exclude module:'proto-google-common-protos'
        implementation.exclude module:'protolite-well-known-types'
        implementation.exclude module:'guava'
        implementation.exclude module:'protobuf-lite'

    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// CameraX core library using camera2 implementation
    implementation "androidx.camera:camera-camera2:1.1.0-alpha04"
// CameraX Lifecycle Library
    implementation "androidx.camera:camera-lifecycle:1.1.0-alpha04"
// CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha24"


    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation platform('com.google.cloud:libraries-bom:20.2.0')
    implementation 'com.google.cloud:google-cloud-vision'

    implementation 'com.google.api-client:google-api-client-android:1.22.0'
    implementation 'com.google.apis:google-api-services-vision:v1-rev357-1.22.0'
    implementation 'com.google.code.findbugs:jsr305:3.0.2'

    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:28.0.1')

    // Add the dependency for the Firebase SDK for Google Analytics
    // When using the BoM, don't specify versions in Firebase dependencies
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-database:20.0.0'
}
具有bom版本的app/build.graddle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.projetl3"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    configurations {
        compile.exclude group: "guava", module: "guava"
        implementation.exclude module:'proto-google-common-protos'
        implementation.exclude module:'protolite-well-known-types'
        implementation.exclude module:'guava'
        implementation.exclude module:'protobuf-lite'

    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/INDEX.LIST'
        exclude 'META-INF/DEPENDENCIES'
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// CameraX core library using camera2 implementation
    implementation "androidx.camera:camera-camera2:1.1.0-alpha04"
// CameraX Lifecycle Library
    implementation "androidx.camera:camera-lifecycle:1.1.0-alpha04"
// CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha24"


    implementation 'com.google.android.gms:play-services-vision:20.1.3'
    implementation platform('com.google.cloud:libraries-bom:20.2.0')
    implementation 'com.google.cloud:google-cloud-vision'

    implementation 'com.google.api-client:google-api-client-android:1.22.0'
    implementation 'com.google.apis:google-api-services-vision:v1-rev357-1.22.0'
    implementation 'com.google.code.findbugs:jsr305:3.0.2'

    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:28.0.1')

    // Add the dependency for the Firebase SDK for Google Analytics
    // When using the BoM, don't specify versions in Firebase dependencies
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-database'
}
我的规则:(我甚至试着把它们变成真的,甚至是“真的”)

{
“规则”:{
.read:“现在<1623794400000”//2021-6-16
.write:“现在<1623794400000”//2021-6-16
}
}

在有setValue失败原因解释的介质上查看这篇文章。
. 您可能需要检查Firebase规则

您需要使用
映射
在Firebase实时数据库中添加值。firebase实时数据库的工作方式与json类似

   DatabaseReference ref = db.getReference();
    Map<String, Object> users = new HashMap<>();
    users.put("user",   "Alan Turing");
    ref.setValueAsync(users);
databasereferef=db.getReference();
Map users=newhashmap();
用户。put(“用户”、“艾伦图灵”);
参考setValueAsync(用户);

有关更多详细信息,请参见:

您可以使用地图向数据库添加数据,正如@ahmad bajwa在其回答中所建议的,或者您可以使用模型类。考虑到您至少要编写一些用户数据,类的声明应该如下所示:

class User {
    public String userName;
}
Firebase-root
  |
  --- users
       |
       --- firstUser
             |
             --- name: "Constant Abrh"
现在,要将数据写入实时数据库,请使用以下代码行:

User user = new User("Constant Abrh");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
usersRef.child("firstUser").setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "User added successfully.");
        } else {
            Log.d"(TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

如果你想要,你也可以考虑实现,所以你可以稍后写。

嗨,谢谢你的快速回复,但是我没有StValuaSaveCyc,我错过了什么东西还是我的StValk?你也可以使用StValk。setValueAsync与setValueAsync相同,只是它可以作为异步任务使用。但是使用setValueAsync()仍然不起作用,setValueAsync()已经过时了我想因为它不可用,我的规则设置为true,我甚至尝试创建另一个Firebase projet,我尝试了Firebase文档中提到的映射,我甚至尝试了Android Studio中Firebase Assistant的一个,但仍然没有数据保存SetValueAsync在其官方网站上没有过时。尝试更新您的依赖项。忘记提到我的规则都已设置为true,因此它应该可以工作,但仍然无法工作。谢谢您,我将尝试您的代码,我确实创建了Firebase身份验证并工作,事实上我的问题是注册我的用户类,但在这里我甚至无法保存一个简单的字符串,即使我正在跟踪视频/论坛代码,我更新了我的帖子,也许你会发现我的问题是的,试试看,告诉我它是否有效。我想你的第二个Log.d(TAG)应该被“TAG”替换,你的任务应该被Task替换,刚刚更正了我的答案。我可能会将您的日志更改为Toast.makeText,因为我没有使用计算机启动Android仿真器/使用USB设备测试我的应用程序,但我正在构建APK,然后直接在手机上更新我的应用程序
Firebase-root
  |
  --- users
       |
       --- firstUser
             |
             --- name: "Constant Abrh"