非常大的 SOAP 响应 - Android- 内存不足错误 [英] Very large SOAP response - Android- out of memory error

查看:13
本文介绍了非常大的 SOAP 响应 - Android- 内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,在该应用程序首次运行时,我需要通过对 Web 服务的 SOAP 调用将大量数据下载到该应用程序中.然后将响应发送到一个函数,该函数转换 XML 并将数据存储在 db 文件中.

I have an application where i need to download a large amount of data via a SOAP call to a webservice into the application when it is first run. The response is then sent to a function which converts the XML and stores the data in a db file.

数据大小超过 16MB,我每次都会出现 java.lang.OutOfMemoryError.

The data is more than 16MB in size and i have a java.lang.OutOfMemoryError everytime.

修改网络服务以提供更少量的数据不是一种选择.

Modifying the webservice to give out smaller amounts of data is not an option.

有没有办法可以下载大数据?也许像 InputStream 这样的东西?

Is there a way to be able to download the large data? Something like an InputStream perhaps?

这是我的代码

public Protocol[] getProtocols() {

    String METHOD_NAME = "GetProtocols";
    String SOAP_ACTION = "urn:protocolpedia#GetProtocols";
    Log.d("service", "getProtocols");
    SoapObject response = invokeMethod(METHOD_NAME, SOAP_ACTION);
    return retrieveProtocolsFromSoap(response);
}

private SoapObject invokeMethod(String methodName, String soapAction) {
    Log.d(TAG, "invokeMethod");
    SoapObject request = GetSoapObject(methodName);
    SoapSerializationEnvelope envelope = getEnvelope(request);
    return makeCall(envelope, methodName, soapAction);

}

谁能建议在这种情况下应该怎么做?

Can anyone suggest what should be done in this case?

感谢和问候穆库尔

推荐答案

帮助您解决此问题的两种策略:

Two strategies to help you solve this problem:

  1. 下载时将 SOAP XML 流直接保存到磁盘.不要将其存储在内存中.
  2. 使用 SAX 风格的解析器对其进行解析,您无需在内存中加载整个 DOM,而是将其分块解析.

根据您处理的 XML 类型,在代码中使用 SAX 解析器通常更难;您将不得不自己跟踪许多事情,并且您将无法从 DOM 树的一个部分跳转"到另一个部分.但是内存消耗会低很多.

Depending on the kind of XML you are handling, using SAX parsers is usually harder in code; you will have to keep track of many things yourself, and you won't be able to "jump" from section to section of your DOM tree. But the memory consumption will be way lower.

但是请注意,许多高级"网络通信库通常将整个 XML DOM 加载到内存中,这里可能就是这种情况.您可能需要自己创建和管理 HTTP 连接,然后手动解析结果.

Take note, however, that many "high-level" network communication libraries usually load the whole XML DOM in memory, which might be the case here. You will probably have to create and manage the HTTP connection yourself, and then manually parse the result.

这篇关于非常大的 SOAP 响应 - Android- 内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆