I am using ksoap2 to communicate to a .net webservice. However, when I submit a byte array from an image, I get an OutOfMemoryError.
I am using the following code:
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();
StackTrace:
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)
The combination of a "big" image and no optimization in kSOAP2 is probably too much for the limited memory of your smart phone.
I estimate that kSOAP2 temporarily uses at least 5 times as much memory as the size of the byte array you want to send. This is in addition to the byte array itself and in addition to other copies of the image you might keep around.
So to get it working you either need to find other areas in your app where you can reduce the memory consumption. Or you need to find a different SOAP library that doesn't keep the complete XML message as well as a Base 64 encoded string of the byte array in memory but uses a streaming approach that encodes small chunks of the array and sends them directly over the network connection.