在Android中使用SOAP从服务器下载大文件?我正在使用kso​​ap2库。大于30MB的文件将抛出OutOfMemoryError

时间:2018-08-30 12:39:00

标签: android android-ksoap2

 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。

请指导我解决问题。谢谢... !!!

1 个答案:

答案 0 :(得分:0)

您可以下载块大小的文件,并将其添加到每个块大小的相同文件中。下面是示例代码:

  @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; 
}