Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package www;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.concurrent.CountDownLatch;
- import com.google.auth.oauth2.GoogleCredentials;
- import com.google.firebase.FirebaseApp;
- import com.google.firebase.FirebaseOptions;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- public class app {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- final String json="E:\\\\Thesis\\\\4.2\\\\Resources\\\\rakib\\\\google-services-json\\\\google-services.json";
- try {
- InputStream inputStream = new FileInputStream(json);
- FirebaseOptions options = new FirebaseOptions.Builder()
- .setDatabaseUrl("https://database-ff395.firebaseio.com/")
- .setCredentials(GoogleCredentials.fromStream(inputStream))
- .build();
- FirebaseApp.initializeApp(options);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- private static String getDatafromFirebase() {
- CountDownLatch done = new CountDownLatch(1);
- StringBuilder b = new StringBuilder();
- DatabaseReference dbRef = FirebaseDatabase.getInstance()
- .getReference();
- dbRef.child("topics").addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- if(snapshot.exists()) {
- for(DataSnapshot s:snapshot.getChildren()) {
- Topics t = s.getValue(Topics.class);
- b.append(t.getName());
- b.append(" ");
- }
- done.countDown();
- }
- else {
- b.append("No exist");
- done.countDown();
- }
- }
- @Override
- public void onCancelled(DatabaseError error) {
- b.append("Error: "+error.getDetails());
- done.countDown();
- }
- });
- try {
- done.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return b.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement