android - 在 Android 中使用 SOAP 从服务器下载大文件?我正在使用 ksoap2 库。大于 30MB 的文件会引发 OutOfMemoryError
问题描述
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_GET_FILE);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION_GET_FILE, envelope);
StringBuffer theresponseString = new StringBuffer(androidHttpTransport.responseDump);
if( !(envelope.bodyIn instanceof SoapFault)) {
{
byte[] arrby = Base64.decode((String) ((SoapObject) envelope.bodyIn).getPropertyAsString("file").toString(), Base64.DEFAULT);
Log.e("Byte", arrby.toString());
FileOutputStream output;
output = new FileOutputStream(dir + "/" + requiredFiles.getPath());
output.write(arrby);
output.flush();
output.close();
status = true;
}
}
catch (SoapFault fault)
{
status = false;
fault.printStackTrace();
}
catch (Exception e) {
status = false;
e.printStackTrace();
}
上面的代码用于下载文件,当文件大小大于 30MB 时,它会在 byte[] 处引发 OutOfMemoryError。
请指导我修复解决方案。在此先感谢...!!!
解决方案
您可以下载块大小的文件并将其附加到每个块大小的同一文件中。下面是示例代码:
@Override
protected Void doInBackground1(RequiredFiles... params) {
FileOutputStream output;
requiredFiles = params[0];
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_GET_FILE);
// adding method property here serially
request.addProperty("serverKey", serverKey);
request.addProperty("hardwareKey", CommonFunctions.getUUID(context).toString());
request.addProperty("fileId", requiredFiles.getId());
request.addProperty("fileType", requiredFiles.getType());
request.addProperty("chunkOffset", requiredFiles.getDefaultChuckOFFSet());
request.addProperty("chuckSize", requiredFiles.getDefaultChuckSize());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
String URL_GET_FILE = baseURL + UrlApiResource.GET_FILE;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_GET_FILE);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION_GET_FILE, envelope);
if( !(envelope.bodyIn instanceof SoapFault)) {
byte[] arrby = Base64.decode((String) ((SoapObject) envelope.bodyIn).getPropertyAsString("file"), Base64.DEFAULT);
try {
output = new FileOutputStream(dir + "/temp_" + requiredFiles.getPath(), true);
output.write(arrby);
output.flush();
output.close();
}catch (IOException ex){
ex.printStackTrace();
}//catch
}//if
status = true;
} catch (SoapFault fault) {
status = false;
fault.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
status = false;
e.printStackTrace();
}
return null;
}
推荐阅读
- python - 处理大量请求的 aiohttp session.get() 超时问题
- angular - 如何连接离子ngStyle中的变量?
- python - 制作一个循环列表的 Python 脚本
- typescript - 在 TypeScript 中导入机器人类型和值的正确语法
- javascript - 如何旋转箭头以指向屏幕上的鼠标位置
- python - Telegram Bot /getUpdates 并解析 JSON 响应?
- java - 如何将图像添加到 ListView
- nlp - 计算序列标记任务的分类指标
- node.js - 如何使用 js 中文件的 base64 编码获取文件的 MD5(用于验证 s3 上传)
- android - 使用回收站视图解析 JSON 时应用架构的最佳实践?