问题
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