Skip to content

Instantly share code, notes, and snippets.

@jraczak
Created October 1, 2016 00:35
Show Gist options
  • Save jraczak/f23682543bb4a0f2576af04839bcf439 to your computer and use it in GitHub Desktop.
Save jraczak/f23682543bb4a0f2576af04839bcf439 to your computer and use it in GitHub Desktop.
public String loadExerciseJSONFromAsset() {
String json;
try {
InputStream inputStream = getResources().openRawResource(R.raw.exercises);
int size = inputStream.available();
byte[] buffer = new byte[size];
inputStream.read(buffer);
inputStream.close();
json = new String(buffer, "UTF-8");
} catch (IOException exception) {
exception.printStackTrace();
return null;
}
createExercisesFromJSONString(json);
return json;
}
public void createExercisesFromJSONString(String jsonString) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
final JSONArray jsonArray = jsonObject.getJSONArray("exercises");
Log.d(LOG_TAG, "JSON is of type: " + jsonArray.getClass());
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createOrUpdateAllFromJson(Exercise.class, jsonArray);
}
});
} catch (JSONException exception) {
exception.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment