每次我尝试从android studio中的单元测试向WCF服务发送变量时,服务都不会得到它,它只是开始运行该方法并返回false(参见下面的代码)。奇怪的是,我可以使用不需要变量的方法从服务中获取数据。
这是服务代码:
public bool ValidarAdm(string jsonString)
{
if (jsonString != null)
{
Negocio.Administrador a = Negocio.Administrador.Deserializar(jsonString);
return a.ValidarUsuario();
}
else
{
return false;
}
}
这是Android studio中的连接类
private String soapAction;
private String nameSpace;
private String methodName;
private String url;
public Connection(String soapAction, String nameSpace, String methodName, String url) {
this.soapAction = soapAction;
this.nameSpace = nameSpace;
this.methodName = methodName;
this.url = url;
}
public String getData(String parameter, String value)
{
SoapObject request = new SoapObject(nameSpace,methodName);
request.addProperty(parameter,value);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTrasporSE= new HttpTransportSE(url);
try {
androidHttpTrasporSE.call(soapAction,envelope);
} catch (IOException e)
{
e.printStackTrace();
} catch (XmlPullParserException e)
{
e.printStackTrace();
}
SoapObject result = (SoapObject)envelope.bodyIn;
String s ="";
if(result!=null)
{
s= result.getProperty(0).toString();
}
return s;
}
和单元测试中的代码
public void test_ValidarAdmin_isCorrect()
{
String soapAction="localhost/IEcommerceService/ValidarAdm";
String nameSpace="localhost/";
String methodName="ValidarAdm";
String url="http://localhost:65080/EcommerceService.svc?Wsdl";
Connection con = new Connection(soapAction,nameSpace,methodName,url);
Admin ad = new Admin("a","a",1);
Gson g = new Gson();
String jsonString= g.toJson(ad);
String a ="";
System.out.println(methodName);
System.out.println(jsonString);
a=con.getData(methodName,jsonString);
System.out.println(a);
}