Very large SOAP response - Android- out of memory error

前端 未结 3 623
無奈伤痛
無奈伤痛 2020-11-28 11:53

I have an application where i need to download a large amount of data via a SOAP call to a webservice into the application when it is first run. The response is then sent to

3条回答
  •  抹茶落季
    2020-11-28 12:05

    Fixed!

    I downloaded/copied HttpTransportSE java class from here (after copied, some code errors can occur, but they are all quick fixable) and added to my package:

    https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-j2se/src/main/java/org/ksoap2/transport/HttpTransportSE.java
    

    removed from my Connection class this row:

    import org.ksoap2.transport.HttpsTransportSE;
    

    and substituted this code in my new HttpTransportSE.java file:

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

    with this

    if (debug) {
        FileOutputStream bos = new FileOutputStream(file);
    
                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();
    }
    

    where "file" is a simple file object like new File("/sdcard/","myFile.xml") for example

提交回复
热议问题