这时尝试在给定类别下添加新子项时仅打印“子项”。 最终目标是打印添加的新子代的值,因此,假设我的设备在“数据/温度”下添加了新的温度值
public static void startListeners(){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/data/temperature");
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()) {
System.out.println(child);
}
}
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static void main(String[] args) {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.create(null))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp.initializeApp(options);
} catch (Exception e) {
System.out.println(e.getMessage());
}
while(true) {
try {
startListeners();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}