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

。_饼干妹妹 提交于 2019-12-10 17:09:52

问题


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.


回答1:


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.




回答2:


Use Jackson

Jackson Github

That should solve your issue. But i will not recomment you to use huge data without pagination.



来源:https://stackoverflow.com/questions/9632667/out-of-memory-exception-while-parsing-5mb-json-response-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!