I'm working on a program right now, I wanted to send the "Admin" string to firebase, it worked with the Emulator from the Android Studio, but when I run it on my Phone, it didn't work, so what's wrong with my program?

MainActivity.java

private void addPostDatabase(){
    String content = editPost.getText().toString().trim();
    String name = "Admin";
    long id = System.currentTimeMillis();

    if(TextUtils.isEmpty(content)){
        Toast.makeText(this, "Please fill the box", Toast.LENGTH_LONG).show();
    }
    else{
        String identifier = "post" + id;
        User user = new User(content, name);
        databaseUser.child(identifier).setValue(user);
        Toast.makeText(this, "Content added", Toast.LENGTH_LONG).show();
        id++;
        backToMain();
    };
}

private void backToMain(){
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

User.java

public class User {
String name;
String content;

public User(){

}

public User(String content, String name) {

    this.content = content;
    this.name = name;
}


public String getContent() {
    return content;
}

}

1 Answers

1
Community On

attach listeners to your query like this to check for possible errors

databaseUser.child(identifier).setValue(user)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Write was successful!
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Write failed see log for details
                // Log.i(TAG , "e : " + e )
            }
        });