Month: February 2016

Storing data to Firebase in Android

Data in Firebase

Data is stored as JSON objects in Firebase. Overall its one JSON tree, and when we add data, it becomes a key in this existing tree structure. Example of this tree structure is as follows:

{
   "users": {
      "John": {
         "DOB" : "1/1/1989",
         "Gender" : "Male"
      }
   }
}

Writing Data to Database

  1. Create a reference to the Firebase database with the URL from the previous post.
    Firebase ref = new Firebase(Constants.FIREBASE_URL);
  2. To create a child “Username” with some value like “John”, is done in the following way
    ref.child("Username").setValue("John");

 

How to connect your Android app to “Firebase” as backend solution

A real-time cloud database, Firebase provides back-end support for android applications which requires a global storage of data. It also provides services like user authentication and static hosting.

Some advantages of using Firebase as the back-end solution are as follows:

  1. Efficiently capable of handling the constant real-time data updates between devices when a lot of users manipulates the data
  2. Cloud based storage so readily available everywhere.
  3. Flexible and cross platform API.

Here I demonstrate how to connect your android application with Firebase service with a simple code snippet:

Step 1 : Create a free account here.  This creates a new Firebase app for you with a unique url ending in firebaseio.com

Step 2 : In build.gradle file of the app, add the following as a dependency :

dependencies {
    compile 'com.firebase:firebase-client-android:2.5.0+'
}

Step 3 : You might get a build error for duplicate files, add the following packagingOptions directive to the build.gradle file as a solution:

packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}

Step 4 Add the android.permission.INTERNET to your AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />

Step 5 : Initialize the Firebase library with an Android context. This needs to be done a firebase app reference is created or used. For this, first create an Application class in the following manner and then set the android context in the onCreate method of the application class.

import android.app.Application;
import com.firebase.client.Firebase;

public class MyApplicationClass extends Application  {
    @Override
    public void onCreate()
    {
        super.onCreate();
        Firebase.setAndroidContext(this);
    }
}

Step 6 : The next step is to save the firebase app url, which was created when you created an account with firebase , in your android project. This can be done in the following manner :

a. In gradle.properties file, create a variable UniqueFirebaseRootUrl and save the url in this variable.

  UniqueFirebaseRootUrl = "url.firebaseio.com"

b. In app’s build.gradle file, add a buildTypes.each directive in the following manner :

it.buildConfigField 'String','UNIQUE_FIREBASE_ROOT_URL', UniqueFirebaseRootUrl

c. Create a new file Constants.java to create a final Constants class and add a final String for the url in the following manner:

public final class Constants {
   public static final String FIREBASE_URL = BuildConfig.UNIQUE_FIREBASE_ROOT_URL;
}

This FIREBASE_URL can now be used to initialize any reference for firebase. In the next post, I’ll demonstrate how to create a firebase reference using this variable and store and retrieve data in Firebase cloud.