Firebase 实时数据库 - 来自代码的数据不会“到达”数据库

问题描述

我正在使用 Firebase 的实时数据库,以便在 android studio 中制作食谱应用程序。代码运行良好,没有任何崩溃,但每次它应该向数据库发送信息时,它没有。我试图为它创建一个 shell 以将数据放入其中,相反,我看到它一旦通过应该发送数据的部分就将其擦除。我已经根据他们的文档完成了所有必需的导入、实现和依赖项,并根据它编写了代码,但它似乎不起作用。

我发送数据前的 Firebase 实时数据库加上规则截图:

Firebase RD before

Firebase RD rules

运行逻辑发送数据后,我看到它变红然后消失,就像被删除一样,什么也没留下。

这是我的 HomeScreen.java 代码

package com.capteamfour.recipeappdatabase;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class HomeScreen extends AppCompatActivity {

final FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseUsers = mDatabase.getReference("USERS");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    newUser(FirebaseAuth.getInstance().getCurrentUser().getdisplayName(),FirebaseAuth.getInstance().getCurrentUser().getEmail());

    String username = FirebaseAuth.getInstance().getCurrentUser().getdisplayName();
    String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();

    TextView userWelcome = (TextView) findViewById(R.id.userWelcome);
    FloatingActionButton addRecipe = (FloatingActionButton) findViewById(R.id.addRecipeButton);
    ImageButton userProfile = (ImageButton) findViewById(R.id.userProfileButton);

    // Welcomes the current user
    userWelcome.setText("Welcome," + username + "!");

    // Creates a listener for the "add recipe" button; takes user to recipe form activity
    addRecipe.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent toRecipeForm = new Intent(HomeScreen.this,recipeForm.class);
            startActivity(toRecipeForm);
        }
    });

    // Creates a listener for the "user Profile Button" button; takes user to profile activity
    userProfile.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent ToProfile = new Intent(HomeScreen.this,Profile.class);
            startActivity(ToProfile);
        }
    });

    }

public void newUser(String name,String email)
{
    userProfile user = new userProfile(name,email);

    System.out.println("This is what I need to send: " + mDatabaseUsers.setValue(user));
    mDatabaseUsers.setValue(user);
}

}

我声明了一个数据库实例并创建了一个对 USERS 路径的引用作为全局变量,然后创建了一个函数 newUser(String name,String email) 为我创建了一个 userProfile() 类的实例,然后应该使用下一行将其发送到数据库(Sys.out 是我试图查看它尝试发送的内容,如果有的话)。

userProfile 类代码

package com.capteamfour.recipeappdatabase;

import com.google.firebase.database.IgnoreExtraProperties;

import java.util.ArrayList;
import java.util.List;

@IgnoreExtraProperties
public class userProfile {

private String username;
private String email;
private List<Recipe> subRecipes = new ArrayList<>();

// Not yet implemented
private List<Recipe> favRecipes = new ArrayList<>();
private List<Recipe> savedRecipes = new ArrayList<>();

// A user profile will include their name,the recipes they've submitted,as well as recipes they've
// favorited or saved to their profile. Saved recipes are private to the user.

public userProfile() {
    // Default constructor for DataSnapshot.getValue(userProfile.class) calls
}

public userProfile(String usernameIn,String emailIn)
{
    this.username = username;
    this.email = email;
}

public String getUsername () {
    return this.username;
}

public String getEmail() {
    return this.email;
}

/*
    Each of these add functions will check the length of the given array and either
    add to the end of it if there's already results,or just adds it on the empty array.

    Example: array empty,then array[0] = recipe
             array has values,then array[length + 1] = recipe
 */

public void addSubRecipe (Recipe recipe) {
    int size = this.subRecipes.size();
    if (size == 0) {
        subRecipes.add(recipe);
    }
    else {
        subRecipes.add(size+1,recipe);
    }
}

public List<Recipe> getSubRecipes() {
    return subRecipes.subList(0,(subRecipes.size()));
}
/*
These are commented out to prevent additional bugs and confusion while they're not implemented

public void addSavedRecipe (Recipe recipe) {
    int size = this.savedRecipes.size();
    if (size == 0) {
        savedRecipes.add(recipe);
    }
    else {
        savedRecipes.add(size + 1,recipe);
    }
}


    public List<Recipe> getSavedRecipes() {
    return this.savedRecipes.subList(0,(subRecipes.size() + 1));
    }

/*public void addFavRecipe (Recipe recipe) {
    int size = this.favRecipes.size();
    if (size == 0) {
        favRecipes.add(recipe);
    }
    else {
        favRecipes.add(size + 1,recipe);
    }
}

public List<Recipe> getFavRecipes() {
    return this.favRecipes.subList(0,(subRecipes.size() + 1));
}*/

}

在这个类中,我确保有一个带有额外构造函数和常用 get/set 函数的空构造函数,以便数据库可以使用它。

我感谢任何人可以提供的有关如何解决此问题的任何和所有指示。这是我的大学顶点课程,它对我们的影响很大,似乎没有充分的理由。谢谢大家。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)