Advertisement
Guest User

firebase java

a guest
Oct 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. package www;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.concurrent.CountDownLatch;
  8.  
  9. import com.google.auth.oauth2.GoogleCredentials;
  10. import com.google.firebase.FirebaseApp;
  11. import com.google.firebase.FirebaseOptions;
  12. import com.google.firebase.database.DataSnapshot;
  13. import com.google.firebase.database.DatabaseError;
  14. import com.google.firebase.database.DatabaseReference;
  15. import com.google.firebase.database.FirebaseDatabase;
  16. import com.google.firebase.database.ValueEventListener;
  17.  
  18.  
  19. public class app {
  20. public static void main(String[] args) {
  21. // TODO Auto-generated method stub
  22.  
  23. final String json="E:\\\\Thesis\\\\4.2\\\\Resources\\\\rakib\\\\google-services-json\\\\google-services.json";
  24.  
  25. try {
  26.  
  27. InputStream inputStream = new FileInputStream(json);
  28. FirebaseOptions options = new FirebaseOptions.Builder()
  29. .setDatabaseUrl("https://database-ff395.firebaseio.com/")
  30. .setCredentials(GoogleCredentials.fromStream(inputStream))
  31. .build();
  32. FirebaseApp.initializeApp(options);
  33.  
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch(IOException e) {
  37. e.printStackTrace();
  38. }
  39.  
  40. }
  41.  
  42. private static String getDatafromFirebase() {
  43. CountDownLatch done = new CountDownLatch(1);
  44. StringBuilder b = new StringBuilder();
  45. DatabaseReference dbRef = FirebaseDatabase.getInstance()
  46. .getReference();
  47.  
  48. dbRef.child("topics").addValueEventListener(new ValueEventListener() {
  49. @Override
  50. public void onDataChange(DataSnapshot snapshot) {
  51. if(snapshot.exists()) {
  52. for(DataSnapshot s:snapshot.getChildren()) {
  53. Topics t = s.getValue(Topics.class);
  54. b.append(t.getName());
  55. b.append(" ");
  56. }
  57. done.countDown();
  58. }
  59. else {
  60. b.append("No exist");
  61. done.countDown();
  62. }
  63.  
  64. }
  65.  
  66. @Override
  67. public void onCancelled(DatabaseError error) {
  68. b.append("Error: "+error.getDetails());
  69. done.countDown();
  70. }
  71. });
  72. try {
  73. done.await();
  74. } catch (InterruptedException e) {
  75. e.printStackTrace();
  76. }
  77. return b.toString();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement