1

我想添加一个枚举作为 Web 服务请求的参数。我在android上使用kso​​ap2,但我得到java.lang.RuntimeException:无法序列化: 通过枚举时未定义我实现枚举遵循如何将枚举值传递给wcf webservice(Fildor回答)

interface BaseEnum extends Marshal
{
    public String getDesc(Enum en);
}
enum DrivingLicenseTypeEnum implements BaseEnum 
{
    UNDEFINED,
    NSURED_DRIVER_INJURED,
    INSURED_PASSENGER_INJURED,
    PARTY_DRIVER_INJURED,
    PARTY_PASSENGER_INJURED,
    THIRD_PARTY_INJURED,
    INSURED_VEHICLE_DAMAGE,
    PARTY_VEHICLE_DAMAGE,
    ASSET;

    public  String getDesc(Enum en) {
        String result="";
        //generate description
        return result;
    }

    public Object readInstance(XmlPullParser arg0, String arg1, String arg2,
            PropertyInfo arg3) throws IOException, XmlPullParserException {
        // TODO Auto-generated method stub
        return DrivingLicenseTypeEnum.valueOf(arg0.nextText());
    }

    public void register(SoapSerializationEnvelope arg0) {
        arg0.addMapping("http://tempuri.org/", "DrivingLicenseTypeEnum", DrivingLicenseTypeEnum.class);
    }

    public void writeInstance(XmlSerializer arg0, Object arg1)
            throws IOException {
        arg0.text(((DrivingLicenseTypeEnum)arg1).name());
    }
}

我通过 DrivingLicenseTypeEnum 作为参数传递

DrivingLicenseTypeEnum c = DrivingLicenseTypeEnum.UNDEFINED;
PropertyInfo pi=new PropertyInfo();
pi.setName("driverLicenseType");
pi.setValue(c);
pi.setType(DrivingLicenseTypeEnum.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
c.register(envelope);

你能帮我解决这个问题吗?非常感谢你。

4

1 回答 1

0

哦,我得到了一个答案,因为我使用了实现 Marshal 的枚举,所以会出现这个问题。例如,当我将此枚举划分为类和枚举时

enum DrivingLicenseTypeEnum 
{
    UNDEFINED,
    NSURED_DRIVER_INJURED,
    INSURED_PASSENGER_INJURED,
    PARTY_DRIVER_INJURED,
    PARTY_PASSENGER_INJURED,
    THIRD_PARTY_INJURED,
    INSURED_VEHICLE_DAMAGE,
    PARTY_VEHICLE_DAMAGE,
    ASSET
}

class DrivingLicenseTypeEnumClass implements Marshal
{
public Object readInstance(XmlPullParser arg0, String arg1, String arg2,
            PropertyInfo arg3) throws IOException, XmlPullParserException {
        // TODO Auto-generated method stub
        return DrivingLicenseTypeEnum.valueOf(arg0.nextText());
    }

    public void register(SoapSerializationEnvelope arg0) {
        arg0.addMapping("http://tempuri.org/", "DrivingLicenseTypeEnum", DrivingLicenseTypeEnum.class,new DrivingLicenseTypeEnumClass());
    }

    public void writeInstance(XmlSerializer arg0, Object arg1)
            throws IOException {
        arg0.text(((DrivingLicenseTypeEnum)arg1).name());
    }

}

它工作正常。

于 2012-05-06T07:05:56.797 回答