Muk*_*ain 10 android soap android-ksoap2
我有一个应用程序,我需要在第一次运行时通过SOAP调用将大量数据下载到Web服务中.然后将响应发送到一个函数,该函数转换XML并将数据存储在db文件中.
数据大小超过16MB,每次都有一个java.lang.OutOfMemoryError.
修改Web服务以提供较少量的数据不是一种选择.
有没有办法能够下载大数据?或许像InputStream?
这是我的代码
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);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议在这种情况下应该做些什么?
谢谢并问候Mukul
只是一个更新,我发现AndroidHttpTransport中的"调用"方法在此行没有内存 -
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);
Run Code Online (Sandbox Code Playgroud)
对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();
}
Run Code Online (Sandbox Code Playgroud)
设备不再耗尽内存,我有一个自定义解析方法,它接受此XML并将其写入数据库.
两种策略可以帮助您解决此问题:
根据您处理的XML类型,使用SAX解析器通常在代码中更难; 你必须自己跟踪很多事情,否则你将无法从你的DOM树的一个部分"跳转".但内存消耗将会降低.
但请注意,许多"高级"网络通信库通常会将整个XML DOM加载到内存中,这可能就是这种情况.您可能必须自己创建和管理HTTP连接,然后手动解析结果.
归档时间: |
|
查看次数: |
9327 次 |
最近记录: |