Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static String loadJSONFromResource(Context context, int resource) {
        if (resource <= 0 || context == null)
            return null;

        String json = null;
        InputStream is = context.getResources().openRawResource(resource);
        try {
            if (is != null) {
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                json = new String(buffer, "UTF-8");
            }
        } catch (IOException e) {
            return null;
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
        }

        return json;
    }
}