Memory exception while parsing 5MB JSON response on Android

I get 5 MB of JSON response, I load and save to StringBuffer using a byte array with a size of 1024.

To parse this answer, I need to create a JSONObject with a parameter as a string. Converting the response to String, I get an out of memory exception(stringBufferVar.toString()).

From the service, I get the following response as a maximum of 5 attachments, each of which contains a maximum of 5 MB of Base64 encoded data.

Below is the response from the service.

 {"result":[{"attachment":{"name":"one.doc", "type":"document", "data":"base64 encoded data max of 5MB"}, {"attachment":{"name":"two.txt", "type":"text", "data":"base64 encoded data max of 5MB"} }] } 

I will show the attachments in the list when the user clicks on the item that I have to save this application to SDCard. For this, how can I parse and save this huge amount of data in JSON.

Please provide any solution for this.

Thanks in advance.

+4
source share
2 answers

You need to use a thread based parser. in API 11+ there is http://developer.android.com/reference/android/util/JsonReader.html

Below is gson http://code.google.com/p/google-gson/

Also, you probably need to save the service response as a file before opening it again with JsonReader.

+1
source

Use jackson

Jackson github

This should solve your problem. But I will not recommend you to use huge data without pagination.

0
source

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


All Articles