所以我在 IIS 上用 C# 设置了一个 Web 服务。我有3种方法。2个工作,1个包含布尔值作为参数不。
似乎android ksoap2对布尔值和.net的定义不同。如此之多,以至于当我联系网络服务时,所有参数都是默认值(0 表示 int,false 表示 bool)。
我只是更改了我的android代码以传递整数而不是布尔值,并且网络服务接受整数并将它们转换为布尔值。
但是有没有聪明的解决方案?
[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();
}
我的安卓代码:
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;
}