android - Firebase realtime database is not connecting
问题描述
Iam trying to use firebase real-time database. I have set rules to true and in my project added all required dependencies and also internet permission. My mobile is very well connected to the wifi.
When I tried to use write the data then it's not working. So I checked the status as per documentation and it's showing dB is not connected to my app.
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
Log.d(TAG, "connected");
} else {
Log.d(TAG, "not connected");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "Listener was cancelled");
}
});
Am I doing anything wrong hwre?
Appreciate help.
PD
解决方案
您应该添加查询
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference();
Query query = connectedRef.child("info/connected");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
Log.d(TAG, "connected");
} else {
Log.d(TAG, "not connected");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.w(TAG, "Listener was cancelled");
}
});
推荐阅读
- f# - F#: [
] vs 覆盖 __.ToString()。¿ 怎么了? - delphi - delphi中的搜索自动化?
- python - 如何解决 Django 查询“'ExtractHour' 对象没有属性 'split'”错误?
- ios - 颤振 -
使用 google_sign_in 库时未找到 - r - 嵌套小标题,执行操作,以不同的形式取消嵌套和嵌套数据
- ionic-framework - 如何从 Firestore 集合创建一个 ion-reorder-group
- java - 如何以 .war 和 .jar 的形式构建/运行 maven 项目?
- symfony - FatalErrorException 编译错误:App\Controller\DefaultController::forward 的声明
- rdf - 如何使用 JSON-LD 序列化的 RDF 图来描述设备功能?
- java - 为什么在 jpanel.paintComponent() 中调用 setText() 方法时 JLabel 不刷新?