内存溢出错误的ArrayList。 Android版 [英] OutOfMemory Error in ArrayList. Android

查看:281
本文介绍了内存溢出错误的ArrayList。 Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的HTTP GET Android上的要求。

我收到了巨大的来自服务器的JSON数据量,一串无法处理或存储数据,并得到了OutOfMemory异常。

然后我试图将其保存在ArrayList的其中一个最大Integer.Maximum值可以存储。
 但它越来越OutOfMemory例外,而在8970〜存储位置。

下面是我的链接有JSON数据。

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

这是我的code:

 的ArrayList<串GT; newarr =新的ArrayList<串GT;();
    尝试{        网址URL =新的URL(urlFilePath);
        HttpURLConnection类的URLConnection =(HttpURLConnection类)网址
                .openConnection();        urlConnection.setRequestMethod(GET);
        // urlConnection.setDoOutput(真);        //连接
        urlConnection.connect();        //流用于从因特网读取数据
        为InputStream的InputStream = urlConnection.getInputStream();        //创建一个缓冲区...
        字节[]缓冲区=新的字节[1024];
        INT BufferLength中= 0;
        诠释检查;
        而(量(bufferLength = inputStream.read(缓冲液))大于0){
            字符串德codeD =新的String(缓冲,0,BufferLength中);
            newarr.add(德codeD); //内存溢出异常。
        }        fileOutput.close();
        缓冲= NULL;
        inputStream.close();
        字符串路径= file.getAbsolutePath();
        返回路径;
    }赶上(最终MalformedURLException的E){
        e.printStackTrace();
        返回;
    }赶上(最终IOException异常五){
        e.printStackTrace();
        返回;
    }赶上(最终例外五){
        的System.out.println(异常:+ e.getMessage());
        e.printStackTrace();
        返回;
    }


解决方案

您需要处理的数据流,而不是直接把它作为一个字符串。看GSON流器为例:

 公开名单<消息> readJsonStream(以InputStream的)抛出IOException
    JsonReader读者=新JsonReader(新InputStreamReader的(在UTF-8));
    清单<消息>消息=新的ArrayList<消息>();
    reader.beginArray();
    而(reader.hasNext()){
        消息消息= gson.fromJson(读卡器,Message.class);
        messages.add(消息);
    }
    reader.endArray();
    reader.close();
    返回消息;
}

您可以在这里想起名单作为分析的结果。如果要显示它您还可以使用无论从任何 ListAdapter 您正在使用的列表中。

I am working on http get request on android.

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

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.

Here is my link which has json data.

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

here is my code:

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 "";
    }

解决方案

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;
}

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。 Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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