Search code examples
c#android.netweb-servicesksoap2

Exception getting binary from web service in Android app


I am trying to receive images from my .NET web services formatted like base64array. If I browse my web services from Chrome I can use them normally, but when I call them from my Android app using ksoap2 I get an exception.

That is how Chrome shows the correct base64array:

enter image description here

(Yes, the 3 images are the same, so the 3 base64array are the same too)

And this is the code that I am using to call this web service (I am using other web services normally):

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = requestFotografias;
envelope.dotNet = true;
envelope.setOutputSoapObject(requestFotografias);

transporte = new HttpTransportSE(URL);
try 
{
   transporte.call(SOAP_ACTION_DOWNLOAD_PHOTO, envelope);
   SoapObject result = (SoapObject) envelope.getResponse(); //Exception in this line

   for (int i = 0; i < result.getAttributeCount(); i++) 
   {
      SoapObject object = (SoapObject) result.getAttribute(i);
      byte[] imagen = (byte[]) object.getAttribute(0);
      this.listaImagenes.add(imagen);
   }
} 
catch (Exception e) 
{
   correcto = false;
   System.out.println(e.toString());
}

This is the code of my webservice (C#):

[WebMethod]
public List<Byte[]> DownloadImages(int idEstablecimiento)
{
   List<CAD.fotografia> listaFotografias = new List<CAD.fotografia>();
   listaFotografias = BLL.Fotografia.GetAllFotografiasFromEstablecimiento(idEstablecimiento);
   List<Byte[]> listaImagenes = new List<Byte[]>();

   foreach (CAD.fotografia fotografia in listaFotografias)
   {
      string path = fotografia.ruta_fotografia.ToString();
      FileStream objfilestream = new FileStream(path, FileMode.Open, FileAccess.Read);
      Byte[] imagen = new byte[objfilestream.Length];
      objfilestream.Read(imagen, 0, imagen.Length);
      listaImagenes.Add(imagen);
   }
   return listaImagenes;
}

The exception that I get is in spanish so I will not post it here, but is something related to System.FormatException and System.Number.StringToNumber, wich makes not too much sense since I am just using binary...

Edit: this is the full exception trace:

SoapFault - faultcode: 'soap:Client' faultstring: 'System.Web.Services.Protocols.SoapException: El servidor no puede leer la solicitud. ---> System.InvalidOperationException: Error en el documento XML (1, 336). ---> System.FormatException: La cadena de entrada no tiene el formato correcto.
en System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
en System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
en Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read12_DownloadImages()
en Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer4.Deserialize(XmlSerializationReader reader)
en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
--- Fin del seguimiento de la pila de la excepción interna ---
en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
en System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
--- Fin del seguimiento de la pila de la excepción interna ---
en System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
en System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()' faultactor: 'null' detail: org.kxml2.kdom.Node@42d06478

Any idea about what can cause this troubles or how to debug it? Remember that I can access to the other web services in the same server.


Solution

  • Make sure idEstablecimiento is an int value.