的Java / Android的:java.lang.OutOfMemoryError同时建立一个JSON对象 [英] Java/Android: java.lang.OutOfMemoryError while building a JSON object

查看:130
本文介绍了的Java / Android的:java.lang.OutOfMemoryError同时建立一个JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个公共数据库URI <一个导入JSON数据href="http://data.seattle.gov/api/views/3k2p-39jp/rows.json">http://data.seattle.gov/api/views/3k2p-39jp/rows.json和行远去为445454.使用以下code,我构建了整个数据的JSON对象。

I am importing JSON data from a public database URI http://data.seattle.gov/api/views/3k2p-39jp/rows.json and the rows go as far as 445454. Using the following code I am constructing the JSON object of the entire data.

   HttpGet get = new HttpGet(uri);
   HttpClient client = new DefaultHttpClient();
   HttpResponse response = client.execute(get);
   BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    StringBuilder builder=new StringBuilder();
for(String line=null;(line = reader.readLine()) != null;){
      builder.append(line).append("\n");
    }
  JSONTokener jsonTokener=new JSONTokener(builder.toString());
  JSONObject finalJson=new JSONObject(jsonTokener);
  JSONArray data=finalJson.getJSONArray("data");

由于数据过于庞大,我得到 3月3日至21日:41:49.714:E / AndroidRuntime(666):产生的原因:java.lang.OutOfMemoryError 指向误差的来源为 buildr.append(线).append(\ N)。反正我有可以处理大型数据集没有得到内存分配问题?

Because the data is too large, i am getting 03-21 03:41:49.714: E/AndroidRuntime(666): Caused by: java.lang.OutOfMemoryError pointing the source of error at buildr.append(line).append("\n"). Is there anyway I can handle large datasets without getting memory allocation issues?

推荐答案

这JSON是巨大的!

您一定要使用流JSON解析器。有两个在那里为Android:GSON和杰克逊

You definitely need to use a streaming JSON parser. There are two out there for Android: GSON and Jackson.

GSON流传输解释流

GSON Streaming is explained at: https://sites.google.com/site/gson/streaming

我喜欢GSON说明您遇到的问题:

I like how GSON explains the problem you're having:

大多数应用程序只使用对象模型API。在短短的几年的情况JSON数据流是有用的:

Most applications should use only the object model API. JSON streaming is useful in just a few situations:

当它是不可能或不希望将整个对象模型加载到内存中。这是最相关的移动平台上,其中的内存是有限的。

When it is impossible or undesirable to load the entire object model into memory. This is most relevant on mobile platforms where memory is limited.

杰克逊流是记录在: http://wiki.fasterxml.com/JacksonInFiveMinutes#Streaming_API_Example

这篇关于的Java / Android的:java.lang.OutOfMemoryError同时建立一个JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆