parse soap response with a complex object

1,296 views
Skip to first unread message

Jens J

unread,
May 8, 2012, 12:27:04 PM5/8/12
to ksoap2-...@googlegroups.com
Hi,

I am trying to parse a soap call response and cast it to a complex object, but I it does not work. My request is the following:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:checkUserLogin xmlns:ns2="http://ServerFacade/">
            <username>name</username>
            <password>pass</password>
        </ns2:checkUserLogin>
    </S:Body>
</S:Envelope>

And my response looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:checkUserLoginResponse xmlns:ns2="http://ServerFacade/">
            <return>
                <message>Login erfolgreich!</message>
                <msgType>1</msgType>
            </return>
        </ns2:checkUserLoginResponse>
    </S:Body>
</S:Envelope>

I created a class named ServerMessageStruct, which is implementing the KVMSerializable interface. It has two attributes, message and msgType. Now I am trying to parse this response, but I am not sure if I understood it quite right... I am trying to do this:

ServerMessageStruct dataStruct = (ServerMessageStruct) soapEnvelope.getResponse();

This ends in a ClassCatException... and when I try to do it like this...

SoapObject so = (SoapObject) soapEnvelope.getResponse();
ServerMessageStruct dataStruct = (ServerMessageStruct) so.getProperty("checkUserLogin");

I get an exception because of an unknown property... 

What do I do wrong? What am I missing to get this to work? If you need more detailed Information, please let me know...

best regards,
Jens

Jens J

unread,
May 9, 2012, 3:03:21 AM5/9/12
to ksoap2-...@googlegroups.com
Now, i tried some other ways of parsing the response:

SoapObject so = (SoapObject) soapEnvelope.bodyIn;
ServerMessageStruct dataStruct = (ServerMessageStruct) so.getProperty("return");

and 

ServerMessageStruct dataStruct = (ServerMessageStruct)soapEnvelope.getResponse();

do not work either...

Is it even possible to cast the soap response into an object? Or do I have to do it manually by getting every property from the response?

Anatoliy Shuba

unread,
May 9, 2012, 12:07:06 PM5/9/12
to ksoap2-android
ksoap2-android project has a great examples of using library.
Did you saw it?
First of all see soccer sample - ksoap2-android\ksoap2-samples\src\main
\java\org\ksoap2\samples\soccer\"

Manfred Moser

unread,
May 9, 2012, 12:54:53 PM5/9/12
to ksoap2-...@googlegroups.com
The repsonse will always be either a SoapObject, a SoapPrimitive or a
SoapFault ...

You will need to implement parsing either manually traversing the
SoapObject tree or with the help of marshalling automating it...

Check out the links and other documentation on the wiki as well as the
samples..

manfred

Jens J

unread,
May 10, 2012, 2:51:06 AM5/10/12
to ksoap2-...@googlegroups.com
Hi!

Thanks for your answers! I have still some troubles and I do not understand why... I never saw the soccer sample, now I looked into it and tried to follow it with my own class.

This is my code for the ServerMessageStruct: http://pastebin.com/7zN0wz7t

And this is the (shortened) method of my class SoapHandler, which calls the WS: http://pastebin.com/wQZSrKs2

My request_dump, my response_dump  and my corresponding WSDL parts look like this: http://pastebin.com/JVxASEVX

I still get a classCastException in the line:

ServerMessageStruct sms = (ServerMessageStruct) so;

I do not see any big difference to the class in the soccer sample, except it is a vector and I have only a normal class with two attributes. Can you help me to understand what I am doing wrong?

best regards,
Jens

Manfred Moser

unread,
May 10, 2012, 2:07:40 PM5/10/12
to ksoap2-...@googlegroups.com
On Wed, May 9, 2012 at 11:51 PM, Jens J <jeen...@googlemail.com> wrote:
> Hi!
>
> Thanks for your answers! I have still some troubles and I do not understand
> why... I never saw the soccer sample, now I looked into it and tried to
> follow it with my own class.
>
> This is my code for the ServerMessageStruct: http://pastebin.com/7zN0wz7t
>
> And this is the (shortened) method of my class SoapHandler, which calls the
> WS: http://pastebin.com/wQZSrKs2
>
> My request_dump, my response_dump  and my corresponding WSDL parts look like
> this: http://pastebin.com/JVxASEVX
>
> I still get a classCastException in the line:
>
> ServerMessageStruct sms = (ServerMessageStruct) so;

Well.. thats because your response it a SoapObject.. just debug there
and see. You cant just cast it... if you debug you will see what the
the SoapObject tree is composed of and can then parse it..

> I do not see any big difference to the class in the soccer sample, except it
> is a vector and I have only a normal class with two attributes. Can you help
> me to understand what I am doing wrong?

I have not look at the soccer example for ages..

manfred
Reply all
Reply to author
Forward
0 new messages