6

Is there a way to retrieve data from Firebase to a Java application ?

I have an Android application which stores the data in to a Firebase database and I need to read that data in a Java application. I searched over the Internet, but I have not found a clear answer.

//not the URL from browser, the one above the database

        FirebaseOptions options = new FirebaseOptions.Builder()
            .setDatabaseUrl("https://database-ff395.firebaseio.com/")
            .setServiceAccount(new 
      FileInputStream("C:/Users/Denisa/Desktop/database-2aab74679b79.json"))
            .build();
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase
                .getInstance()
                .getReference("restricted_access/secret_document");

//here I try to reference the database and put a value in it

            final FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference ref1 = database.getReferenceFromUrl("https://database-ff395.firebaseio.com/database-ff395");
            ref1.setValue("ooo");
Denisa Corbu
  • 301
  • 2
  • 16

1 Answers1

5

Accessing the Firebase Database from a regular (non-Android) JVM is considered a server platform by the Firebase documentation. You can find all about it in the Firebase documentation for its server SDKs.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • One more question: The application should be an web app ? – Denisa Corbu Jun 22 '16 at 17:31
  • I'm not sure what you mean by "web app" in this context. Can you elaborate? – Frank van Puffelen Jun 22 '16 at 17:37
  • I made a simple "Java simple app" with public static void main.. and I added the code from the link you gave me. It doesn't work. On the "Prerequisits" it says you have to have "a server running Java 7 or later". I'm not sure what does it means. Should I make a web application running on Tomcat? – Denisa Corbu Jun 22 '16 at 17:44
  • The machine that you run the code on must have Java 7 or greater. Most likely you already have that. What doesn't work? Any errors? – Frank van Puffelen Jun 22 '16 at 17:47
  • No errors. I put a simple string to print in the OnDataChange method and I changed manually from firebase..it doesn't print anything. Also I created a reference to the firebase DB and try to set a value to a node but it doesn't work. – Denisa Corbu Jun 22 '16 at 17:51
  • Without seeing your code, all we can do is guess. Please edit your question to include a [MCVE](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jun 22 '16 at 18:04
  • That's not how Stack Overflow works. You can [edit](http://stackoverflow.com/posts/37905828/edit) your question to include the **minimal** code to reproduce the problem. If you cannot reduce to a reasonable size and readable code, it is unlikely that I can efficiently help you. – Frank van Puffelen Jun 23 '16 at 04:04
  • Thank you, but I have a misunderstanding. In Firebase says I need a Server app, and I don't know exactly what kind of application should I make and also I did not understand how they communicate. Because I made a simpla Java application and It seems to me that the event o changing the data is never triggered. – Denisa Corbu Jun 23 '16 at 07:27
  • The code looks fine at first glance. Is this similar to the code you have in your Android app? Does the Android app work? – Frank van Puffelen Jun 23 '16 at 16:03
  • In android I am using this https://www.firebase.com/docs/android/quickstart.html and it works. At this point, I'm trying to read something on the server using this https://firebase.google.com/support/guides/firebase-android#get_a_database_reference_numbered , regardless the android part. I was trying just to connect to a test database, and when I will manage to do so, i will adapt the android part to use the same database as the server. – Denisa Corbu Jun 24 '16 at 13:04
  • I managed to fix it.. It was a dependency error.. But I also wanted to ask you, if there is a way to work with 2 firebase database from the same Android app? – Denisa Corbu Jun 25 '16 at 06:24
  • Good to hear that you found the problem. For accessing multiple databases from a single Android app, see: http://stackoverflow.com/questions/37634767/how-to-connect-to-more-than-one-firebase-database-from-an-android-app/37643374#37643374 – Frank van Puffelen Jun 25 '16 at 15:04