我需要从在线数据库中获取大量数据(比如 7000 条记录)到 Android 应用程序。在网上搜索时,我开始知道大量数据会导致在从字符串转换时json中出现内存不足的问题。解决方案是在 gson 或 jackson 的帮助下转换 json。我有两个问题如下
另一种转换 json 以避免内存不足问题的解决方案。
我应该获取 xml 格式的数据吗?如果可以,我是否可以解决内存不足的问题?
编辑
String result = convertStreamToString(is);
JSONObject jObject = new JSONObject(result); // Only i am getting outofMemory Exception..
private static String convertStreamToString(InputStream inputStream)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1024];
int n = 0;
try {
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
} finally {
out.close();
in.close();
}
return out.toString("UTF-8");
}
最佳答案
我认为 xml 格式将允许您处理大量数据。我正在开发一个项目,我在其中下载了大约 15000 个 xml 数据,并且没有内存问题。
关于android - 如何在没有内存不足的情况下将大数据从 Web 服务检索到 Android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676292/