Search code examples
androidenumsksoap2runtimeexception

Got RuntimeException when call web service via ksoap2-android with pass enum as parameter


I want to add an enumeration as parameter of web service request. I use ksoap2 on android but I got java.lang.RuntimeException: Cannot serialize: UNDEFINED when pass enumeration I implement enumeration follow How to pass an enum value to wcf webservice (Fildor answer)

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

and I pass DrivingLicenseTypeEnum as parameter by

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

Could you please help me for resolve this problem? Thank you very much.


Solution

  • Oh I got an answer this problem is occur because I use enum that implements Marshal. When I divide this enum to class and enum for example

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

    It work correctly.