无法在Firebase数据库中保存消息

时间:2019-07-23 09:16:10

标签: java android firebase firebase-realtime-database

我正在尝试将消息对象保存在firebase数据库中,当我运行代码时没有出现任何错误,一切看起来都很好,但是当我访问firebase网站时,数据库中什么都没出现

public class MainChatActivity extends AppCompatActivity {
    private DatabaseReference mDatabaseReference;

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

        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage();
            }
        });
    }

    private void sendMessage() {
        String input = mInputText.getText().toString();
        if(!input.equals("")){
            InstantMessage chat=new InstantMessage(input, mDisplayName);
            mDatabaseReference.child("children").push().setValue(chat);
            mInputText.setText("");
        }
    }
}

public class InstantMessage {
    private String message;
    private String author;

    public InstantMessage(String message, String author) {
        this.message = message;
        this.author = author;
    }

    public InstantMessage() {
    }

    public String getMessage() {
        return message;
    }

    public String getAuthor() {
        return author;
    }
}

我希望将具有作者姓名的消息保存在Firebase中名为“消息”的目录中,但这没有发生

3 个答案:

答案 0 :(得分:1)

将您的Firebase数据库规则更改为以下内容:

{
  "rules": {
    ".read": true
    ".write": true
  }
}

它允许在任何情况下对所有用户进行读/写访问。

警告:从不在生产中使用此规则集;它允许任何人覆盖您的整个数据库。

答案 1 :(得分:0)

似乎是许可问题

确保您对数据库具有读/写权限,请检查规则并将其公开

{
  "rules": {
    ".read": true
    ".write": true
  }
}

答案 2 :(得分:0)

尝试一下:

    public class MainChatActivity extends AppCompatActivity {
    private DatabaseReference mDatabaseReference;

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

        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendMessage();
            }
        });
    }

    private void sendMessage() {
        String input = mInputText.getText().toString();
        if(!input.equals("")){
            InstantMessage chat=new InstantMessage(input, mDisplayName);
            String key = mDatabaseReference.push().getKey();
            mDatabaseReference.child("messages").child(key).setValue(chat) //here you can add a Listener to check success;
            mInputText.setText("");
        }
    }
}

public class InstantMessage {
    private String message;
    private String author;

    public InstantMessage(String message, String author) {
        this.message = message;
        this.author = author;
    }

    public InstantMessage() {
    }

    public String getMessage() {
        return message;
    }

    public String getAuthor() {
        return author;
    }
}