无法从Android手机将数据发送到Firebase数据库

时间:2019-04-11 02:55:57

标签: java android firebase firebase-realtime-database

我现在正在开发一个程序,我想将“ Admin”字符串发送到firebase,它可以与Android Studio中的Emulator一起使用,但是当我在手机上运行它时,它没有没用,那么我的程序怎么了?

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 个答案:

答案 0 :(得分:1)

像这样将侦听器附加到查询中,以检查可能的错误

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 )
            }
        });