Search code examples
androidweb-servicesksoap2

Android: soap primitive error


I am having some issues with accessing a web service. Getting ClassCastException Error. Consider a scenario that I am trying to access a method of a web-service and the webservice is supposed to return two strings (Lets say String1 and String2). Moreover, I have to provide or pass two parameters (lets say Parameter 1 and Parameter 2 where Parameter 1 should be integer and Parameter 2 should be String) Here is my code

public class MyWebService extends Activity {

private static final String SOAP_ACTION ="http://www.mywebsite.com/myMethod";
private static final String METHOD_NAME = "MyMethod";
private static final String NAMESPACE = "http://www.myNamespace/";
    private static final String URL = "http://mysession.com/myservice.asmx?WSDL";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo pi = new PropertyInfo();   
        pi.setName("Parameter 1");
        pi.setValue(1);
        pi.setType(pi.INTEGER_CLASS);
        request.addProperty(pi);

        PropertyInfo pi2 = new PropertyInfo();  
        pi2.setName("Parameter 2");
        pi2.setValue("Any string");
        pi2.setType(pi2.STRING_CLASS);
        request.addProperty(pi2);

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);                   
        SoapObject result=(SoapObject)envelope.getResponse();               


        String string1=result.getProperty(0).toString();
        String string2=result.getProperty(1).toString();


    } catch (Exception e) {
        e.printStackTrace();    

    }        
}

}

This is the exception I am getting java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive

Can anyone tell me if I am doing something wrong here.. Thanks


Solution

  • Try this,

    SoapPrimitive result= (SoapPrimitive)envelope.getResponse();
    

    OR

    Object result= (Object)envelope.getResponse();
    

    instead of

    SoapObject result=(SoapObject)envelope.getResponse();