使用KSOAP2发送字节数组时的java.lang.OutOfMemoryError

时间:2012-06-04 11:01:07

标签: android web-services ksoap2

我正在使用kso​​ap2与.net webservice进行通信。但是,当我从图像提交一个字节数组时,我得到一个OutOfMemoryError。

我使用以下代码:

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            new MarshalBase64().register(envelope); // serialization
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.call(ADD_CLIENT_SOAP_ACTION, envelope);               
            Object response = null;
            response = envelope.getResponse();

堆栈跟踪:

06-04 12:46:41.605: E/AndroidRuntime(9661): FATAL EXCEPTION: IntentService[SyncService]
06-04 12:46:41.605: E/AndroidRuntime(9661): java.lang.OutOfMemoryError
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:216)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:164)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.OutputStreamWriter.convert(OutputStreamWriter.java:236)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.OutputStreamWriter.write(OutputStreamWriter.java:225)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at java.io.BufferedWriter.write(BufferedWriter.java:249)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.kxml2.io.KXmlSerializer.writeEscaped(KXmlSerializer.java:137)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.kxml2.io.KXmlSerializer.text(KXmlSerializer.java:544)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.MarshalBase64.writeInstance(MarshalBase64.java:40)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:656)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:649)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:604)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:582)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:658)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:564)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:205)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:111)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:120)
06-04 12:46:41.605: E/AndroidRuntime(9661):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)

1 个答案:

答案 0 :(得分:0)

对于智能手机的有限内存而言,kSOAP2中“大”图像和无优化的组合可能太多了。

我估计kSOAP2暂时使用的内存量至少是要发送的字节数组大小的5倍。除了字节数组本身之外,还有您可能保留的其他图像副本。

因此,要使其正常工作,您需要在应用中找到可以减少内存消耗的其他区域。或者您需要找到一个不同的SOAP库,它不保留完整的XML消息以及内存中字节数组的Base 64编码字符串,但使用流式方法对数组的小块进行编码并直接通过网络连接。