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.