gpt4 book ai didi

java - Android StringBuilder 大 XML 文件内存不足

转载 作者:行者123 更新时间:2023-11-30 04:12:28 24 4
gpt4 key购买 nike

我在 android 设备上使用 soap 服务,作为响应,我从服务器获得了 7mb 文件。这会导致我的应用程序在将输入流转换为字符串时因内存不足错误而崩溃。 MemoryAnalyzer 显示内存已分配给 StreamBuilder。处理如此大的响应的最佳方法是什么?

HttpEntity entity = new StringEntity(soap.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
if( r_entity != null ) {
result = inputStreamToString(r_entity.getContent());
}
...
//convert stream to string
public static String inputStreamToString(InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}

最佳答案

最明显的答案是使用流并在结果出现时对其进行解析。如果您要解析的是 XML 文件,那么 SAX 可能是您的最佳途径。

关于java - Android StringBuilder 大 XML 文件内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10621261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com