无法读取或写入Firebase实时数据库(Android Studio)

时间:2020-05-25 05:58:46

标签: java android firebase android-studio firebase-realtime-database

我认为我已连接到Firebase,因为我收到了终端的消息

I/FirebaseInitProvider: FirebaseApp initialization successful

我认为我设定了正确的规则

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

这是我的读写代码。

public class MainActivity extends AppCompatActivity {

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

    // Write a message to the database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");

    myRef.setValue("Hello, World!");

}

@Override
protected void onStart() {
    super.onStart();
    // Read from the database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            System.out.println(value);
            Log.d("T", "Value is: " + value);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("F", "Failed to read value.", error.toException());
        }
    });
}
}

非常奇怪的是,它可以打印值,但是数据永远不会在控制台中更新。

印刷价值

I/System.out: Hello, World!
D/T: Value is: Hello, World!

但在控制台中……enter image description here

2 个答案:

答案 0 :(得分:1)

您必须推送消息,以使它们不会每次都更新:

// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");

//use a map
Map<String, String> message = new HashMap<>();  
message.put("text", "Hello, World!");

myRef.push().setValue(message);

此后,您的消息将如下所示:

message
|
|----------randomID
           |-----text : "Hello, World!"
|
|

要阅读消息文本:

@Override
protected void onStart() {
    super.onStart();
    // Read from the database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds: dataSnapshot.getChildren()){
              //this loops through all random ids and grabs the text 
              //you can maybe store the text in some list and do whatever.
              String value = ds.child("text").getValue(String.class);
              System.out.println(value);
              Log.d("T", "Value is: " + value);

             }

        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("F", "Failed to read value.", error.toException());
        }
    });
}

答案 1 :(得分:0)

    tableUser.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         tv.setText(dataSnapshot.getValue().toString());
            Log.e(TAG, "onDataChange: " + dataSnapshot.getValue().toString());
            Toast.makeText(MainActivity.this, "" + dataSnapshot, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });