load JSON From Asset - Android App

Android examples for App:Assets

Description

load JSON From Asset

Demo Code


//package com.java2s;
import android.content.Context;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String loadJSONFromAsset(Context context, String jsonFile) {
        String json = null;/*from   www.j a  v  a  2 s  . c o  m*/
        try {
            InputStream is = context.getAssets().open(jsonFile);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
}

Related Tutorials