← All Articles

Simple Android utility to parse config files in your app

If you ever had to load similar content or configuration values into different Android apps before, you know how awesome it would be if you could just edit some basic JSON files and the rest is done for you. 

This is a simple utility class I wrote to do just that. In this example, I am using it to load a list of locations for display in an app and then to display in a ListView.

Drop in your JSON file

Firstly, make sure you have a valid JSON file. I leave it as res/raw/locations.json, but you may want to place this in your assets folder also (it doesn't make too much of a difference).


{
  "items": [
    {
      "title": "Arkadia",
      "details": "Perkantoran Hijau Arcadia \nJl. Tb Simatupang Kav 88 Tower C \nlt. Dasar",
      "thumbnail": "image_location_1",
      "link": "-6.302118,106.833844"
    },
    {
      "title": "Jamsostek",
      "details": "Menara Jamsostek South Tower Ground Floor \nJl. Jendral Gatot Subroto no 38 \nJakarta Selatan.",
      "thumbnail": "image_location_2",
      "link": "-6.233651,106.821655"
    },
    {
      "title": "Summitmas",
      "details": "Summitmas Gd. summitmas 1 \nJl. Jendral sudirman kav. 61-62 \nJakarta 12190",
      "thumbnail": "image_location_3",
      "link": "-6.226307,106.803862"
    },
    //** and so on **//
  ]
}

 

The FileReader class

I usually keep this class stored at main/utilities/FileReader.java. There are three main functions:

  • a public method processFile() used to call this class (eg from any Activity) and set which JSON file you want to read;
  • a private method readRawTextFile() used to read the JSON file into a string;
  • another private method parseJsonFromString() used to parse the string into an ArrayList which can then be used.

Here's what it looks like:


package my.package.name;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import my.package.name.models.ContentBlock;

public class FileReader {

  public FileReader(Context context){
    mContext = context;
  }
  private static Context mContext;
  private static int mResourceId;

  public ArrayList processFile(int resourceId)
  {
    mResourceId = resourceId;
    ArrayList contentList = new ArrayList();

    String jsonStr = this.readRawTextFile();
    try {
      contentList = this.parseJsonFromString(jsonStr);
    } catch(Exception e){
      e.printStackTrace();
    }

    return contentList;
  }

  private static String readRawTextFile()
  {
    InputStream inputStream = mContext.getResources().openRawResource(mResourceId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();

    try {
      while (( line = buffreader.readLine()) != null) {
        text.append(line);
      }
    } catch (IOException e) {
      return null;
    }

    return text.toString();
  }

  private ArrayList parseJsonFromString(String jsonString)
      throws JSONException {

    ArrayList contentList = new ArrayList();

    try {
      JSONObject contentJson = new JSONObject(jsonString);

      JSONArray itemArray = contentJson.getJSONArray("items");

      for(int i = 0; i < itemArray.length(); i++) {
        String title="", details="", thumbnail="", link="";

        JSONObject itemDetails = itemArray.getJSONObject(i);

        if (!itemDetails.isNull("title")) { title = itemDetails.getString("title"); }
        if (!itemDetails.isNull("details")) { details = itemDetails.getString("details"); }
        if (!itemDetails.isNull("thumbnail")) { thumbnail = itemDetails.getString("thumbnail"); }
        if (!itemDetails.isNull("link")) { link = itemDetails.getString("link"); }

        ContentBlock newContent = new ContentBlock(mContext, title, details, thumbnail, link);

        contentList.add(newContent);
      }

    } catch (JSONException e) {
      e.printStackTrace();
    }

    return contentList;
  }
}

Note that I read the contents from the JSON file and use it to construct a ContentBlock object, which is a custom class defined in my models folder. This is just a simple representation of a real-world object.

 

Using the FileReader

In your calling Activity then, you can simply call this with:


// somewhere in your activity
ArrayList<ContentBlock> mContentList = new FileReader(getBaseContext()).processContentFile(R.raw.locations);

This will read the locations.json file from your res/raw directory, and return you with an ArrayList of ContentBlock objects. You may be surprised what a handy time-saver this is, I use it to read in configuration settings all the time.

Made with JoyBird