0

我正在使用 ksoap2 与 .net 网络服务进行通信。但是,当我从图像提交字节数组时,我会收到 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)
4

1 回答 1

0

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

我估计 kSOAP2 临时使用的内存至少是您要发送的字节数组大小的 5 倍。这是对字节数组本身的补充,也是您可能保留的图像的其他副本的补充。

因此,要使其正常工作,您要么需要在应用程序中找到可以减少内存消耗的其他区域。或者您需要找到一个不同的 SOAP 库,它不会将完整的 XML 消息以及字节数组的 Base 64 编码字符串保存在内存中,而是使用流式处理方法来编码数组的小块并将它们直接发送到网络连接。

于 2012-06-04T12:51:32.153 回答