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.