将自定义对象数组从Android传递到WCF:反序列化程序不知道映射到此名称的任何类型

时间:2017-10-03 10:41:13

标签: c# android wcf soap

我正在尝试从android向WCF发送一个自定义对象类型数组

我将此错误视为来自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. 

我使用的代码来自此问题的接受答案中列出的示例code

以下是客户的KvmSerializable用户定义类:

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;
}

}

 }

现在是客户的c#用户定义类

 public class Customer
{
    public int Customer_ID;
    public string Customer_Name;
    public string Customer_Family;
}

这是我为通过以下方式发送KvmSerializable对象而定义的CallSoap类:

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;

    }
 }

最后是服务器端的AddCustomer方法:

[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;

}

0 个答案:

没有答案