Search code examples
c#androidjsonwcfksoap2

Send data to WCF from Android using KSOAP2


My code is,

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("ItemList", mainObject.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
envelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
try {
    transport.call(SOAP_ACTION, envelope);
} catch (final Exception e) {
    activity.runOnUiThread(new Runnable() {

        public void run() {
            new CustomToast(activity, SOAP_ACTION + " - "
                    + e.getMessage() + " error").show();
            e.printStackTrace();
        }
    });
}
try {
    fault = (SoapFault) envelope.bodyIn;
    activity.runOnUiThread(new Runnable() {

        public void run() {
            if (fault != null) {
                new CustomToast(activity, fault.getMessage())
                        .show();
            }
        }
    });
} catch (Exception e) {
    e.printStackTrace();
}
try {
    result = (SoapObject) envelope.bodyIn;
} catch (Exception e) {
    e.printStackTrace();
}

whereas mainObject is a JSONObject, which contain following data,

{"ItemList":[{"ID":"","Name":"Abc","Mark":"81"},{"ID":"","Name":"XYZ","Mark":"82"}]}

I am receiving this in my WCF as following way.

[OperationContract]
void InsertUpdateEntry(Items ItemList);

and Items class is

[CollectionDataContract(Namespace = "")]
public class Items : List<clsitems>
{
}

and clsitems class is

[DataContract]
public class clsitems
{
    [DataMember]
    public string ID { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Mark { get; set; }
}

And finally I am facing following exception.

java.io.IOException: HTTP request failed, HTTP status: 500

I want to send data to Items class only, so any other solution is also acceptable.


Solution

  • Try setting the ItemName on the CollectionDataContract attribute. For example:

    [CollectionDataContract(Name = "Custom{0}List", ItemName = "CustomItem")]
    public class Items : List<clsitems>
    {
    }
    

    The CollectionDataContractAttribute is intended to ease interoperability when working with data from non- providers and to control the exact shape of serialized instances. To this end, the ItemName property enables you to control the names of the repeating items inside a collection. This is especially useful when the provider does not use the XML element type name as the array item name, for example, if a provider uses "String" as an element type name instead of the XSD type name "string".

    Taken from here