SOAP - Very large XML response - OutOfMemoryError

前端 未结 3 1802
再見小時候
再見小時候 2020-12-10 09:17

First, this question looks like Very large SOAP response - Android- out of memory error subject. Because of my English weakness and the similarity of this probl

相关标签:
3条回答
  • 2020-12-10 09:51

    You might be loading the entire response into memory(maybe as a string?). Android devices, especially older ones running on older hardware and older versions of the os, have a limited vm heap size. I can't remember the exact number but a quick google indicates it is as low as 16mb.

    The best way to process responses that large in android devices is to read it as a stream. I think the latest versions of apache-commons/jakarta httpclient supports streaming.

    0 讨论(0)
  • 2020-12-10 09:56

    Thank's Graeme, opening connection as a byte stream works. If it could be helpfull to somebody, this is my source code

            //Make a temporary file to save data
            File responseFile = File.createTempFile("SOAP", "xml", context.getFilesDir());      
            int nbCharRead = 0; int i=0; int totalRead = 0;
    
            //Send query
            OutputStream outputS = url_Connection.getOutputStream();
            Writer w_out = new OutputStreamWriter(outputS);
            w_out.write(webServiceXml);
            w_out.flush();
            w_out.close();
    
    
            //Buffers
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(url_Connection.getInputStream()));
            BufferedWriter bufWriter = new BufferedWriter(new FileWriter(responseFile));
            char[] buffer = new char[10000];
    
    
    
            while((nbCharRead = bufReader.read(buffer, 0, 10000)) != -1)
            {
                totalRead += nbCharRead;
                Log.d("Test InputStream", "i: " + i++ +" - " + nbCharRead + " -> " + totalRead);
                bufWriter.write(buffer, 0, nbCharRead );
            }       
    
            if(bufWriter != null)
            {
                bufWriter.flush();
                bufWriter.close();
            }
    
    
            Log.w(MsgLog, "--- Stream Got--- ; Total : " + totalRead);
    
    0 讨论(0)
  • 2020-12-10 09:59

    You can open a connection as a byte stream:

    URL url = new URL("www.google.com");
    InputStream in = new BufferedInputStream(url.openStream());
    byte[] buffer = new byte[1024];
    int chunk = 0;
    while ((chunk =in.read(buffer ))!=-1)
    {
        proccessChunk(chunk);
    }
    in.close();
    

    From this point you should be able to metre the response and process it bit-by-bit, saving each processed chunk to text files (or, if it is relational data to SQLite).

    HTH

    0 讨论(0)
提交回复
热议问题