非常大的 SOAP 响应 - Android - 内存不足错误
我有一个应用程序,在首次运行时,我需要通过对 Web 服务的 SOAP 调用将大量数据下载到应用程序中。然后,响应被发送到一个函数,该函数转换 XML 并将数据存储在 db 文件中。
数据大小超过 16MB,每次都会出现 java.lang.OutOfMemoryError 错误。
修改网络服务以提供更少量的数据并不是一种选择。
有没有办法可以下载大数据?也许像输入流之类的东西?
这是我的代码
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);
}
有人能建议在这种情况下应该做什么吗?
感谢和问候 穆库尔
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.
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.
Is there a way to be able to download the large data? Something like an InputStream perhaps?
This is my code
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?
Thanks and regards
Mukul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是一个更新,我发现 AndroidHttpTransport 中的“call”方法在这一行内存不足 -
对 toByteArray 的调用需要大量内存,因此为了克服这个问题,我现在不将响应转换为字节数组直接将其写入 XML 文件,并将其保存在我选择的位置。这里 -
设备不再耗尽内存,并且我有一个自定义解析方法,该方法获取此 XML 并将其写入数据库。
Just an update, I found that the "call" method in AndroidHttpTransport was running out of memory at this line -
the call to toByteArray takes a lot of memory, so to overcome this, instead of converting the response to a byte array, i now directly write it to an XML file, and this is saved at a location of my choice. Here -
The device no longer runs out of memory, and i have a custom parse method that takes this XML and writes it to the DB.
有两种策略可以帮助您解决此问题:
根据您正在处理的 XML 类型,在代码中使用 SAX 解析器通常会比较困难;您必须自己跟踪许多事情,并且无法从 DOM 树的一个部分“跳转”到另一个部分。但内存消耗会低得多。
但请注意,许多“高级”网络通信库通常将整个 XML DOM 加载到内存中,这里可能就是这种情况。您可能需要自己创建和管理 HTTP 连接,然后手动解析结果。
Two strategies to help you solve this problem:
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.
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.
固定的!
我从这里下载/复制了 HttpTransportSE java 类(复制后,可能会出现一些代码错误,但它们都可以快速修复)并添加到我的包中:
从我的 Connection 类中删除这一行:
并在我的新 HttpTransportSE.java 中替换此代码file:
其中
“file”是一个简单的文件对象,例如 new File("/sdcard/","myFile.xml")
Fixed!
I downloaded/copied HttpTransportSE java class from here (after copied, some code errors can occur, but they are all quick fixable) and added to my package:
removed from my Connection class this row:
and substituted this code in my new HttpTransportSE.java file:
with this
where "file" is a simple file object like new File("/sdcard/","myFile.xml") for example