如何在没有outofmemory的情况下从web服务检索到Android的大数据

时间:2022-10-31 13:20:20

I need to get large amount of data (say 7000 records) from online database to android application. While googling i came to know that large amount of data will leads to outofmemory problem in json while convert from string. The solution was to convert the json with the help of gson or jackson. I have two question as follows

我需要从在线数据库获取大量数据(比如7000条记录)到android应用。当我在google上搜索时,我发现大量的数据会导致json中的outofmemory问题,而从字符串进行转换。解决方案是在gson或jackson的帮助下转换json。我有两个问题

  1. Anyother solution for converting json to avoid out of memory problem.

    任何其他用于转换json的解决方案,以避免内存不足的问题。

  2. Shall i get the data in xml format? if so whether i can solve out of memory?

    我需要xml格式的数据吗?如果是这样,我能不能解决内存问题?

Please help me..its very urgent.

请帮我. .它很紧急。

Edit:

编辑:

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

2 个解决方案

#1


0  

I think xml format will allow you for large amount of data. I am developing a Project where I download approx 15000 data in xml and there is no a memory issue.

我认为xml格式可以提供大量的数据。我正在开发一个项目,我下载了大约15000个xml数据,没有内存问题。

#2


0  

You should use Jackson, since it's a streaming processor, you can directly feed in an arbitrarily sized InputStream to the ObjectMapper.readValue() method. As long as the records themselves are not too heavy, then you should be able to keep them in memory. That said, you should probably not keep them in memory, you should feed them into a database and query them from there.

您应该使用Jackson,因为它是一个流处理程序,您可以直接将任意大小的InputStream提供给ObjectMapper.readValue()方法。只要记录本身不太沉重,那么你应该能够将它们保存在记忆中。也就是说,您可能不应该将它们保存在内存中,您应该将它们输入数据库并从那里查询它们。

#1


0  

I think xml format will allow you for large amount of data. I am developing a Project where I download approx 15000 data in xml and there is no a memory issue.

我认为xml格式可以提供大量的数据。我正在开发一个项目,我下载了大约15000个xml数据,没有内存问题。

#2


0  

You should use Jackson, since it's a streaming processor, you can directly feed in an arbitrarily sized InputStream to the ObjectMapper.readValue() method. As long as the records themselves are not too heavy, then you should be able to keep them in memory. That said, you should probably not keep them in memory, you should feed them into a database and query them from there.

您应该使用Jackson,因为它是一个流处理程序,您可以直接将任意大小的InputStream提供给ObjectMapper.readValue()方法。只要记录本身不太沉重,那么你应该能够将它们保存在记忆中。也就是说,您可能不应该将它们保存在内存中,您应该将它们输入数据库并从那里查询它们。