Created
October 1, 2016 00:35
-
-
Save jraczak/f23682543bb4a0f2576af04839bcf439 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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