I have a file with a size of about 1 MB JSON, which is stored in my resource folder, which I need to upload to my application every time it starts. I find that the built-in JSON parser (org.json) processes the file very slowly, however after analyzing it, I can quickly access and manipulate the data. I counted as much as 7 or 8 seconds from the moment I click on the application until Activity1 appears, but only a few milliseconds to go from Activity1 to Activity2, which depends on the data processed from the data loaded in Activity1.
I read the file in memory and parse it using:
String jsonString = readFileToMemory(myFilename)
JSONArray array = new JSONArray(jsonString);
where readFileToMemory (String) is as follows:
private String readFileToMemory(String filename) {
StringBuilder data = new StringBuilder();
BufferedReader reader = null;
try {
InputStream stream = myContext.getAssets().open(filename);
reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
int result = 0;
do
{
char[] chunk = new char[512];
result = reader.read(chunk);
data.append(chunk);
}
while(result != -1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data.toString();
}
- , ? ?