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”.
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.
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?
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.
Yea I am having trouble with the write method it throws java errors when I compile. I suspect it has to do with the HTTP.Client class.
I just pushed a bugfix to GitHub. Please try it with the new version and let me know if you still have problems.
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
I can recommend several things:
1) try to use gradle, in addition to maven;
2) integration with hibernate or similar framework (JPA)
Hibernate and JPA are not equal replacements for Firebase. That are 2 different pair of shoes.
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
Very good. Many Thanks for the post.