Category Archives: Android and .Net webservices

Access .Net webservices which return complex types

I thought it was pretty simple for a beginner like me to parse the dataset returned from .Net webservices. I tried all the options, tried to map like
envelope.addMapping(“Namespace”, “class or tag”, classname);
but this one works fine with the complex types not with the datasets. Or maybe I might have missed something, I was not able to parse data.

For simple types we can use this

private SoapObject table = null;       // Its the table of a DataSet
private SoapObject client = null;       // Its the client pettition to the web service
private SoapObject tableRow = null;      // Gets the information of a row from the table DataSet
private SoapObject responseBody = null;     // Its the whole response of the WebService
private HttpTransport transport = null;     // Its whta is used to make the call to the WebService
private SoapSerializationEnvelope sse = null;
sse = new SoapSerializationEnvelope(SoapEnvelope.VER11); // SOAP version is indicated here. Version 11 works nice
sse.addMapping(<webService NameSpace>, <name of the Class using it>, this.getClass());
sse.dotNet = true; // We stablish we sill be working with .NET webservices
transport = new HttpTransport(<url of the webservices>);
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); transporte.debug = debug; // Set it to either use transport.requestdump or reponsedump to see the xml of the webservice request or response
// Here we set who are we gonna call, give the method arguments and do the call
client = new SoapObject(<webServiceNameSpace>, <webService Method Name we want to reuqest>);
// You can add as many properties as many as you need. I'd say that you set your webservices method to recieve only strings and do the convertions on you webservice. Its quite hard work with primitive values
client.addProperty(<agument name as String>, <argument Value>);
sse.bodyOut = client;<webService direct url>, sse);
// We'll get the whole XML reponse with all the information
responseBody = (SoapObject) sse.getResponse();
// Now, the reponse works as a Vector. On position 0 we have the information of the Reponse while in position 1 we get the info we requested. So we get rid of the information reponse and lets grab the info we requested
responseBody = (SoapObject) responseBody.getProperty(1);
// And now, we have the n tables the WebService returned to us with the information, but still we can't access the info, so we'll get one of the tables separated. We'll use a very different variable for not to lose the whole info the webservice sended us. Once again, the tables are contained on a Vector. Lets grab the first one
table = (SoapObject) responseBody.getProperty(0);
// Now we have the table, but still can't use the info, soooo, lets grab a row individually
tableRow = (SoapObject) table.getProperty(0);



It worked pretty fine when we have few records.

I parsed the responsedump xml to parse the response data into objects using SAXParser. I felt this one to be easy way.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
//Use the handler to parse the XML
ExampleHandler handler = new ExampleHandler();
reader.parse(new InputSource(new ByteArrayInputStream(transport.responsedump.getbytes())));
Object object = handler.parsedata();

The response can be captured from handler. You need to create a handler with the parsedata.

Hope this helps..