Java – Android applications request WCF services (SerializationException – utf-8 related)

Android applications request WCF services (SerializationException – utf-8 related)… here is a solution to the problem.

Android applications request WCF services (SerializationException – utf-8 related)

Some requests from our Android application to the WCF service result in the following error:

System.Runtime.Serialization.SerializationException: There was an
error deserializing the object of type xxx.SaveChangesCommand.
‘�rg�rden’ contains invalid UTF8 bytes. —> System.Xml.XmlException:
‘�rg�rden’ contains invalid UTF8 bytes. —>
System.Text.DecoderFallbackException: Unable to translate bytes [E6]
at index 0 from specified code page to Unicode.

at
System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown,
Int32 index) at
System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[]
bytesUnknown, Int32 index) at
System.Text.DecoderFallbackBuffer.InternalFallback(Byte[] bytes, Byte*
pBytes, Char*& chars) at
System.Text.UTF8Encoding.FallbackInvalidByteSequence(Byte*& pSrc,
Int32 ch, DecoderFallbackBuffer fallback, Char*& pTarget) at
System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char*
chars, Int32 charCount, DecoderNLS baseDecoder) at
System.Text.UTF8Encoding.GetChars(Byte[] bytes, Int32 byteIndex, Int32
byteCount, Char[] chars, Int32 charIndex) at
System.Xml.XmlConverter.ToChars(Byte[] buffer, Int32 offset, Int32
count, Char[] chars, Int32 charOffset)

--- End of inner exception

stack trace — at System.Xml.XmlConverter.ToChars(Byte[] buffer,
Int32 offset, Int32 count, Char[] chars, Int32 charOffset) at
System.Xml.XmlBufferReader.GetChars(Int32 offset, Int32 length, Char[]
chars) at System.Xml.XmlBufferReader.GetString(Int32 offset, Int32
length) at System.Xml.ValueHandle.GetString() at
System.Xml.XmlBaseReader.ReadContentAsString() at
System.Xml.XmlBaseReader.ReadElementContentAsString() at
ReadArrayOfstringFromJson(XmlReaderDelegator ,
XmlObjectSerializerReadContextComplexJson , XmlDictionaryString ,
XmlDictionaryString , CollectionDataContract ) at
System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator
jsonReader, XmlObjectSerializerReadContextComplexJson context) at
System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator
jsonReader, XmlObjectSerializerReadContextComplexJson context) at
System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
reader, String name, String ns, Type declaredType, DataContract&
dataContract) at
System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String
name, String ns) at
ReadSaveShoppingListChangesCommandFromJson(XmlReaderDelegator ,
XmlObjectSerializerReadContextComplexJson , XmlDictionaryString ,
XmlDictionaryString[] ) at
System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator
jsonReader, XmlObjectSerializerReadContextComplexJson context) at
System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator
jsonReader, XmlObjectSerializerReadContextComplexJson context) at
System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
reader, String name, String ns, Type declaredType, DataContract&
dataContract) at
System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
xmlReader, Type declaredType, DataContract dataContract, String name,
String ns) at
System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator
xmlReader, Boolean verifyObjectName) at
System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator
reader, Boolean verifyObjectName, DataContractResolver
dataContractResolver)

— End of inner exception stack trace —
at
System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator
reader, Boolean verifyObjectName, DataContractResolver
dataContractResolver) at
System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader
reader, Boolean verifyObjectName) at
System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message
message, Object[] parameters) at
System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message
message, Object[] parameters) at
System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message
message, Object[] parameters) at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&
rpc) at
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc) at
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&
rpc) at
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&
rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean
isOperationContextSet)

I

was looking for some similar cases and I found this link:
Configuring the .NET WCF UTF-8 deserializer to modify/discard non-shortest form chars instead of throwing an exception?
Probably fixed the bug of using non-shortest form of UTF-8 – if that’s what I think is the case.

But I don’t see why this fix is necessary, because as follows:
https://blogs.oracle.com/CoreJavaTechTips/entry/the_overhaul_of_java_utf

The new UTF-8 charset implementation has been integrated in JDK7,
Open JDK 6, JDK 6 update 11 and later, JDK5.0u17, and 1.4.2_19.

When I look at the request, I see:

HTTP_USER_AGENT:Apache-HttpClient/UNAVAILABLE (java 1.4)

But Android apps are compiled with jdk1.7.0_13

How do I fix this?

Solution

I don’t think the XML is entirely in UTF-8 format.
See Text of type “rg rden” may be in a format other than UTF-8.
For example, ISO 8859-1 and the like.

So try encoding text in UTF-8, it might work 🙂

Related Problems and Solutions