I am new to android and ksoap2. I am trying to get a map through web service. My soap response is like below.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:getAllMessagesResponse xmlns:ns2="http://messages.webservice.smshost.com/">
         <return>
            <messageEntries>
               <mobile>9676783637</mobile>
               <message>
                  <messageDate>2013-12-21T22:11:14.461+05:30</messageDate>
                  <SMSId>1</SMSId>
                  <sent>true</sent>
                  <text>Hello</text>
                  <userId>bhanu1607</userId>
                  <userMobile>96767xxxxx</userMobile>
               </message>
               <message>
                  <messageDate>2013-12-21T22:11:14.461+05:30</messageDate>
                  <SMSId>1</SMSId>
                  <sent>true</sent>
                  <text>Fine</text>
                  <userId>bhanu1607</userId>
                  <userMobile>96767xxxxx</userMobile>
               </message>
            </messageEntries>
            <messageEntries>
               <mobile>9676783636</mobile>
               <message>
                  <messageDate>2013-12-21T22:11:14.461+05:30</messageDate>
                  <SMSId>1</SMSId>
                  <sent>true</sent>
                  <text>Hi</text>
                  <userId>bhanu1607</userId>
                  <userMobile>96767xxxxx</userMobile>
               </message>
               <message>
                  <messageDate>2013-12-21T22:11:14.461+05:30</messageDate>
                  <SMSId>1</SMSId>
                  <sent>false</sent>
                  <text>H r u?</text>
                  <userId>bhanu1607</userId>
                  <userMobile>96767xxxxx</userMobile>
               </message>
            </messageEntries>
         </return>
      </ns2:getAllMessagesResponse>
   </soap:Body>
</soap:Envelope>

For this i have created a class like below.

public class GetAllMessagesResponse implements KvmSerializable {


    MessageEntries messageEntries = new MessageEntries();
    @Override
    public Object getProperty(int index) {
        return this.messageEntries;
    }

    @Override
    public int getPropertyCount() {
        return 1;
    }

    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
        info.type = new MessageEntries().getClass();
        info.name = "return";

    }

    @Override
    public void setProperty(int index, Object value) {
        this.messageEntries = (MessageEntries) value;

    }

}

and

public class MessageEntries implements KvmSerializable {

    private Vector<SoapObject> dataVector = new Vector<SoapObject>();
    private String mobileNumber;
    //private Vector<Message> messageList = new Vector<Message>();
    private MessageArray messageArray = new MessageArray();
    @Override
    public Object getProperty(int index) {
        switch (index) {
        case 0:
            return this.mobileNumber;
        case 1:
            return this.messageArray;
        default:
            break;
        }
        return null;
    }

    @Override
    public int getPropertyCount() {
        return 2;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch (index) {
        case 0:
            info.name = "mobileNumber";
            info.type = PropertyInfo.STRING_CLASS;
            break;
        case 1:
            info.name = "messageArray";
            info.type = new Vector<Message>().getClass();
            break;
        default:
            break;
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public void setProperty(int index, Object value) {
        switch (index) {
        case 0:
            if(value!=null)
                this.mobileNumber = value.toString();
            break;
        case 1:
            if(value != null){
                this.dataVector = (Vector<SoapObject>) value;
                for(int i =0; i<dataVector.size(); i++){
                    messageArray.setProperty(0, dataVector.get(i));
                }
            }
            break;
        default:
            break;
        }

    }


}

Calling Soap url here

package com.smshost.andy.webservice.sync;

import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.smshost.andy.webservice.sync.smshost.buildresponse.GetAllMessagesResponse;

public class SyncData {

    private final String NAMESPACE="http://messages.webservice.smshost.com/";
    private final String URL = "http://10.0.2.2:8080/smshost/services/smshostWebservice?WSDL";
    private final String SOAP_ACTION = "";
    private final String METHOD_NAME = "getAllMessages";

    public void sync(){
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(soapObject);
        envelope.addMapping(NAMESPACE, "GetAllMessagesResponse", new GetAllMessagesResponse().getClass());
        HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
        httpTransportSE.debug = true;
        try{
            httpTransportSE.call(SOAP_ACTION, envelope);
            GetAllMessagesResponse allMessagesResponse = new GetAllMessagesResponse();
            //Getting ClassCastException here....
            allMessagesResponse = (GetAllMessagesResponse) envelope.bodyIn;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /*public GetAllMessagesResponse GetSMSList() throws Exception {

    }*/
}

When i am trying to convert SOapObject to GetAllMessagesResponse i am getting ClassCastException. Can you please guide me how to solve this problem.

Thanks & Regards
Bhanu Teja M.

Post whole error not portion and maybe someone may help you...

This is the exception i am getting.

12-25 13:45:51.809: W/System.err(339): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
12-25 13:45:51.809: W/System.err(339):  at com.smshost.andy.webservice.sync.SyncData.sync(SyncData.java:40)
12-25 13:45:51.809: W/System.err(339):  at com.smshost.andy.SMSHostActivity.onClick(SMSHostActivity.java:66)
12-25 13:45:51.809: W/System.err(339):  at android.view.View.performClick(View.java:2408)
12-25 13:45:51.809: W/System.err(339):  at android.view.View$PerformClick.run(View.java:8816)
12-25 13:45:51.820: W/System.err(339):  at android.os.Handler.handleCallback(Handler.java:587)
12-25 13:45:51.820: W/System.err(339):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-25 13:45:51.820: W/System.err(339):  at android.os.Looper.loop(Looper.java:123)
12-25 13:45:51.820: W/System.err(339):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-25 13:45:51.820: W/System.err(339):  at java.lang.reflect.Method.invokeNative(Native Method)
12-25 13:45:51.820: W/System.err(339):  at java.lang.reflect.Method.invoke(Method.java:521)
12-25 13:45:51.829: W/System.err(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-25 13:45:51.829: W/System.err(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-25 13:45:51.829: W/System.err(339):  at dalvik.system.NativeStart.main(Native Method)
12-25 13:45:51.839: D/SMSHostActivity(339): Clicked...apple 

Thanks & Regards
Bhanu Teja M.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.