【问题标题】:Android App can't write data to Firebase Realtime databaseAndroid 应用无法将数据写入 Firebase 实时数据库
【发布时间】:2021-08-23 12:29:31
【问题描述】:

我一直在关注如何将数据写入 Firebase 实时数据库的指南。尽管我遵循了每条指令,但我的应用程序似乎无法正常工作。当我单击发送按钮时,数据库不会更新。我没有收到任何错误,所以我决定添加一个 phew 侦听器,但它们也不起作用(没有显示任何祝酒词)。 Firebase 工具表示所有依赖项都已正确设置。 Firebase 项目以测试模式启动,因此问题无法与规则联系起来。 我究竟做错了什么?我的活动代码如下所示:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    private EditText email,name,password;
    Button btn;
    private DatabaseReference ref;
    private String USER_KEY="User";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initListener();
    }
    public void init(){
        email=findViewById(R.id.editTextTextPersonName);
        name=findViewById(R.id.editTextTextPersonName2);
        password=findViewById(R.id.editTextTextPersonName3);
        ref = FirebaseDatabase.getInstance().getReference().child(USER_KEY);
        btn =findViewById(R.id.button);
    }
    public void initListener(){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id =ref.getKey();
                String ema=email.getText().toString();
                String nam=name.getText().toString();
                String p=password.getText().toString();
                User user = new User(id,ema,nam,p);
                DatabaseReference usersRef = ref.child("users");
                usersRef.push().setValue(user).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT).show();
                        System.out.println("Failed");
                    }
                }).addOnCanceledListener(new OnCanceledListener() {
                    @Override
                    public void onCanceled() {
                        Toast.makeText(MainActivity.this,"Canceled",Toast.LENGTH_SHORT).show();
                        System.out.println("Canceled");
                    }
                }).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(MainActivity.this,"Succeed",Toast.LENGTH_SHORT).show();
                        System.out.println("Succeed");
                    }
                }).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(MainActivity.this,"Complete",Toast.LENGTH_SHORT).show();
                        System.out.println("Complete");
                    }
                });
                Toast.makeText(MainActivity.this,"end",Toast.LENGTH_SHORT).show();

            }

        });
    }

}

用户类别:

public class User {
    public String email,password,nickName;

    public User(String email, String password, String nickName) {
        this.email = email;
        this.password = password;
        this.nickName = nickName;
    }

    public User() {
    }
}

Gradle 构建文件:

  plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 18
        targetSdkVersion 30
        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
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

另外,这是我第一次来这里,所以请不要无礼。

【问题讨论】:

  • 您是否在创建数据库后从 Firebase 导入了 JSON 文件?如果您在创建数据库之前导入它,您将没有对它的引用。
  • @StefanoLeone 是的,我正在尝试在创建数据库后上传我的文件。如果您描述的情况(我没有得到任何),它不应该抛出异常吗?
  • 我说的是 google-services.json 文件。创建数据库后需要重新下载导入。
  • @StefanoLeone 非常感谢

标签: java android firebase firebase-realtime-database


【解决方案1】:

你可以在 firebase 项目设置中插入你的项目 sha1 密钥,而不是下载 google json 文件并保存在你在 android studio 的 app 文件夹中。

【讨论】:

    【解决方案2】:

    只需要重新导入 google-services.json 文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多