ArrayList 中的 OutOfMemory 错误.安卓 [英] OutOfMemory Error in ArrayList. Android

查看:22
本文介绍了ArrayList 中的 OutOfMemory 错误.安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 android 上的 http get 请求.

I am working on http get request on android.

我从服务器收到大量 json 数据,字符串无法处理或存储该数据并出现 OutOfMemory 异常.

I receive a huge amount of json data from server, a string is unable to handle or store that data and getting OutOfMemory Exception.

然后我尝试将其保存在可以存储最大 Integer.Maximum 值的 arrayList 中.但在 ~8970 位置存储时出现 OutOfMemory 异常.

Then I tried to save it in arrayList which can store a maximum of Integer.Maximum value. but it is getting OutOfMemory exception while storing at ~8970 location.

这是我的链接,其中包含 json 数据.

Here is my link which has json data.

http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist10122013035042.txt

这是我的代码:

ArrayList<String> newarr = new ArrayList<String>();
    try {

        URL url = new URL(urlFilePath);
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        urlConnection.setRequestMethod("GET");
        // urlConnection.setDoOutput(true);

        // connect
        urlConnection.connect();

        // Stream used for reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        // create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        int check;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            String decoded = new String(buffer, 0, bufferLength);
            newarr.add(decoded);    // OutOfMemory Exception.   
        }

        fileOutput.close();
        buffer = null;
        inputStream.close();
        String path = file.getAbsolutePath();
        return path;
    } catch (final MalformedURLException e) {
        e.printStackTrace();
        return "";
    } catch (final IOException e) {
        e.printStackTrace();
        return "";
    } catch (final Exception e) {
        System.out.println("EXCEPTION:: " + e.getMessage());
        e.printStackTrace();
        return "";
    }

推荐答案

您需要直接处理流,而不是将其存储为字符串.以 Gson Stream Reader 为例:

You need to process the stream directly instead of storing it as a String. Look at the Gson Stream Reader as an example:

public List<Message> readJsonStream(InputStream in) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    List<Message> messages = new ArrayList<Message>();
    reader.beginArray();
    while (reader.hasNext()) {
        Message message = gson.fromJson(reader, Message.class);
        messages.add(message);
    }
    reader.endArray();
    reader.close();
    return messages;
}

您可以将此处的 List 视为解析结果.如果您想显示它,您也可以使用来自您正在使用的任何 ListAdapter 的列表.

You can think of the List here as the parsed result. You could also use the list from whatever ListAdapter you are using if you want to display it.

这篇关于ArrayList 中的 OutOfMemory 错误.安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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