我有一个应用程序,在首次运行时,我需要通过对 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);
}
谁能建议在这种情况下应该做什么?
感谢致敬
穆库尔
只是更新,我发现 AndroidHttpTransport 中的“call”方法在这一行内存不足 -
if (debug) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1)
break;
bos.write(buf, 0, rd);
}
bos.flush();
buf = bos.toByteArray(); //Goes out of memory here
responseDump = new String(buf);
is.close();
is = new ByteArrayInputStream(buf);
对 toByteArray 的调用需要大量内存,因此为了克服这个问题,我现在不将响应转换为字节数组,而是直接将其写入 XML 文件,并将其保存在我选择的位置。这里 -
if (debug) {
FileOutputStream bos = new FileOutputStream("/data/data/com.mypackage.myapp/response.xml");
byte[] buf = new byte[1048576];
int current = 0; int i=0; int newCurrent = 0;
while ((current = inputStream.read(buf)) != -1) {
newCurrent = newCurrent + current;
Log.d("current", "Current = " + current + " total = "+newCurrent+" i = "+i++);
bos.write(buf, 0, current);
}
bos.flush();
}
设备不再耗尽内存,并且我有一个自定义解析方法,可以获取此 XML 并将其写入数据库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)