I have a .Net Webservice that returns a simple serialized object.
I Have the following code to get response from my .Net webservice. How can I deserialize returned XML to my object?
I want to return MyObject instead of SoapPrimitive.
private static SoapPrimitive callWebServiceMethod(String url,
String namespace, String methodName,
HashMap<String, Object> parameters, String soapAction)
throws Exception {
Log.i("WebService", "URL: " + url);
Log.i("WebService", "MethodName: " + methodName);
URL myurl = new URL(url);
URLConnection connection = myurl.openConnection();
connection.setConnectTimeout(20 * 1000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
{
httpConnection.disconnect();
SoapObject request = new SoapObject(namespace, methodName);
if (parameters != null) {
String[] keys = new String[0];
keys = (String[]) parameters.keySet().toArray(keys);
Object[] vals = (Object[]) parameters.values().toArray();
for (int i = 0; i < parameters.size(); i++) {
request.addProperty(keys[i], vals[i]);
Log.i("WebService", keys[i] + ": " + vals[i]);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(url,
TimeOutInSeconds * 1000);
androidHttpTransport.call(soapAction, envelope);
return (SoapPrimitive) envelope.getResponse();
}
else
{
httpConnection.disconnect();
throw new Exception(
"Http Error code: " + String.valueOf(responseCode));
}
}
I have not understand well but I think you wish that your method should return object of your custom class instead SOAP object.
So I have done that in my similar function like this..
SoapObject soapObject = (SoapObject) envelope.getResponse(); // SOAP Object
realTimeUtil = new RealTimeUtil(); // CUSTOM Object of My Class
if(soapObject.hasProperty("diffgram"))
soapObject = (SoapObject) soapObject.getProperty("diffgram");
if(soapObject.hasProperty(clientName)){
soapObject = (SoapObject) soapObject.getProperty(clientName);
for (int i = 0 ; i<soapObject.getPropertyCount(); i++){
if(soapObject.hasProperty(clientName)){
SoapObject soapObject1 = (SoapObject) soapObject.getProperty(i);
if(soapObject1.hasProperty("Site")){
String site = soapObject1.getProperty("Site").toString();
String dateTime = soapObject1.getProperty("DateTime").toString();
String clicks =soapObject1.getProperty("Clicks").toString();
String orders = soapObject1.getProperty("Orders").toString();
String reveneue = soapObject1.getProperty("Revenue").toString();
String items = soapObject1.getProperty("Items").toString();
realTimeUtil.setSite(site);
realTimeUtil.setClicks(clicks);
realTimeUtil.setDateTime(dateTime);
realTimeUtil.setOrders(orders);
realTimeUtil.setRevenue(reveneue);
realTimeUtil.setItem(items);
}
}
}
}
return realTimeUtil;
RealtimeUTil is one Custom class in my app with few variables and it's getter-setter methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With