how can consume a web service in php from android with ksoap - android

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="checkpoint">
<SOAP-ENV:Body>
<ns1:getPedidoResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
<tabla xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:pedidos[1]">
<item xsi:type="tns:pedidos">
<ID_PEDIDO xsi:type="xsd:int">1</ID_PEDIDO>
<RUT_CLIENTE xsi:type="xsd:int">1234</RUT_CLIENTE>
<ID_PAQUETE xsi:type="xsd:int">1</ID_PAQUETE>
<RUT_EMPLEADO xsi:type="xsd:int">5678</RUT_EMPLEADO>
<DIRECCION xsi:type="xsd:string">la moenda</DIRECCION>
<LATITUD xsi:type="xsd:string">-33,44291</LATITUD>
<lONGITUD xsi:type="xsd:string">-70,65386</lONGITUD>
<ESTADO xsi:type="xsd:int">1</ESTADO>
</item>
</tabla>
</ns1:getPedidoResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is my class:
public class Pedidos implements KvmSerializable {
public int ID_PEDIDO;
public int RUT_CLIENTE;
public int ID_PAQUETE;
public int RUT_EMPLEADO;
public String DIRECCION;
public String LATITUD;
public String lONGITUD;
public int ESTADO;
public Pedidos(){}
#Override
public Object getProperty(int arg0) {
switch (arg0){
case 0:
return ID_PEDIDO;
case 1:
return RUT_CLIENTE;
case 2:
return ID_PAQUETE;
case 3:
return RUT_EMPLEADO;
case 4:
return DIRECCION;
case 5:
return LATITUD;
case 6:
return lONGITUD;
case 7:
return ESTADO;
default:
return null;
}
}
#Override
public int getPropertyCount() {
return 7;
}
#Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch (arg0){
case 0:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "ID_PEDIDO";
break;
case 1:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "RUT_CLIENTE";
break;
case 2:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "ID_PAQUETE";
break;
case 3:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "RUT_EMPLEADO";
break;
case 4:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "DIRECCION";
break;
case 5:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "LATITUD";
break;
case 6:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "lONGITUD";
break;
case 7:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name = "ESTADO";
break;
default:
break;
}
}
#Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0:
ID_PEDIDO = Integer.parseInt(arg1.toString());
break;
case 1:
RUT_CLIENTE = Integer.parseInt(arg1.toString());
case 2:
ID_PAQUETE = Integer.parseInt(arg1.toString());
case 3:
RUT_EMPLEADO = Integer.parseInt(arg1.toString());
case 4:
DIRECCION = arg1.toString();
case 5:
LATITUD = arg1.toString();
case 6:
lONGITUD = arg1.toString();
case 7:
ESTADO = Integer.parseInt(arg1.toString());
default:
}
}
}
And here i make the call:
public class MainActivity extends Activity {
static final String SOAP_ACTION="http://192.168.117.1/ws_checkpoint.php/getPedido";
static final String METHOD_NAME="getPedido";
static final String NAMESPACE="checkpoint";
static final String URL="http://192.168.117.1/ws_checkpoint.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("pRutEmpleado",5678);
SoapObject result;
HttpTransportSE httpt = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(NAMESPACE, "checkpoint", Pedidos.class);
envelope.dotNet = false;
envelope.bodyOut = request;
try
{
httpt.debug = true;
httpt.call(SOAP_ACTION, envelope);
result = (SoapObject)envelope.bodyIn;
Vector<Pedidos> result_array = (Vector<Pedidos>)envelope.getResponse();
if(result_array != null){
for (Pedidos current_crop: result_array){
System.out.println(current_crop.RUT_EMPLEADO);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The error is :
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
when i trying to print current_crop.RUT_EMPLEADO.

I think this is due to AndroidHttpTransport, please check this out How to call a .NET Webservice from Android using KSOAP2?
and http://seesharpgears.blogspot.com/2010/11/basic-ksoap-android-tutorial.html
Thank you.

Related

How to send/receive a custom object from Android to .NET using kSOAP?

So, I am a newbie in mobile development and interoperability. I am developing a Android application that comunnicates send a Contact object to a remove server that is running a .NET Web Service (via kSOAP). But there is a 500 error in the server and I really don't know how to map it because it is running remotely and my host plan doesn't allow me to debug remotely. I believe it is an error in the interpretation of the Java object in the ASP.NET Web Service, I don't know if it is necessary to deserialize the object and how to do it. I hope that a more experienced developer could help me. I already used primitive objects (string, int and others) and it worked like a charm but I really prefer to work with my models.
The Android Application - Call of the Web Service:
public int webServiceMethodRegisterUser(String... paramss)
{
String resultData;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
Contact c = new Contact(paramss[0], paramss[1]);
c._email = paramss[0];
c._password = paramss[1];
c._cellphonesim = paramss[2];
c._simoperator = paramss[3];
PropertyInfo pi = new PropertyInfo();
pi.setName("contact");
pi.setValue(c);
pi.setType(c.getClass());
request.addProperty(pi);
//request.addProperty("useremail", paramss[0]);
//request.addProperty("userpass", paramss[1]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "Contact" , new Contact().getClass());
//Marshal floatMarshal = new Marshal();
//floatMarshal.register(envelope);
//Marshal oi = new ();
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result=(Object)envelope.getResponse();
//To get the data.
resultData= result.toString();
publishProgress(resultData);
return Integer.parseInt(resultData);
} catch (Exception e) {
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
throw new RuntimeException(e1.toString());
}
throw new RuntimeException(e.toString());
}
}
Custom Object in Java
package com.example.ivi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.MarshalDate;
import org.ksoap2.serialization.PropertyInfo;
public class Contact implements KvmSerializable {
// private variables
int _id;
String _email;
String _password;
String _simoperator;
String _cellphonesim;
String _sn_facebook;
String _sn_linkedin;
String _sn_googleplus;
String _sn_orkut;
String _sn_twitter;
Date _last_refresh;
Date _register_date;
public Contact(int id, String email, String password, String simoperator,
String cellphonesim, String facebook, String linkedin,
String googleplus, String orkut, String twitter, Date refreshuser,
Date userregister) {
_id = id;
_email = email;
_password = password;
_simoperator = simoperator;
_cellphonesim = cellphonesim;
_sn_facebook = facebook;
_sn_linkedin = linkedin;
_sn_googleplus = googleplus;
_sn_orkut = orkut;
_sn_twitter = twitter;
_last_refresh = refreshuser;
_register_date = userregister;
}
public Contact() {
}
// constructor
public Contact(int id, String email, String password) {
this._id = id;
this._email = email;
this._password = password;
}
// constructor
public Contact(String email, String password) {
this._email = email;
this._password = password;
}
#Override
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return _id;
case 1:
return _email;
case 2:
return _password;
case 3:
return _simoperator;
case 4:
return _cellphonesim;
case 5:
return _sn_facebook;
case 6:
return _sn_linkedin;
case 7:
return _sn_googleplus;
case 8:
return _sn_orkut;
case 9:
return _sn_twitter;
case 10:
return _last_refresh;
case 11:
return _register_date;
}
return null;
}
#Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 12;
}
#Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "UserId";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserEmail";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserPassword";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserSimOperator";
break;
case 4:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserCellPhoneSim";
break;
case 5:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserFacebook";
break;
case 6:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserLinkedin";
break;
case 7:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserGoogleplus";
break;
case 8:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserOrkut";
break;
case 9:
info.type = PropertyInfo.STRING_CLASS;
info.name = "UserTwitter";
break;
case 10:
info.type = MarshalDate.DATE_CLASS;
;
info.name = "UserLastRefresh";
break;
case 11:
info.type = MarshalDate.DATE_CLASS;
;
info.name = "UserRegisterDate";
break;
}
}
#Override
public void setProperty(int index, Object value) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
switch (index) {
case 0:
_id = Integer.parseInt(value.toString());
break;
case 1:
_email = value.toString();
break;
case 2:
_password = value.toString();
break;
case 3:
_simoperator = value.toString();
break;
case 4:
_cellphonesim = value.toString();
break;
case 5:
_sn_facebook = value.toString();
break;
case 6:
_sn_linkedin = value.toString();
break;
case 7:
_sn_googleplus = value.toString();
break;
case 8:
_sn_orkut = value.toString();
break;
case 9:
_sn_twitter = value.toString();
break;
case 10:
try {
_last_refresh = formatter.parse(value.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 11:
try {
_register_date = formatter.parse(value.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
. NET Web Service
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace WebServiceIvi
{
[WebService(Namespace = "http://www.ividomain.somee.com/Service1")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int registerUser(Contact contato)
{
string connString = CONNECTION_STRING;
SqlConnection con = new SqlConnection(connString);
SqlCommand cmdSQL = con.CreateCommand();
try
{
con.Open();
cmdSQL.CommandText = String.Format("INSERT INTO [databaseivi].[dbo].[UsersIvi] ([Useremail],[Userpass],[Userxml],[Usersince]) VALUES('{0}','{1}',null ,getDate())", contato.UserEmail, contato.UserPassword);
int result = cmdSQL.ExecuteNonQuery();
con.Close();
return result;
}
catch (SqlException e)
{
foreach (SqlError error in e.Errors)
{
return error.Number;
}
return 0;
}
}
}
}
The Custom Object in C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebServiceIvi
{
public class Contact
{
public Contact(int id, String email, String password, String simoperator, String cellphonesim, String facebook, String linkedin, String googleplus, String orkut, String twitter, DateTime refreshuser, DateTime userregister )
{
_id = id;
_email = email;
_password = password;
_simoperator = simoperator;
_cellphonesim = cellphonesim;
_sn_facebook = facebook;
_sn_linkedin = linkedin;
_sn_googleplus = googleplus;
_sn_orkut = orkut;
_sn_twitter = twitter;
_last_refresh = refreshuser;
_register_date = userregister;
}
public Contact()
{
}
int _id;
String _email;
String _password;
String _simoperator;
String _cellphonesim;
String _sn_facebook;
String _sn_linkedin;
String _sn_googleplus;
String _sn_orkut;
String _sn_twitter;
DateTime _last_refresh;
DateTime _register_date;
public int UserId
{
set { _id = value; }
get
{
if (_id <= 0) return _id;
else return 0;
}
}
public string UserEmail
{
set { _email = value; }
get
{
if (_email != null) return _email.ToString();
else return string.Empty;
}
}
public string UserPassword
{
set { _password = value; }
get
{
if (_password != null) return _password.ToString();
else return string.Empty;
}
}
public string UserSimOperator
{
set { _simoperator = value; }
get
{
if (_simoperator != null) return _simoperator.ToString();
else return string.Empty;
}
}
public string UserCellPhoneSim
{
set { _cellphonesim = value; }
get
{
if (_cellphonesim != null) return _cellphonesim.ToString();
else return string.Empty;
}
}
public string UserFacebook
{
set { _sn_facebook = value; }
get
{
if (_sn_facebook != null) return _sn_facebook.ToString();
else return string.Empty;
}
}
public string UserLinkedin
{
set { _sn_linkedin = value; }
get
{
if (_sn_linkedin != null) return _sn_linkedin.ToString();
else return string.Empty;
}
}
public string UserGoogleplus
{
set { _sn_googleplus = value; }
get
{
if (_sn_googleplus != null) return _sn_googleplus.ToString();
else return string.Empty;
}
}
public string UserOrkut
{
set { _sn_orkut = value; }
get
{
if (_sn_orkut != null) return _sn_orkut.ToString();
else return string.Empty;
}
}
public string UserTwitter
{
set { _sn_twitter = value; }
get
{
if (_sn_twitter != null) return _sn_twitter.ToString();
else return string.Empty;
}
}
public DateTime UserLastRefresh
{
set { _last_refresh = value; }
get
{
if (_last_refresh != null) return DateTime.Parse(_last_refresh.ToString());
else return DateTime.MinValue;
}
}
public DateTime UserRegisterDate
{
set { _register_date = value; }
get
{
if (_register_date != null) return DateTime.Parse(_register_date.ToString());
else return DateTime.MinValue;
}
}
}
}
The SOAP Request XML:
POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.ividomain.somee.com/Service1/registerUser"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<registerUser xmlns="http://www.ividomain.somee.com/Service1">
<contato>
<UserId>int</UserId>
<UserEmail>string</UserEmail>
<UserPassword>string</UserPassword>
<UserSimOperator>string</UserSimOperator>
<UserCellPhoneSim>string</UserCellPhoneSim>
<UserFacebook>string</UserFacebook>
<UserLinkedin>string</UserLinkedin>
<UserGoogleplus>string</UserGoogleplus>
<UserOrkut>string</UserOrkut>
<UserTwitter>string</UserTwitter>
<UserLastRefresh>dateTime</UserLastRefresh>
<UserRegisterDate>dateTime</UserRegisterDate>
</contato>
</registerUser>
</soap:Body>
</soap:Envelope>
I've been stuck in this for a while and didn't found any implementation and deserialization of a java model in .NET. I hope someone out there helps me! =D
ThiagoMello, I had this error days ago and solve it generting java class from this site:
http://www.wsdl2code.com/Pages/Home.aspx
With this classes my code works fine! Try it.
Hope this helps.
Joan.

KSOAP2 - serialize complex object array in another complex object

I'm trying to send complex object to webmethod via ksoap2. A problem occurs when IDE execute AndroidHttpTransport.call command, it sends that error;
java.lang.RuntimeException: Cannot serialize: [webServiceClasses.movementItem#4123d840]
I sent parameter to webmethod which is below;
[WebMethod]
public TransferReturn ApplyVanToVanMovement(vanToVanMovement vMovement)
{
OracleConnection opCnn = new OracleConnection(Settings.Instance.GetConnectionString());
opCnn.Open();
DBOperations dbOp = new DBOperations();
.
.
.
}
Here is my code that sent paramateres to webmethod;
public static TransferReturn ApplyVanToVanMovement(TransferReturn pTransferReturn, vanToVanMovement vMovement)
throws Exception {
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "ApplyVanToVanMovement";
String SOAP_ACTION = "http://tempuri.org/ApplyVanToVanMovement";
String URL;
if (Settings.instance().getDocNum().isApn == 0)
URL = Settings.instance().getDocNum().WANIP + Settings.instance().getDocNum().WANTransferServiceUrl;
else
URL = Settings.instance().getDocNum().LANIP + Settings.instance().getDocNum().LANTransferServiceUrl;
PropertyInfo pi = new PropertyInfo();
pi.setName("vanToVanMovement");
pi.setValue(vMovement);
pi.setType(vMovement.getClass());
Request.addProperty(pi);
ArrayList<movementItem> alMi = vMovement.getItemCollectionField();
for (int i = 0; i < alMi.size(); i++) {
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("movementItem" + i);
pi2.setValue(alMi.get(i));
pi2.setType(movementItem.class);
Request.addProperty(pi2);
}
/*
* Set the web service envelope
*/
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(NAMESPACE, "vanToVanMovement", new vanToVanMovement().getClass());
for (int i = 0; i < alMi.size(); i++) {
envelope.addMapping(NAMESPACE, "movementItem" + i, movementItem.class);
}
MarshalDouble marshaldDouble = new MarshalDouble();
marshaldDouble.register(envelope);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
/*
* Call the web service and retrieve result ... how luvly <3
*/SoapObject response = null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
}
// (SoapObject) wsClass.InvokeMethodMarshal(pInfo);
pTransferReturn = (TransferReturn) DBLayout.setSoapObjectToClass(response, pTransferReturn);
return pTransferReturn;
}
My classes, first one is parameter that i sent, second one is property of first one as array(I tried same thing with Vector instead of ArrayList, nothing changed.);
public class vanToVanMovement implements WebServiceReturn {
private long companyIdField;
private long branchIdField;
private long createdUserIdField;
private long fromWarehouseIdField;
private long toWarehouseIdField;
private long representativeIdField;
private long routeIdField;
private long vehicleIdField;
private ArrayList<movementItem> itemCollectionField;
public vanToVanMovement() {
}
public long getCompanyIdField() {
return companyIdField;
}
public void setCompanyIdField(long companyIdField) {
this.companyIdField = companyIdField;
}
public long getBranchIdField() {
return branchIdField;
}
public void setBranchIdField(long branchIdField) {
this.branchIdField = branchIdField;
}
public long getCreatedUserIdField() {
return createdUserIdField;
}
public void setCreatedUserIdField(long createdUserIdField) {
this.createdUserIdField = createdUserIdField;
}
public long getFromWarehouseIdField() {
return fromWarehouseIdField;
}
public void setFromWarehouseIdField(long fromWarehouseIdField) {
this.fromWarehouseIdField = fromWarehouseIdField;
}
public long getToWarehouseIdField() {
return toWarehouseIdField;
}
public void setToWarehouseIdField(long toWarehouseIdField) {
this.toWarehouseIdField = toWarehouseIdField;
}
public long getRepresentativeIdField() {
return representativeIdField;
}
public void setRepresentativeIdField(long representativeIdField) {
this.representativeIdField = representativeIdField;
}
public long getRouteIdField() {
return routeIdField;
}
public void setRouteIdField(long routeIdField) {
this.routeIdField = routeIdField;
}
public long getVehicleIdField() {
return vehicleIdField;
}
public void setVehicleIdField(long vehicleIdField) {
this.vehicleIdField = vehicleIdField;
}
public ArrayList<movementItem> getItemCollectionField() {
return itemCollectionField;
}
public void setItemCollectionField(ArrayList<movementItem> itemCollectionField) {
this.itemCollectionField = itemCollectionField;
}
public Object getProperty(int index) {
switch (index) {
case 0:
return companyIdField;
case 1:
return branchIdField;
case 2:
return createdUserIdField;
case 3:
return fromWarehouseIdField;
case 4:
return toWarehouseIdField;
case 5:
return representativeIdField;
case 6:
return routeIdField;
case 7:
return vehicleIdField;
case 8:
return itemCollectionField;
default:
return null;
}
}
public int getPropertyCount() {
return 9;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo pInfo) {
switch (index) {
case 0:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "companyIdField";
break;
case 1:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "branchIdField";
break;
case 2:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "createdUserIdField";
break;
case 3:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "fromWarehouseIdField";
break;
case 4:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "toWarehouseIdField";
break;
case 5:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "representativeIdField";
break;
case 6:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "routeIdField";
break;
case 7:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "vehicleIdField";
break;
case 8:
pInfo.type = PropertyInfo.OBJECT_CLASS;
pInfo.name = "itemCollectionField";
break;
default:
break;
}
}
public void setProperty(int index, Object obj) {
switch (index) {
case 0:
companyIdField = Long.valueOf(obj.toString());
break;
case 1:
branchIdField = Long.valueOf(obj.toString());
break;
case 2:
createdUserIdField = Long.valueOf(obj.toString());
break;
case 3:
fromWarehouseIdField = Long.valueOf(obj.toString());
break;
case 4:
toWarehouseIdField = Long.valueOf(obj.toString());
break;
case 5:
representativeIdField = Long.valueOf(obj.toString());
break;
case 6:
routeIdField = Long.valueOf(obj.toString());
break;
case 7:
vehicleIdField = Long.valueOf(obj.toString());
break;
case 8:
itemCollectionField = (ArrayList<movementItem>) obj;
break;
default:
break;
}
}
public int getArrayNumber(String s) {
int i = -1;
if (s.equals("companyIdField"))
i = 0;
if (s.equals("branchIdField"))
i = 1;
if (s.equals("createdUserIdField"))
i = 2;
if (s.equals("fromWarehouseIdField"))
i = 3;
if (s.equals("toWarehouseIdField"))
i = 4;
if (s.equals("representativeIdField"))
i = 5;
if (s.equals("routeIdField"))
i = 6;
if (s.equals("vehicleIdField"))
i = 7;
if (s.equals("itemCollectionField"))
i = 8;
return i;
}
}
public class movementItem implements WebServiceReturn {
private long itemIdField;
private long unitSetIdField;
private long unitIdField;
private long unitSetId2Field;
private long unitId2Field;
private double convFact1Field;
private double convFact2Field;
private double planQuantityField;
private double planQuantity1Field;
private double planQuantity2Field;
public movementItem() {
}
public long getItemIdField() {
return itemIdField;
}
public void setItemIdField(long itemIdField) {
this.itemIdField = itemIdField;
}
public long getUnitSetIdField() {
return unitSetIdField;
}
public void setUnitSetIdField(long unitSetIdField) {
this.unitSetIdField = unitSetIdField;
}
public long getUnitIdField() {
return unitIdField;
}
public void setUnitIdField(long unitIdField) {
this.unitIdField = unitIdField;
}
public long getUnitSetId2Field() {
return unitSetId2Field;
}
public void setUnitSetId2Field(long unitSetId2Field) {
this.unitSetId2Field = unitSetId2Field;
}
public long getUnitId2Field() {
return unitId2Field;
}
public void setUnitId2Field(long unitId2Field) {
this.unitId2Field = unitId2Field;
}
public double getConvFact1Field() {
return convFact1Field;
}
public void setConvFact1Field(double convFact1Field) {
this.convFact1Field = convFact1Field;
}
public double getConvFact2Field() {
return convFact2Field;
}
public void setConvFact2Field(double convFact2Field) {
this.convFact2Field = convFact2Field;
}
public double getPlanQuantityField() {
return planQuantityField;
}
public void setPlanQuantityField(double planQuantityField) {
this.planQuantityField = planQuantityField;
}
public double getPlanQuantity1Field() {
return planQuantity1Field;
}
public void setPlanQuantity1Field(double planQuantity1Field) {
this.planQuantity1Field = planQuantity1Field;
}
public double getPlanQuantity2Field() {
return planQuantity2Field;
}
public void setPlanQuantity2Field(double planQuantity2Field) {
this.planQuantity2Field = planQuantity2Field;
}
public Object getProperty(int index) {
switch (index) {
case 0:
return itemIdField;
case 1:
return unitSetIdField;
case 2:
return unitIdField;
case 3:
return unitSetIdField;
case 4:
return unitSetId2Field;
case 5:
return unitId2Field;
case 6:
return convFact1Field;
case 7:
return convFact2Field;
case 8:
return planQuantityField;
case 9:
return planQuantity1Field;
default:
return null;
}
}
public int getPropertyCount() {
return 10;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo pInfo) {
switch (index) {
case 0:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "itemIdField";
break;
case 1:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "unitSetIdField";
break;
case 2:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "unitIdField";
break;
case 3:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "unitSetId2Field";
break;
case 4:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "unitId2Field";
break;
case 5:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "convFact1Field";
break;
case 6:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "convFact2Field";
break;
case 7:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "planQuantityField";
break;
case 8:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "planQuantity1Field";
break;
case 9:
pInfo.type = PropertyInfo.LONG_CLASS;
pInfo.name = "planQuantity2Field";
break;
default:
break;
}
}
public void setProperty(int index, Object obj) {
switch (index) {
case 0:
itemIdField = Long.valueOf(obj.toString());
break;
case 1:
unitSetIdField = Long.valueOf(obj.toString());
break;
case 2:
unitIdField = Long.valueOf(obj.toString());
break;
case 3:
unitSetId2Field = Long.valueOf(obj.toString());
break;
case 4:
unitId2Field = Long.valueOf(obj.toString());
break;
case 5:
convFact1Field = (double)Double.valueOf(obj.toString());
break;
case 6:
convFact2Field = (double)Double.valueOf(obj.toString());
break;
case 7:
planQuantityField = (double)Double.valueOf(obj.toString());
break;
case 8:
planQuantity1Field = (double)Double.valueOf(obj.toString());
break;
case 9:
planQuantity2Field = (double)Double.valueOf(obj.toString());
break;
default:
break;
}
}
public int getArrayNumber(String s) {
int i = -1;
if (s.equals("itemIdField"))
i = 0;
if (s.equals("unitSetIdField"))
i = 1;
if (s.equals("unitIdField"))
i = 0;
if (s.equals("unitSetId2Field"))
i = 1;
if (s.equals("unitId2Field"))
i = 0;
if (s.equals("convFact1Field"))
i = 1;
if (s.equals("convFact2Field"))
i = 0;
if (s.equals("planQuantityField"))
i = 1;
if (s.equals("planQuantity1Field"))
i = 0;
if (s.equals("planQuantity2Field"))
i = 1;
return i;
}
these two classes implement from ;
public interface WebServiceReturn extends KvmSerializable {
public int getArrayNumber(String s);
}
and at last, my marshaling metod, which is utilized to serialize doubles;
public class MarshalDouble implements Marshal {
public Object readInstance(XmlPullParser parser, String namespace, String name,
PropertyInfo expected) throws IOException, XmlPullParserException {
return Double.parseDouble(parser.nextText());
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping("http://tempuri.org/", "double", Double.class, this);
}
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(obj.toString());
}
}

Parsing complex soap response

I'm building my first application in android which consumes a wcf service.I'm using ksoap2 to parse the response.The response is actually an array of objects defined in C#.I did this following this really helpful guide.Now my problem is that I need to consume a wcf service which returns again an array of objects in C# but this time some properties of these objects are other objects.So my question is how can I map the inner objects so that i can parse their properties?
In case I was unclear, I need to parse an object like this:
public class OutterObject {
private InnerObject1 io1;
private InnerObject2 io2;
}
Hope I was clear enough
Ok this is my simplified code i can't post all of it just the portion i think is at fault
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("connString",args.get(0));
Request.addProperty("ClCode", args.get(1));
Request.addProperty("TeCode", args.get(2));
Request.addProperty("CourseDate", args.get(3));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(namespace, "SRV_WeekProgramm",newSRV_WeekProgramm().getClass());
envelope.addMapping(namespace, "Course", new Course().getClass());
envelope.addMapping(namespace, "StudentHours", new StudentHours().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Course response = (Course)envelope.bodyIn; //this is where it crashes
and this is the thrown exception: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to connectionInitializer.Course
Here is an example that worked for me.
This is response message type from web service
<message name="loginUserResponse">
<part name="code" type="xsd:int"/>
<part name="desc" type="xsd:string"/>
<part name="user" type="tns:user"/>
</message>
The method loginUser definition
<operation name="loginUser">
<documentation>Login user.</documentation>
<input message="tns:loginUserRequest"/>
<output message="tns:loginUserResponse"/>
</operation>
Class UserResponse (Outter) contains User property:
public class UserResponse implements KvmSerializable {
public int code;
public String desc;
public User user;
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return code;
case 1:
return desc;
case 2:
return user;
default:
break;
}
return null;
}
public int getPropertyCount() {
return 3;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "code";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "desc";
break;
case 2:
info.type = User.class;
info.name = "user";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch (index) {
case 0:
this.code = Integer.parseInt(value.toString());
break;
case 1:
this.desc = value.toString();
break;
case 2:
this.user = (User) value;
default:
break;
}
}
}
And the User class (Inner)
public class User implements KvmSerializable {
public int user_id;
public String username;
public String email;
public String password;
public User() {
}
public Object getProperty(int index) {
switch (index) {
case 0:
return user_id;
case 1:
return username;
case 2:
return email;
case 3:
return password;
default:
return null;
}
}
public int getPropertyCount() {
return 4;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "user_id";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "username";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "email";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "password";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
if(null == value)
value = "";
switch (index) {
case 0:
user_id = Integer.parseInt(value.toString());
break;
case 1:
username = value.toString();
break;
case 2:
email = value.toString();
break;
case 3:
password = value.toString();
break;
}
}
This is important: make sure you register the keys for both outter class and inner class.
envelope.addMapping(NAMESPACE, "loginUserResponse", UserResponse.class);
envelope.addMapping(NAMESPACE, "user", User.class);
Finally, you can get the result by casting:
HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); //open the requisition
androidHttpTransport.call(SOAP_ACTION, envelope);// call
UserResponse response = (UserResponse)envelope.bodyIn;
Hope this help!

Android ksoap2 sending complex object

I am trying to send a complex object through a soap service call in an android application. I am getting a quite weird error.
The class that I am trying to serialize and send (and the one that are included to it) is the following:
public class TSavedMessage extends BaseObject{
public static Class TSAVEDMESSAGE_CLASS = new TSavedMessage().getClass();
protected String sToken;
protected String dateDeferredDelivery;
protected int idDepartment;
protected boolean bDraft;
protected String txtRelationsType;
protected String txtRelatedIds;
protected int idForwardOf;
protected int idReplyOf;
protected String numPriority;
protected boolean bReadReceipt;
protected boolean bDeliveryReport;
protected String txtKeywords;
protected int numTimeOffset;
protected String txtTimeZone;
protected String txtSubject;
protected String txtBody;
protected TRecipient[] recipients;
public TSavedMessage() {
}
public TSavedMessage(String sToken, String dateDeferredDelivery, int idDepartment, boolean bDraft, String txtRelationsType, String txtRelatedIds, int idForwardOf, int idReplyOf, String numPriority, boolean bReadReceipt, boolean bDeliveryReport, String txtKeywords, int numTimeOffset, String txtTimeZone, String txtSubject, String txtBody, TRecipient[] recipients, int[] vesselRelations) {
this.sToken = sToken;
this.dateDeferredDelivery = dateDeferredDelivery;
this.idDepartment = idDepartment;
this.bDraft = bDraft;
this.txtRelationsType = txtRelationsType;
this.txtRelatedIds = txtRelatedIds;
this.idForwardOf = idForwardOf;
this.idReplyOf = idReplyOf;
this.numPriority = numPriority;
this.bReadReceipt = bReadReceipt;
this.bDeliveryReport = bDeliveryReport;
this.txtKeywords = txtKeywords;
this.numTimeOffset = numTimeOffset;
this.txtTimeZone = txtTimeZone;
this.txtSubject = txtSubject;
this.txtBody = txtBody;
this.recipients = recipients;
}
public String getSToken() {
return sToken;
}
public void setSToken(String sToken) {
this.sToken = sToken;
}
public String getDateDeferredDelivery() {
return dateDeferredDelivery;
}
public void setDateDeferredDelivery(String dateDeferredDelivery) {
this.dateDeferredDelivery = dateDeferredDelivery;
}
public int getIdDepartment() {
return idDepartment;
}
public void setIdDepartment(int idDepartment) {
this.idDepartment = idDepartment;
}
public boolean isBDraft() {
return bDraft;
}
public void setBDraft(boolean bDraft) {
this.bDraft = bDraft;
}
public String getTxtRelationsType() {
return txtRelationsType;
}
public void setTxtRelationsType(String txtRelationsType) {
this.txtRelationsType = txtRelationsType;
}
public String getTxtRelatedIds() {
return txtRelatedIds;
}
public void setTxtRelatedIds(String txtRelatedIds) {
this.txtRelatedIds = txtRelatedIds;
}
public int getIdForwardOf() {
return idForwardOf;
}
public void setIdForwardOf(int idForwardOf) {
this.idForwardOf = idForwardOf;
}
public int getIdReplyOf() {
return idReplyOf;
}
public void setIdReplyOf(int idReplyOf) {
this.idReplyOf = idReplyOf;
}
public String getNumPriority() {
return numPriority;
}
public void setNumPriority(String numPriority) {
this.numPriority = numPriority;
}
public boolean isBReadReceipt() {
return bReadReceipt;
}
public void setBReadReceipt(boolean bReadReceipt) {
this.bReadReceipt = bReadReceipt;
}
public boolean isBDeliveryReport() {
return bDeliveryReport;
}
public void setBDeliveryReport(boolean bDeliveryReport) {
this.bDeliveryReport = bDeliveryReport;
}
public String getTxtKeywords() {
return txtKeywords;
}
public void setTxtKeywords(String txtKeywords) {
this.txtKeywords = txtKeywords;
}
public int getNumTimeOffset() {
return numTimeOffset;
}
public void setNumTimeOffset(int numTimeOffset) {
this.numTimeOffset = numTimeOffset;
}
public String getTxtTimeZone() {
return txtTimeZone;
}
public void setTxtTimeZone(String txtTimeZone) {
this.txtTimeZone = txtTimeZone;
}
public String getTxtSubject() {
return txtSubject;
}
public void setTxtSubject(String txtSubject) {
this.txtSubject = txtSubject;
}
public String getTxtBody() {
return txtBody;
}
public void setTxtBody(String txtBody) {
this.txtBody = txtBody;
}
public TRecipient[] getRecipients() {
return recipients;
}
public void setRecipients(TRecipient[] recipients) {
this.recipients = recipients;
}
public Object getProperty(int index) {
switch (index) {
case 0:
return sToken;
case 1:
return dateDeferredDelivery;
case 2:
Integer iDepartment = new Integer(idDepartment);
return iDepartment;
case 3:
Boolean isDraft = new Boolean(bDraft);
return isDraft;
case 4:
return txtRelationsType;
case 5:
return txtRelatedIds;
case 6:
Integer iForwardOf = new Integer(idForwardOf);
return iForwardOf;
case 7:
Integer iReplyOf = new Integer(idReplyOf);
return iReplyOf;
case 8:
return numPriority;
case 9:
Boolean isReadReceipt = new Boolean(bReadReceipt);
return isReadReceipt;
case 10:
Boolean isDeliveryReport = new Boolean(bDeliveryReport);
return isDeliveryReport;
case 11:
return txtKeywords;
case 12:
Integer iTimeOffset = new Integer(numTimeOffset);
return iTimeOffset;
case 14:
return txtTimeZone;
case 15:
return txtSubject;
case 16:
return txtBody;
case 17:
return recipients; // complex type
default:
return null;
}
}
public int getPropertyCount() {
return 18;
}
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "sToken";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "dateDeferredDelivery";
break;
case 2:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "idDepartment";
break;
case 3:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "bDraft";
break;
case 4:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtRelationsType";
break;
case 5:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtRelatedIds";
break;
case 6:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "idForwardOf";
break;
case 7:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "idReplyOf";
break;
case 8:
info.type = PropertyInfo.STRING_CLASS;
info.name = "numPriority";
break;
case 9:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "bReadReceipt";
break;
case 10:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "bDeliveryReport";
break;
case 11:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtKeywords";
break;
case 12:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "numTimeOffset";
break;
case 14:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtTimeZone";
break;
case 15:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtSubject";
break;
case 16:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtBody";
break;
case 17:
info.type = PropertyInfo.VECTOR_CLASS;
info.name = "recipients"; // complex type
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch (index) {
case 0:
sToken = value.toString();
break;
case 1:
dateDeferredDelivery = value.toString();
break;
case 2:
idDepartment = Integer.parseInt(value.toString());
break;
case 3:
if(value.toString().equalsIgnoreCase("false")) bDraft = false;
else bDraft = true;
break;
case 4:
txtRelationsType = value.toString();
break;
case 5:
txtRelatedIds = value.toString();
break;
case 6:
idForwardOf = Integer.parseInt(value.toString());
break;
case 7:
idReplyOf = Integer.parseInt(value.toString());
break;
case 8:
numPriority = value.toString();
break;
case 9:
if(value.toString().equalsIgnoreCase("false")) bReadReceipt = false;
else bReadReceipt = true;
break;
case 10:
if(value.toString().equalsIgnoreCase("false")) bDeliveryReport = false;
else bDeliveryReport = true;
break;
case 11:
txtKeywords = value.toString();
break;
case 12:
numTimeOffset = Integer.parseInt(value.toString());
break;
case 14:
txtTimeZone = value.toString();
break;
case 15:
txtSubject = value.toString();
break;
case 16:
txtBody = value.toString();
break;
case 17:
recipients = (TRecipient[]) value; // complex type
break;
default:
break;
}
}
}
public class TRecipient extends BaseObject{
public static Class TRECIPIENT_CLASS = new TRecipient().getClass();
protected int idRecipient;
protected String txtRealAddress;
protected String Visibility;
public TRecipient() {
}
public TRecipient(int idRecipient, String txtRealAddress, String visibility) {
this.idRecipient = idRecipient;
this.txtRealAddress = txtRealAddress;
this.Visibility = visibility;
}
public int getIdRecipient() {
return idRecipient;
}
public void setIdRecipient(int idRecipient) {
this.idRecipient = idRecipient;
}
public String getTxtRealAddress() {
return txtRealAddress;
}
public void setTxtRealAddress(String txtRealAddress) {
this.txtRealAddress = txtRealAddress;
}
public String getVisibility() {
return Visibility;
}
public void setVisibility(String visibility) {
this.Visibility = visibility;
}
public Object getProperty(int index) {
switch (index) {
case 0:
Integer iRecipient = new Integer(idRecipient);
return iRecipient;
case 1:
return txtRealAddress;
case 2:
return Visibility;
default:
return null;
}
}
public int getPropertyCount() {
return 3;
}
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "idRecipient";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "txtRealAddress";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Visibility";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch (index) {
case 0:
idRecipient = Integer.parseInt(value.toString());
break;
case 1:
txtRealAddress = value.toString();
break;
case 2:
Visibility = value.toString();
break;
default:
break;
}
}
}
The method that sends the request is the following:
TSavedMessage msgTemp = new TSavedMessage();
msgTemp.setSToken(LoginActivity.sToken);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
msgTemp.setDateDeferredDelivery(dateFormat.format(date));
msgTemp.setIdDepartment(datasource.getDepartmentID(departmentString));
msgTemp.setBDraft(false);
msgTemp.setTxtRelationsType("");
msgTemp.setTxtRelatedIds("");
msgTemp.setIdForwardOf(params[1]);
msgTemp.setIdReplyOf(params[2]);
msgTemp.setNumPriority("Normal");
msgTemp.setBReadReceipt(false);
msgTemp.setBDeliveryReport(false);
msgTemp.setTxtKeywords(keywords.getText().toString());
msgTemp.setNumTimeOffset(0);
msgTemp.setTxtTimeZone("0");
msgTemp.setTxtSubject(subject.getText().toString());
msgTemp.setTxtBody(body.getText().toString());
TRecipient[] recarrayTemp = new TRecipient[1];
recarrayTemp[0] = new TRecipient(0, "nickvoulgaris#hotmail.com", "To");
msgTemp.setRecipients(recarrayTemp);
SoapObject recipients = new SoapObject(LoginActivity.NAMESPACE, "Recipients");
SoapObject recarray[] = new SoapObject[1];
for (int i = 0; i < 1; i++){
recarray[i] = new SoapObject(BaseObject.NAMESPACE, "TRecipient");
recarray[i].addProperty("idRecipient", Integer.toString(0));
recarray[i].addProperty("txtRealAddress", "nickvoulgaris#hotmail.com");
recarray[i].addProperty("Visibility", "To");
recipients.addProperty("TRecipient", recarray[i]);
}
SoapObject msg = new SoapObject(BaseObject.NAMESPACE, "MessageToSend");
msg.addProperty("sToken", LoginActivity.sToken);
msg.addProperty("dateDeferredDelivery", msgTemp.getDateDeferredDelivery());
msg.addProperty("idDepartment", Integer.toString(msgTemp.getIdDepartment()));
msg.addProperty("bDraft", "false");
msg.addProperty("txtRelationsType", Integer.toString(0));
msg.addProperty("txtRelatedIds", Integer.toString(0));
msg.addProperty("idForwardOf", Integer.toString(0));
msg.addProperty("idReplyOf", Integer.toString(0));
msg.addProperty("numPriority", msgTemp.getNumPriority());
msg.addProperty("bReadReceipt", "false");
msg.addProperty("bDeliveryReport", "false");
msg.addProperty("txtKeywords", msgTemp.getTxtKeywords());
msg.addProperty("numTimeOffset", msgTemp.getNumTimeOffset());
msg.addProperty("txtTimeZone", msgTemp.getTxtTimeZone());
msg.addProperty("txtSubject", msgTemp.getTxtSubject());
msg.addProperty("txtBody", msgTemp.getTxtBody());
msg.addProperty("Recipients", recipients);
SoapObject rpc = new SoapObject(BaseObject.NAMESPACE, METHOD_NAME);
rpc.addProperty("sToken", LoginActivity.sToken);
rpc.addProperty("MessageToSend", msg);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(LoginActivity.URL);
androidHttpTransport.debug = true;
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
androidHttpTransport.call(SOAP_ACTION, envelope);
System.out.println("=== " + envelope.getResponse().toString());
}
catch(XmlPullParserException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
Finally, the error that I'm getting is this one:
SoapFault - faultcode: 'EConvertError' faultstring: '''0nickvoulgaris#hotmail.comTo'' is not a valid integer value'
faultactor: 'null' detail: null
It is quite unfortunate that the error points me to nowhere. I cannot understand why is trying to pass these 3 fields as an Integer. Also what troubles me is the fact that there is a triple quote in the left hand side and a double one in the right hand side.
Any thoughts could prove extremely valuable. Thanks in advance.
Also, the soap object that this code generates and tries to send seems perfectly ok. It is the following:
MobSaveMessage{
sToken=MOB-2704FB8FFBC946469408A3BEE90CA163;
MessageToSend=MessageToSend{
sToken=MOB-2704FB8FFBC946469408A3BEE90CA163;
dateDeferredDelivery=2012/07/25 13:30:36;
idDepartment=18;
bDraft=false;
txtRelationsType=0;
txtRelatedIds=0;
idForwardOf=0;
idReplyOf=0;
numPriority=Normal;
bReadReceipt=false;
bDeliveryReport=false;
txtKeywords=key;
numTimeOffset=0;
txtTimeZone=0;
txtSubject=sub;
txtBody=mail;
Recipients=Recipients{
TRecipient=TRecipient{
idRecipient=0;
txtRealAddress=nickvoulgaris#hotmail.com;
Visibility=To;
};
};
};
}
After all it turns out that we had a wrong version of the WSDL file sent to us along with the specifications. I'm really sorry if I put anyone into trouble. Not really my fault. I'm just responding in case anyone bumps into the question.

kSoap2 Android -- Cast Class Exception (SoapObject)

I'm currently trying to retrieve an array from my webservice by using regular Ksoap implementation. However, when trying to access this array I am getting the following logcat error:
04-27 00:27:01.772: ERROR/AndroidRuntime(530): Caused by: java.lang.RuntimeException: Unable to
start activity ComponentInfo{android.waiter/android.waiter.TablesActivity}:
java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive
It appears that for some reason Soap is not getting along with the way I am casting my property values. Attached is the problem code (and related methods)
public static TableDTO[] RetrieveFromSoap(SoapObject soap)
{
TableDTO[] tables = new TableDTO[soap.getPropertyCount()];
for (int i = 0; i < tables.length-1; i++) {
SoapObject pii = (SoapObject) soap.getProperty(i);
TableDTO table = new TableDTO();
table.setTableID(Integer.parseInt(pii.getProperty(0).toString()));
table.setCapacity(Integer.parseInt(pii.getProperty(1).toString()));
table.setStatusFlag(pii.getProperty(2).toString());
table.setCurrentWaiter(pii.getProperty(3).toString());
tables[i] = table;
}
return tables;
}
TableDTO Information:
#Override
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return TableID;
case 1:
return Capacity;
case 2:
return StatusFlag;
case 3:
return currentWaiter;
}
return null;
}
#Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 3;
}
#Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
switch(arg0){
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "TableID";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Capacity";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "StatusFlag";
break;
case 3:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "currentWaiter";
break;
default:break;
}
}
#Override
public void setProperty(int arg0, Object value) {
switch(arg0)
{
case 0:
TableID = Integer.parseInt(value.toString());
break;
case 1:
Capacity = Integer.parseInt(value.toString());
break;
case 2:
StatusFlag = value.toString();
break;
case 3:
currentWaiter = value.toString();
break;
default :
break;
}
}
The problem is with the line
SoapObject pii = (SoapObject) soap.getProperty(i);
It does not like the casting there. I'm at a loss on how to fix this.
Thanks for your time and help!
Instead of using
SoapObject response = (SoapObject)envelope.getResponse();
Use
SoapObject response = (SoapObject)envelope.bodyIn;
That worked for me.

Categories

Resources