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="">
         <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="";
    private final String URL = "";
    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.

Dani AI

Generated

A short diagnosis tied to the stack trace posted by : ksoap2 returns a dynamic SoapObject for the response (envelope.bodyIn / envelope.getResponse()) — you cannot safely cast that SoapObject to your KvmSerializable subclass unless ksoap2 has an exact mapping for the response element and every nested complex type. The trace shows exactly that cast failure. (daniweb.com)

Two practical fixes (pick one):

  • Register correct mappings and match XML names. Call envelope.addMapping(namespace, elementName, YourClass.class) using the exact XML element name (case-sensitive) for the wrapper and for each nested complex type, and make each KvmSerializable implementation return the exact element names in getPropertyInfo (for example the SOAP tag is mobile but your code used mobileNumber). ksoap2 will then try to deserialize into your classes. (simpligility.github.io)

  • Parse the SoapObject manually. This is often simpler: call Object resp = envelope.getResponse(); check instanceof SoapObject, then drill into the "return" element and iterate its properties, extracting primitives or nested SoapObjects and mapping them into your POJOs. Example pattern:

Object resp = envelope.getResponse();
if (resp instanceof SoapObject) {
  SoapObject so = (SoapObject) resp;
  SoapObject ret = (SoapObject) so.getProperty("return");
  for (int i = 0; i < ret.getPropertyCount(); i++) {
    SoapObject entry = (SoapObject) ret.getProperty(i);
    String mobile = entry.getPropertyAsString("mobile");
    ...
  }
}

See ksoap2 how-to notes for mapping vs manual parsing. (seesharpgears.blogspot.com)

Quick troubleshooting checklist: enable HttpTransportSE.debug and inspect requestDump/responseDump, print envelope.getResponse().getClass() to confirm types, fix getPropertyCount/getPropertyInfo to match XML order and names, and register Marshal classes for dates if needed. If mapping keeps failing, manual parsing is a robust fallback. (simpligility.github.io)

Recommended Answers

All 2 Replies

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.