小编典典
只是更新,我发现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并将其写入数据库。
2020-10-14