Web服务使用android ksoap2方法从应用程序接收空参数

时间:2012-07-19 12:28:31

标签: c# android web-services soap android-ksoap2

所以我通过IIS在C#中设置了Webservice。 我有3种方法。 2工作,1包含布尔值作为参数不。

似乎android ksoap2对boolean和.net的定义有所不同。 所以,当我联系web服务时,所有参数都是默认值(0表示int,false表示bool)。

我刚刚更改了我的android代码以传递ints而不是bools和webservice接受int并将它们转换为bools。

但是有一个聪明的解决方案吗?

[WebService(Namespace = "http://myNameSpace/")]
....
    [WebMethod] //This works fine!
    public List<myCustomClass> GetData()
    {
        myEntities db = new myEntities();
        return db.myCustomClassTable.ToList();
    }

    [WebMethod] //This works fine!
    public List<myCustomClass> GetDataByID(int id)
    {
        myEntities db = new myEntities();
        var res = from p in db.myCustomClassTable where p.id == id select p;
        return res.ToList();
    }

    [WebMethod] //This DOESNT work with android ksoap2
    public List<myCustomClass> GetDataBySomeBool(int id, boolean someBool)
    {
        ..... // I set up some logging here to check incoming params

        myEntities db = new myEntities();
        var res = from p in db.myCustomClassTable 
            where p.id == id && p.SomeBool == someBool 
            select p;
        return res.ToList();
    }

我的Android代码:

public static String Get_Data(int id, bool someBool) {

    try {
        SoapObject request = new SoapObject("http://myNameSpace/", "GetDataBySomeBool");

        // Use this to add parameters
        PropertyInfo pi1 = new PropertyInfo(), pi2 = new PropertyInfo();
        pi1.setName("id");
        pi1.setValue(id);
        pi1.setType(int.class);
        request.addProperty(pi1);
        pi2.setName("someBool");
        pi2.setValue(someBool);
        pi2.setType(boolean.class);
        request.addProperty(pi2);

        // Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            String SOAP_ACTION = "http://myNameSpace/GetDataBySomeBool";
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SoapObject result = (SoapObject) envelope.bodyIn;

        if (result != null) {
            .....
        }
    } catch (Exception err) {
        err.printStackTrace();
        return null;
    }
    return null;
}

0 个答案:

没有答案