Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cast or deserialize SoapObject to my object?

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));
        }

    }
like image 708
Bobs Avatar asked Nov 05 '22 06:11

Bobs


1 Answers

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.

like image 135
MKJParekh Avatar answered Nov 09 '22 08:11

MKJParekh