2012-03-09 40 views
4

我得到了5MB的JSON响应,我正在使用1024字节的字节数组下载并保存在StringBuffer中。解析Android中的5MB JSON响应时出现内存不足异常

为了解析这个响应,我必须创建一个带参数的JSONObject作为String。在将响应转换为字符串时,我得到一个out of memory exception(stringBufferVar.toString()).

从服务我将得到以下响应作为最大5个附件,每个最大5MB数据Base64编码。

以下是服务响应。

{"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"} }] } 

我将在列表中显示附件,当用户单击一个项目时我必须将该附件保存在SDCard中。为此,我如何解析并保存JSON中的大量数据。

请提供任何解决方案。

在此先感谢。

回答

1

您需要使用基于流的解析器。在API 11+有http://developer.android.com/reference/android/util/JsonReader.html

下面,有GSON http://code.google.com/p/google-gson/

此外,你可能需要保存为文件,您的服务响应与JsonReader重新打开它。

+0

感谢您的回复。 我的应用程序构建目标2.3.1(API级别9)。 此外,我尝试与gson解析器仍然是我得到的相同的OutOfMemory。 InputStream is = helper.postData(url,parameters); InputStreamReader ipReader = new InputStreamReader(is); Gson gson = new Gson(); Gson gson = new Gson(); Gson gson = new Gson(); AttachmentDO attachmentResp = gson.fromJson(ipReader,AttachmentDO.class); com.google.gson.Gson.fromJson(Gson.java:694) - java.lang.OutOfMemoryError – Santhosh 2012-03-12 06:07:56

+0

您需要使用基于流的解析器。我认为gson有这种能力,但不是你使用它的方式 – njzk2 2012-03-12 09:42:00

0

使用杰克逊

Jackson Github

这应该解决您的问题。 但我不会推荐你使用没有分页的巨大数据。