I am working on an android app in which i have to fetch data from a .Net webservice Initially methods of webservice returning simple strings.That’s fine my code is working fine and fetch strings smoothly.But my problem starts when methods return objects,I know object holds the data in XML form.I also confirmed it by debugging my code,the resultant object holds the data given below.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<UserInfoResponse xmlns="http://tempuri.org/">
<UserInfoResult>
<UserName>Himanshu</UserName>
<Email>Himanshu@XXXXXXXX.com</Email>
</UserInfoResult>
</UserInfoResponse>
</soap:Body>
</soap:Envelope>
and my code to consume the web service is:-
public void objData(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
Log.d("request", request.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Log.d("envelope", envelope.toString());
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
Log.d("envelope", envelope.toString());
HttpTransportSE aht = new HttpTransportSE(URL);
aht.debug=true;
Log.d("aht", aht.toString());
try
{
aht.call(OBJ_SOAP_ACTION, envelope);
SoapPrimitive results = (SoapPrimitive)envelope.getResponse();
System.out.println("results="+results);
tv4.setText(""+results);
}
catch (Exception e)
{
tv4.setText(e.getClass().toString());
Log.d("Error",e.getClass().toString());
}
}
The object results holds the xml data.Now my problem is that when i print the data of that object by using the code tv4.setText(""+results); it gives me class java.lang.ClassCastException.I know that this is not the right approach to get object’s xml data,I have to parse it.But i dont know how to parse object.So please help me to parse xml contained object.Any help would be highly appreciated.Thanx in advance.
You cannot use SoapPrimitive for complex object, you have to cast the response like below
Then if you want to cast it to an entity object(similar to the object you are sending via .net web service) you have to do like this.
Entity class
the way of setting the retrived soapobject to the entity class as follows.