1

I am using the following code to call a method by soap. It is working perfectly.

private static final String SOAP_ACTION = "http://tempuri.org/GetAuthenticateUser";
    private static final String METHOD_NAME = "GetAuthenticateUser";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://stage.mysite.com/FinancialSnapshotService/Service.asmx?WSDL";
   // I have tried http://stage.mysite.com/FinancialSnapshotService/Service.asmx also

    public void getResults() {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("vstrUserID", "vk@gmail.com");
        request.addProperty("vstrPassword", "password");

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


        HttpTransportSE aht = new HttpTransportSE(URL);

        try {
            aht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            aht.call(SOAP_ACTION, soapEnvelope);

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


            Log.d("WS", String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

But when I tried the same code to use in some other methods in the same server, it gives ClassCastException: org.ksoap2.SoapFault. If I change the line SoapObject result = (SoapObject) soapEnvelope.getResponse(); to SoapObject result = (SoapObject)soapEnvelope.bodyIn;, it works perfectly. Can aanyone tell me what is basic of this code, where to use bodyIn and where to use getResponse()?

dev_android
  • 8,698
  • 22
  • 91
  • 148

2 Answers2

2

I used the following code to solve this prob

try{
                result = (SoapObject) soapEnvelope.getResponse();
            }catch (ClassCastException e) {
                result = (SoapObject)soapEnvelope.bodyIn; 
            }

But still it is not clear for me why it is happening.

dev_android
  • 8,698
  • 22
  • 91
  • 148
  • It's bacause it can't parse the answer. imagine html tag can screw the parsing getresponse places evrything inside o soapobejct's. when he can't parse the value there for you have a soapfault. And then you need to parse buy hand :S . Hope i helped you. – PedroAGSantos May 10 '11 at 14:37
0

If you go there you see the body in is not serialized while get respose give you soapobject already serilized. Is what I undertand on that i'm sorry if i'm wrong. http://ksoap2.sourceforge.net/doc/api/org/ksoap2/SoapEnvelope.html#bodyIn

I had same problem because my webservice was giving me html inside if is that your problem too i recomend you to encode the anser on the webservice side.

add try and catch

catch (SoapFault e) { SoapObject result = soapEnvelope.BodyIn; }

PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34