I'm trying to send an array of custom object type from android to WCF
I'm getting this error as a response from the WCF.
'The formatter threw an exception while trying to deserialize the message:
There was an error while trying to deserialize parameter
http://tempuri.org/:customers. The deserializer has no knowledge of any
type that maps to this name.
The code that I've used is from the example listed in the accepted answer of this question code
Here is KvmSerializable user defined class for customers:
public class Customer implements KvmSerializable {
public int Customer_ID;
public String Customer_Name;
public String Customer_Family;
public Customer() {
}
public Customer(int customer_id,
String customer_name,
String customer_family) {
Customer_ID = customer_id;
Customer_Name = customer_name;
Customer_Family = customer_family;
}
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return Customer_ID;
case 1:
return Customer_Name;
case 2:
return Customer_Family;
}
return null;
}
public int getPropertyCount() {
// TODO Auto-generated method stub
return 25;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
// TODO Auto-generated method stub
switch (index) {
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "Customer_ID";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Customer_Name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Customer_Family";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
// TODO Auto-generated method stub
switch (index) {
case 0:
Customer_ID = Integer.parseInt(value.toString());
break;
case 1:
Customer_Name = value.toString();
break;
case 2:
Customer_Family = value.toString();
break;
default:
break;
}
}
}
And now c# user defined class for customers
public class Customer
{
public int Customer_ID;
public string Customer_Name;
public string Customer_Family;
}
Here is CallSoap class that I defined for send KvmSerializable object via this:
public class CallSoap {
public static String NAMESPACE = "http://127.0.0.1:80/";
public static String URL = "http://127.0.0.1:80/service.asmx?WSDL";
public static Customer[] customers;
public static int AddCustomer(Customer[] customers) {
String MethodName = "AddCustomer";
SoapObject soapAddRequest = new SoapObject(NAMESPACE, MethodName);
//customers Parameter
SoapObject soapDetails = new SoapObject(NAMESPACE, "customers");
SoapObject soapDetail[] = new SoapObject[customers.length];
for (int i=0;i<customers.length;i++){
soapDetail[i]= new SoapObject(NAMESPACE, "Customer");
soapDetail[i].addProperty("Customer_ID", customers[i].Customer_ID);
soapDetail[i].addProperty("Customer_Name", customers[i].Customer_Name);
soapDetail[i].addProperty("Customer_Family",
customers[i].Customer_Family);
soapDetails.addSoapObject(soapDetail[i]);
}
soapAddRequest.addSoapObject(soapDetails);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapAddRequest);
envelope.addMapping(NAMESPACE, "Customer", new Customer().getClass());
HttpTransportSE HttpTransportSE = new HttpTransportSE(URL);
try {
HttpTransportSE.call(NAMESPACE + MethodName, envelope);
String result = envelope.getResponse().toString();
return Integer.parseInt(result);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
and finally the AddCustomer method in server side:
[WebMethod]
public int AddCustomer(Customer[] customers)
{
for(int i=0;i<customers.Length;i++){
//Access to customer fields for allrows via
int id = customers[i].Customer_ID;
String name = customers[i].Customer_Name;
String = customers[i].Customer_Family;
}
return customers.Length;
}