public static class Post {
public String author;
public String title;
public Post(String author, String title) {
// ...
}
}
...
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl(DATABASE_URL)
.build();
FirebaseApp.initializeApp(options);
System.out.println("0");
final FirebaseDatabase database = FirebaseDatabase.getInstance(DATABASE_URL);
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");
System.out.println("1");
// Attach a listener to read the data at our posts reference
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
System.out.println("2");
Post newPost = dataSnapshot.getValue(Post.class);
System.out.println("3");
System.out.println("Author: " + newPost.author);
System.out.println("Title: " + newPost.title);
System.out.println("Previous Post ID: " + prevChildKey);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
该代码是从Firebase网站的教程中复制的。在“ 2”之后停止打印。 Eclipse控制台上没有出现错误。我已经更改了很多次代码,但仍然无法正常工作。顺便说一句,我可以将数据写入数据库。
这是数据结构: