Java Driver for Firebase

I just started a new Project @ GitHub, called jfirebase. That is basically a Java Wrapper for the REST API of Firebase. Clone the git repo and build the package with Maven2 / Maven3 like this:

mvn -Dmaven.test.skip=true package

There are Unit Tests for the project but If you want to run the test cases you have to add your firebase channel to the test classes.

You can install the JAR file with this command into your local Maven Repository:

mvn -Dmaven.test.skip=true install

The Driver contains basically 5 methods to interact with Firebase.

  • boolean write(Map<String, String> map)
  • Reader read(String uri)
  • boolean delete(String uri)
  • void setChannel(String channel)
  • String setKey(String key)

Here is an example for writing data to Firebase.

Map data = new HashMap();
data.put("firstname", "Robert");
data.put("lastname", "Reiz");
IDriver driver = new Driver();
driver.setChannel("http://YOUR_CHANNEL_AT_FIREBASE");
driver.write(data);

Your channel would be something like: “http://demo.firebase.com/myprojectname&#8221;.

OK. Here is an example for reading data.

IDriver driver = new Driver();
driver.setChannel("http://demo.firebase.com/SampleChat/");
Reader reader = driver.read("users/jack");
try{
   ObjectMapper mapper = new ObjectMapper();
   User user = mapper.readValue(reader, User.class);
   System.out.println(user.getName());
} catch (Exception ex){
   ex.printStackTrace();
}

The “read” method returns a “java.io.Reader”. With that you can do what you want. I am using here the jackson-core-lgpl JSON Mapper to map the JSON String from Firebase to my Java class User.

The delete method is pretty straight forward:

IDriver driver = new Driver();
driver.setChannel("http://demo.firebase.com/SampleChat/");
boolean deleted = driver.delete("user/jack");

Let me know if you have questions.

Published by Robert Reiz

CEO @ VersionEye. Passionated software developer since 1998.

9 thoughts on “Java Driver for Firebase

  1. Thanks for building this. I had a little trouble getting it to work in the beginning but It is smooth sailing now. What else are you planning to do with the firebase project?

    1. Hey Ryan. Good question. I didn’t look at the firebase project since round about 6 weeks. Just because I am to busy with the versioneye.com project. And your are the first one who is giving me feedback to this firebase driver.
      If you have any change requests or bugs, than I will continue to work on this project. Your feedback is appreciated.

  2. How do you auth?
    Ex:
    ref.authWithPassword(“vic@apakau.com”, “fbozana129”, new Firebase.AuthResultHandler() {
    public void onAuthenticated(AuthData authData) {
    System.out.println(authData);
    try {
    Android.write(ref);
    } catch (Throwable e) {
    e.printStackTrace();
    }
    }
    public void onAuthenticationError(FirebaseError er) {
    System.out.println(er);
    }
    });

    Also, I may also try http://square.github.io/retrofit

  3. Hi Rob,

    Reading your blog post spawned the idea to also write a Java wrapper for the Firebase REST API, which attempts to be a bit closer to their Java and Javascript SDKs.

    It’s called rest-on-fire and is available over jCenter and Maven Central. Thought I mention it here as a possible alternative. More details can be found on my github repository (https://github.com/j-fischer/rest-on-fire).

    Cheers,
    Johannes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: