Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory exception while parsing 5MB JSON response in Android

I am getting 5MB of a JSON response, I am downloading and saving in StringBuffer using a byte array with 1024 size.

To parse this response, I have to create a JSONObject with the parameter as a String. While converting response into String, I am getting an out of memory exception(stringBufferVar.toString()).

From service I will get the following response as max of 5 attachments each of maximum 5MB data in Base64 encoded.

Following is the response from 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 a list, when the user clicks on an item I have to save that attachment in SDCard. For this, how can I parse and save this huge amount of data in JSON.

Please provide any solution to this.

Thanks in advance.

like image 265
Santhosh Avatar asked Nov 05 '22 04:11

Santhosh


1 Answers

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

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

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

like image 71
njzk2 Avatar answered Nov 09 '22 13:11

njzk2