OutOfMemoryError during JSON parsing in android

I use the code below to parse a JSON string extracted from the Internet (30,000 entries)

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost(params[0]); httppost.setHeader("Content-type", "application/json"); InputStream inputStream = null; String result = null; HttpResponse response = null; try { response = httpclient.execute(httppost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } HttpEntity entity = response.getEntity(); try { inputStream = entity.getContent(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } result = sb.toString(); 

I get an OutofMemory error in the code below

 while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } 

How to get rid of this error. This error occurs when the json string is very large, as it contains data about 30,000 records.

Any help in this regard is much appreciated.

+4
source share
4 answers

Android imposes a limit on the memory limit (16 MB on almost all phones, and some newer tablets have more) for each application. The application must ensure that they keep their memory limit below this level.

Thus, we cannot keep a large string, say more than 1 MB, in full, since the total real-time memory usage of the application can exceed this limit. Remember that shared memory usage includes all objects (including user interface elements) that we allocated in our application.

So, your only solution is to use a streaming JSON parser that takes data as it arrives. That is, you should not hold the entire string in a String object. One option is to use the Jackson JSON parser .

EDIT: Android now supports JSONReader from API level 11. I have never used it, but it seems possible.

+3
source

If the data file is too large, you cannot read it in memory.

Read the line, and then write it to your own file. Do not use StringBuilder to store all data in memory.

+1
source

Try importing data into chancks, for example 1000 records each time. I hope you do not run into this problem.

0
source

I solved this problem using this library . There is a very good tutorial here .

With this, you bypass the conversion of entity.getContent () to String and this solves your problem.

 InputStream inputStream = entity.getContent(); JsonReader reader = Json.createReader(inputStream); JsonObject jsonObject = reader.readObject(); return jsonObject; 
0
source

Source: https://habr.com/ru/post/1443421/


All Articles