我在测试Android应用程序尝试传递字符串参数时遇到问题,我希望返回参数的值,这是我的Web服务代码:
[WebService(Namespace = "http://xxx.xxx.x.xxx:8080/root")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public String SayHello(String name)
{
return name;
}
}
}
此 Web 服务托管在我的本地 PC IIS 上这是我的安卓肥皂电话:
public class CallSoap {
// Lever one
public final String SOAP_SAY_HELLO = "http://xxx.xxx.x.xxx:8080/root/SayHello";
// Lever two operation
public final String SAY_HELLO_OPERATION = "SayHello";
// Lever four Soap target namespace
public final String WSDL_TARGET_NAMESPACE = "http://xxx.xxx.x.xxx:8080/root";
// Lever four Soap address
public final String SOAP_ADDRESS = "http://xxx.xxx.x.xxx:8080/Service1.asmx";
// Call say hello Method
public String getHello(String nameVal) {
String resp = new String();
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
SAY_HELLO_OPERATION);
request.addProperty("name", "Test");
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelop.dotNet = false;
envelop.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Log.d("HTTP REQUEST", "HTTP REQUEST:n" + httpTransport.requestDump);
Log.d("HTTP RESPONSE", "HTTP RESPONSE:n" + httpTransport.responseDump);
Object response1 = new Object();
try {
httpTransport
.setXmlVersionTag("<?xml version="1.0" encoding="UTF-8"?>");
httpTransport.call(SOAP_SAY_HELLO, envelop);
if (envelop.bodyIn instanceof SoapObject) { // SoapObject = SUCCESS
SoapObject soapObject = (SoapObject) envelop.bodyIn;
response1 = envelop.getResponse();
} else if (envelop.bodyIn instanceof SoapFault) { // SoapFault =
// FAILURE
SoapFault soapFault = (SoapFault) envelop.bodyIn;
response1 = soapFault.getClass();
throw new Exception(soapFault.getMessage());
}
return resp;
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
}
我得到空值作为返回值,如果我检查肥皂信封,我可以在数据包中看到我的参数。 我没有想法 发送了参数发生了什么。
请任何可以帮助我确定错误的人,提前感谢
我发现出了什么问题;当我将 Web 服务托管到本地 IIS 时,我必须添加包含托管 Web 服务文件的文件夹的安全性,在我的代码中,我不得不更改envelope.dotNet=true
而不是envelope.dotNet=false
仅此而已。