我正在从 Assets 文件夹中加载 xml 文件。我收到 OutOfMemoryError。 我使用的代码是
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + NEW_LINE);
}
reader.close();
} catch (IOException e) {
//do nothing.
} finally {
try {
reader.close();
} catch (IOException e) {
//do nothing.
}
}
reader=null;
return sb.toString();
}
有没有其他方法可以摆脱这个异常。 如果您发布任何代码,将会更有帮助。 提前致谢。
最佳答案
使用字符串来解析大的 Xml 并不是一个好主意。您应该转向解析器的流式版本。 Google Http Java Client 提出了这样一个库:http://code.google.com/p/google-http-java-client
关于java - 在 Android 中将 Stream 转换为 String 时出现 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13697399/