问题
I'm trying to upload images from an android device to a WCF service. If I send a small image (about 20kb) then it works fine. If I send a slightly larger image (about 95kb) I get an error:
org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@41636470)
Android code is:
byte[] fileContents = IOUtil.readFile(image.getFileName());
request = new SoapObject(CommonFunctions.NAMESPACE, "SaveAttachment");
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
new MarshalBase64().register(envelope);
androidHttpTransport = new HttpTransportSE(CommonFunctions.SERVICE_URL);
request.addProperty("SomeProperty1", somevalue1);
request.addProperty("SomeProperty2", somevalue2);
PropertyInfo p1=new PropertyInfo();
p1.setName("FileContents");
p1.setType(MarshalBase64.BYTE_ARRAY_CLASS);
p1.setValue(fileContents);
request.addProperty(p1);
androidHttpTransport.call(CommonFunctions.SOAP_ACTION + "SaveAttachment", envelope);
int fileId = Integer.parseInt(envelope.getResponse().toString());
The exception is thrown on the androidHttpTransport.call line after a few seconds. The breakpoint in my WCF service is never hit. I've upped the request limits on the WCF bindings:
<basicHttpBinding>
<binding name="MobileIntegrationBinding" messageEncoding="Text" allowCookies="true" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<security mode="Transport" />
</binding>
</basicHttpBinding>
Is there some limit on the amount of data KSOAP will add to a property, and if so is there a way to increase this?
回答1:
Ah, got it in the end. My bindings and service declaration were all messed up in the web.config, and so my changes to maxReceivedMessageSize etc weren't having any effect because the service was using the default bindings instead of using my custom one.
回答2:
you need compress the imagen
String imageEncoded;
InputStream is = activity.getContentResolver().openInputStream(uri);
Bitmap img = BitmapFactory.decodeStream(is);
ByteArrayOutputStream convert = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 50, convert);
byte[] b = convert.toByteArray();
imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
And then convert using Base64 no byte.. on android only convert to byte aprox 50mb no more
来源:https://stackoverflow.com/questions/14650848/sending-byte-array-with-ksoap2-on-android