【问题标题】:realtime database update protocol is not executing实时数据库更新协议未执行
【发布时间】:2021-04-08 08:45:28
【问题描述】:

我正在使用 firebase 在我的应用中实现聊天功能。我按照教程进行了这封信,但我不确定为什么实时数据库在发出请求时没有更新。它说firebase在代码库中连接。实现的代码是请求和推送数据到实时数据库的标准代码。希望能深入了解 sendMessage 和 addValueEventListener 中逻辑失败的位置

package com.example.groupin;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class chatActivity extends AppCompatActivity {


    private DatabaseReference myDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        myDatabase = FirebaseDatabase.getInstance().getReference("Message");

        TextView myText = (TextView) findViewById(R.id.textView8);

        myDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                myText.setText(dataSnapshot.getValue().toString());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                myText.setText("CANCELLED");
            }
        });
    }

    public void sendMessage(View view){
        EditText myEditText = (EditText) findViewById(R.id.editTextTextPersonName);

        myDatabase.setValue(myEditText.getText().toString());
        myEditText.setText("");
    }
}

【问题讨论】:

  • why this chat won't work 什么不起作用?它在做什么/不做什么?
  • 这段代码中到底有什么不符合您的预期?告诉我们共享代码有什么问题。你有什么错误吗?
  • @AlexMamo。我很抱歉没有说清楚。当聊天活动打开时,它应该能够向 Firebase 实时数据库发送消息并接收它们。随着一切设置正确,我看到它实际上是连接的,但是当我运行它时,当我检查 firebase 控制台时消息没有被更新。它们也没有在应用程序本身中更新。 myText 元素是聊天功能的主体,您可以在其中看到消息被发回和第四个,它本身并没有更新。
  • @a_local_nobody 请查看我发送给 Alex Mamo 先生的回复
  • 我认为问题特别在于 addValueEventListener。它实际上并没有在监听任何变化,我只是对为什么会这样感到困惑。

标签: android firebase-realtime-database chat


【解决方案1】:

问题归结为对某些 API 的错误引用。这是我的一个错误。 Firebase 必须配置为在正确的位置引用正确的实时数据库,否则无法触发或接收响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    相关资源
    最近更新 更多