同时的OutOfMemoryError JSON解析在android系统 [英] OutOfMemoryError while JSON parsing in android
问题描述
我使用的是低于code解析JSON字符串从网络获取,(30000条记录)
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(params[0]);
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
inputStream = entity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
result = sb.toString();
我收到OutOfMemory错误在下面的code
I am getting the OutofMemory error in the below code
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
如何摆脱当JSON字符串是非常巨大的,因为它包含约30000条记录数据真的发生了这种error.This错误的。
How to get rid of this error.This error does occur when the json string is very huge as it contains data of about 30,000 records.
在这方面的任何帮助都非常AP preciated ..
Any help in this regard is highly appreciated..
推荐答案
的Android施加了内存上限限制(16 MB,几乎所有的手机,用一些较新的平板电脑拥有更多),为每一个应用程序。申请时应当确保他们保持低于这一水平的实时内存限制。
Android imposes a memory cap limit (of 16 MB in almost all phones, with some newer tablets have more) for each and every application. Application should make sure they maintain their live memory limit below that level.
因此,我们不能容纳大串,说超过1MB,完全有时因为应用程序了的住总内存使用量可能会超过该限制。记住,总内存包括所有对象(包括UI元素)我们在我们的应用程序分配。
So we can't hold a big string, say over 1MB, in full sometimes since the total live memory usage of applicaiton may exceed that limit. Remember, the total memory usage includes all objects (including UI elements) we allocated in our app.
所以,你唯一的解决办法是使用流媒体JSON解析器,这需要数据,因为它涉及。那是你不应该在一个String对象满弦举行。一种选择是使用杰克逊JSON解析器一>。
So your only solution is to use a Streaming JSON parser, which takes data as it comes. That is you should not hold on full string in a String object. One option is to use Jackson JSON parser.
编辑:Android现在从API级别11.切勿支持 JSONReader 用它,但它似乎要走的路。
EDIT : Android now support JSONReader from API level 11. Never used it, but it seems the way to go..
这篇关于同时的OutOfMemoryError JSON解析在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!