java–Ksoap:将用户定义的类作为参数传递给web方法时无法序列化异常

java–Ksoap:将用户定义的类作为参数传递给web方法时无法序列化异常,第1张

概述我花了几天时间试图找出如何使我的用户定义的java类可序列化,以便我可以将它作为参数发送到androidksoap调用c#web方法.下面是我的代码和调用webservice时在logcat中抛出的异常,如果我得到即时答复或帮助,我将感激不尽.我的java类XY.java:importorg.ksoap2.serialization.KvmS

我花了几天时间试图找出如何使我的用户定义的java类可序列化,以便我可以将它作为参数发送到androID ksoap调用c#web方法.
下面是我的代码和调用webservice时在logcat中抛出的异常,如果我得到即时答复或帮助,我将感激不尽.

我的java类XY.java:

import org.ksoap2.serialization.KvmSerializable;import org.ksoap2.serialization.PropertyInfo;public class XY implements KvmSerializable {    public static Class XY_CLASS = XY.class;    private String MyNum;    private String OppPhoneNum;    private String name;    public XY()    {    }    public XY(String MyNum, String name, String oppNum)    {        this.MyNum = MyNum;        this.name = name;        this.OppPhoneNum = oppNum;    }    public String getPhoneNum() {        return MyNum;    }    public voID setPhoneNum(String MyNum) {        this.MyNum = MyNum;    }    public String getname() {        return name;    }    public voID setname(String name) {        this.name = name;    }    public String getopponentPhoneNum() {        return OppPhoneNum;    }    public voID setopponentPhoneNum(String OppPhoneNum) {        this.OppPhoneNum = OppPhoneNum;    }    @OverrIDe    public Object getProperty(int arg0) {        switch(arg0)        {        case 0:            return MyNum;        case 1:            return OppPhoneNum;        case 2:            return name;        }        return null;    }    @OverrIDe    public int getPropertyCount() {        // Todo auto-generated method stub        return 3;    }    @OverrIDe    public voID getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {        switch(index)        {        case 0:            info.type = PropertyInfo.STRING_CLASS;            info.name = "MyNum";            break;        case 1:            info.type = PropertyInfo.STRING_CLASS;            info.name = "OppPhoneNum";            break;        case 2:            info.type = PropertyInfo.STRING_CLASS;            info.name = "name";            break;        default:break;        }    }    @OverrIDe    public voID setProperty(int index, Object value) {        switch(index)        {        case 0:            MyNum = value.toString();            break;        case 1:            OppPhoneNum = value.toString();            break;        case 2:            name = value.toString();            break;        default:            break;        }    }}

C#等价类:

[Serializable]public class XY    {        public System.String name        {            get;            set;        }        public System.String MyNum        {            get;            set;        }        public System.String OppPhoneNum        {            get;            set;        }    }

这就是我从我的活动中使用kso​​ap调用服务的方式:

private voID unKNown(List<XY> entrIEs)     {         //Initialize soap request + add parameters        SoapObject request = new SoapObject(nameSPACE, METHOD_name);         PropertyInfo entrIEsProp =new PropertyInfo();        entrIEsProp.setname("entrIEs");                  entrIEsProp.setValue(entrIEs);        entrIEsProp.setType(ArrayList.class);        //Use this to add parameters        request.addProperty(entrIEsProp);        //Declare the version of the SOAP request        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);        envelope.setoutputSoapObject(request);        envelope.addMapPing(nameSPACE, "XY", XY.XY_CLASS);        envelope.dotNet = true;        try {              httpTransportSE androIDhttpTransport = new httpTransportSE(URL);              //this is the actual part that will call the webservice              androIDhttpTransport.call(SOAP_ADDCONTACTS, envelope);              // Get the SoapResult from the envelope body.              if (envelope.bodyIn instanceof SoapFault)               {                  String str= ((SoapFault) envelope.bodyIn).faultstring;                  Log.i("", str);              }               else               {                  SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;                  if(resultsRequestSOAP != null)                  {                      Log.i("AddContacts", "Adding Contacts succeeded");                  }              }        }        catch (Exception e)        {              e.printstacktrace();        }    }**

Logcat例外:

java.lang.RuntimeException: Cannot Serialize: [XY .....** 

注意:我正在尝试将XY对象列表作为参数传递给Web服务方法.
我将不胜感激任何帮助 .

解决方法:

我编辑了我的示例并为您添加了新的完整示例,我认为它可以帮助您.在这个例子中,我在服务器端数据库中有一个customer表,我想通过KvmSerializable用户定义的类将androID中的数据插入到该表中.
以下是客户的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 soapAddCustomer = 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);    }    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;}
总结

以上是内存溢出为你收集整理的java – Ksoap:将用户定义的类作为参数传递给web方法时无法序列化异常全部内容,希望文章能够帮你解决java – Ksoap:将用户定义的类作为参数传递给web方法时无法序列化异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1101019.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存