Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an enum value to wcf webservice

can ksoap2 pass an enum to webservice?

there is a wcf webservice:

[OperationContract]
string TestEnum(CodeType code);

CodeType is dotnet enum:

    public enum CodeType
    {
        [EnumMember]
        ALL,

        [EnumMember]
        VehicleColor
    }

How can i call this wcf webservice at android client?

i create a enum CodeType and implements KvmSerializable. In method getPropertyInfo, what is the value of info.name(info.type)?

public enum CodeType implements KvmSerializable, BaseInterface {
    ALL,

    VehicleColor;
//.......
    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        //info.namespace = this.NameSpace;
        info.name = ?;
        info.type = ?;

    }
}

Thanks for your help.

like image 860
user730417 Avatar asked Apr 29 '11 03:04

user730417


People also ask

How do you give an enum a value?

You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once. Therefore, the constructor must be declared private or default. To returns the values of the constants using an instance method(getter).

How do you return the value of an enum?

values() method can be used to return all values present inside the enum. Order is important in enums.By using the ordinal() method, each enum constant index can be found, just like an array index. valueOf() method returns the enum constant of the specified string value if exists.

Can we get an enum by value?

Get the value of an EnumTo get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

Can we change enum values?

Enum constants are implicitly static and final and you can not change their value once created.


1 Answers

I just solved that enum-Problem via Marshal.

I created a Java-Enum "copying" the .net one. I then wrote a Marshal-Class for it:

public class MarshalEnum implements org.ksoap2.serialization.Marshal
{
    ... // Singleton-Pattern

     public Object readInstance(XmlPullParser xpp, String string, String string1,
                           PropertyInfo pi)
        throws IOException, XmlPullParserException
{
    return MyEnum.valueOf( xpp.nextText() );
}

public void writeInstance(XmlSerializer xs, Object o)
        throws IOException
{
    xs.text(((MyEnum)o).name());
}

public void register(SoapSerializationEnvelope sse)
{
    sse.addMapping(sse.xsd, "MyEnum", MyEnum.class, MarshalEnum.getInstance() );
}
} // class

Then, when calling the Method where MyEnum-Values shall be sent:

//... blah blah
SoapSerializationEnvelope envelope =
                          new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(SOAP_REMOTE_NAMESPACE, "MyEnum", MyEnum.class,       
                    MarshalEnum.getInstance());
//... and so on.

Note that SOAP_REMOTE_NAMESPACE is the data contract namespace of the enum to be used! See the wsdl-file to find it out if you're not sure. Should look something like "http://schemas.datacontract.org/2009/08/Your.dotNet.Namespace".

I hope this is going to work for you, too.

like image 156
Fildor Avatar answered Oct 19 '22 12:10

Fildor