22. public class BlogPost {
private String author;
private String title;
public BlogPost() {
// empty default constructor
// necessary for Firebase to be able to deserialize blog posts
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
}
39. // Get a reference to our posts
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
BlogPost post = postSnapshot.getValue(BlogPost.class);
System.out.println(post.getAuthor() + " - " + post.getTitle());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
40. // Get a reference to our posts
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
BlogPost post = postSnapshot.getValue(BlogPost.class);
System.out.println(post.getAuthor() + " - " + post.getTitle());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
41. // Get a reference to our posts
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
BlogPost post = postSnapshot.getValue(BlogPost.class);
System.out.println(post.getAuthor() + " - " + post.getTitle());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
42. Child Added Event
• ChildEventListener#onChildAdded
• Once for each existing child initially
• Every time new data is added
43. // Get a reference to our posts
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
ref.addChildEventListener(new ChildEventListener() {
// Retrieve new posts as they are added to the database
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
BlogPost newPost = snapshot.getValue(BlogPost.class);
System.out.println("Author: " + newPost.getAuthor());
System.out.println("Title: " + newPost.getTitle());
}
//... ChildEventListener also defines onChildChanged, onChildRemoved,
// onChildMoved and onCanceled, covered in later sections.
});
44. Child Changed Event
• ChildEventListener#onChildChanged
• Anytime a child is changed including its
descendants
45. @Override
public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {
String title = (String) snapshot.child("title").getValue();
System.out.println("The updated post title is " + title);
}
46. Child Removed Event
• ChildEventListener#onChildRemoved
• Anytime a child is removed
47. @Override
public void onChildRemoved(DataSnapshot snapshot) {
String title = (String) snapshot.child("title").getValue();
System.out.println("The blog post titled " + title + " has been deleted");
}
48. Child Moved Event
• ChildEventListener#onChildMoved
• Anytime a child is moved
49. @Override
public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
String title = (String) snapshot.child("title").getValue();
System.out.println("The updated post title is " + title);
}
56. public class DinosaurFacts {
long height;
double length;
long weight;
public DinosaurFacts() {
// empty default constructor
// necessary for Firebase to be able to deserialize blog posts
}
public long getHeight() {
return height;
}
public double getLength() {
return length;
}
public long getWeight() {
return weight;
}
}
72. • helper libraries to integrate with your own
authentications
• https://www.firebase.com/docs/android/
guide/login/custom.html#section-rest-token-
helper-libraries
73. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
}
});
74. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
}
});
75. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
}
});
given from your server
76. Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticationError(FirebaseError error) {
System.err.println("Login Failed! " + error.getMessage());
}
@Override
public void onAuthenticated(AuthData authData) {
System.out.println("Login Succeeded!");
}
});