Android ArrayList中的OutOfMemory错误。安卓

Android ArrayList中的OutOfMemory错误。安卓,android,json,arraylist,out-of-memory,http-get,Android,Json,Arraylist,Out Of Memory,Http Get,我正在android上处理http get请求 我从服务器接收到大量的json数据,字符串无法处理或存储该数据,并导致内存异常 然后我尝试将它保存在arrayList中,它可以存储最大值Integer.maximum value。 但当它存储在约8970个位置时,它会出现内存异常 这是我的链接,其中包含json数据 这是我的密码: ArrayList<String> newarr = new ArrayList<String>(); try {

我正在android上处理http get请求

我从服务器接收到大量的json数据,字符串无法处理或存储该数据,并导致内存异常

然后我尝试将它保存在arrayList中,它可以存储最大值Integer.maximum value。 但当它存储在约8970个位置时,它会出现内存异常

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

这是我的密码:

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 "";
    }
ArrayList newarr=new ArrayList();
试一试{
URL=新的URL(URL文件路径);
HttpURLConnection urlConnection=(HttpURLConnection)url
.openConnection();
urlConnection.setRequestMethod(“GET”);
//urlConnection.setDoOutput(true);
//连接
urlConnection.connect();
//用于从internet读取数据的流
InputStream InputStream=urlConnection.getInputStream();
//创建一个缓冲区。。。
字节[]缓冲区=新字节[1024];
int bufferLength=0;
整数检查;
而((bufferLength=inputStream.read(buffer))>0){
已解码字符串=新字符串(缓冲区,0,缓冲区长度);
newarr.add(已解码);//OutOfMemory异常。
}
fileOutput.close();
缓冲区=空;
inputStream.close();
字符串路径=file.getAbsolutePath();
返回路径;
}捕获(最终格式错误){
e、 printStackTrace();
返回“”;
}捕获(最终IOE例外){
e、 printStackTrace();
返回“”;
}捕获(最终异常e){
System.out.println(“异常::”+e.getMessage());
e、 printStackTrace();
返回“”;
}

ArrayList(或任何类型的列表)的最大容量仅受JVM可用内存量的限制

你可以使用BufferedReader,或者用扫描器代替字符串

否则,如果你仍然想使用字符串,那么试着增加JVM。据我所知,在运行时无法控制堆的大小

但这可能不是必需的:您可以分别使用-Xms和-Xmx开关提供最小和最大堆大小。(例如-Xms128m-Xmx512m)jvm将在这些范围内管理实际堆大小

试着根据你的使用情况设置限制

欲了解更多参考资料,请尝试从


您需要直接处理流,而不是将其存储为字符串。以Gson流读取器为例:

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;
}
public List readJsonStream(InputStream in)引发IOException{
JsonReader=新的JsonReader(新的InputStreamReader(在“UTF-8”中));
列表消息=新建ArrayList();
reader.beginArray();
while(reader.hasNext()){
Message Message=gson.fromJson(reader,Message.class);
消息。添加(消息);
}
reader.endArray();
reader.close();
返回消息;
}

您可以将这里的列表视为解析结果。如果您想显示列表,也可以使用您正在使用的任何
ListAdapter
中的列表。

尝试使用
StringBuilder
而不是
String
,并检查这是否有帮助。感谢您的回复。我刚才试过了,但没用。获取相同的错误。尝试注释掉
newarr.add(已解码)
并查看它是否仍然崩溃。您能跟踪哪个项目id之后的内存溢出错误吗?为什么您需要字符串中的数据,因为这是json数据。您需要提取json数据。您的场景中有什么特殊情况吗?如果您只需要数据,则可以参考: