I am trying to save a message object in the firebase database, when I run the code I get no errors, everything looks to be fine but when I go to firebase website, nothing appears in the database
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;
}
}
I expect to save the message with author name in a directory named "messages" in firebase, but that's not happening
Change your firebase database rules to the following:
{
"rules": {
".read": true
".write": true
}
}
It allows read/write access to all users under any conditions.
Warning: NEVER use this ruleset in production; it allows anyone to overwrite your entire database.
Make sure you are permission for READ/WRITE on the database, check the rules and make them public
{
"rules": {
".read": true
".write": true
}
}
try this:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.