我使用ksoap2套件要呼叫.Net Webservice﹐由网络上找来的范例﹐如果呼叫的是没有参数的webservice是可以﹐但如果是有参数的webservice却是参数一直无法正确的传递。我的.net webservice如下 //没有参数的
[WebMethod]
public string HelloWorld() {
string str = "Android Call me";
return str;
}
//有参数的
[WebMethod]
public string ShowData(string strValue) {
WriteLog("to Call and str= "+strValue);
if (strValue == null) {
strValue = "strValue is null";
} else if (strValue == "") {
strValue = "str is space";
}
return strValue;
}
在android中的code如下 private static final String SOAP_ACTION="http://tempuri.org/HelloWorld";
private static final String METHOD_NAME="HelloWorld";
private static final String SOAP_ACTION2="http://tempuri.org/ShowData";
private static final String METHOD_NAME2="ShowData";
private static final String NAMESPACE="http://tempuri.org";
private static final String URL="http://198.100.100.101/SampleService/Service.asmx";
//呼叫没有参数的 web service
private String CallNoneParamService(){
String result="";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport aht=new AndroidHttpTransport(URL);
aht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
result = response.toString();
}catch(Exception er){
result=er.toString();
}
return result;
}
//呼叫有参数的Web Service
private String CallParamService(){
String result="";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
request.addProperty("strValue", "TEST");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport aht=new AndroidHttpTransport(URL);
aht.call(SOAP_ACTION2, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
result = response.toString();
}catch(Exception er){
result=er.toString();
}
return result;
}
我由ShowData这个Web Service Method所下的记录﹐确实可以知道这个Service有被呼叫﹐而回传值是"strValue is null"﹐可见呼叫时传递的参数有问题﹐不知道是什么原因。希望能有人可以解惑。
[WebMethod]
public string HelloWorld() {
string str = "Android Call me";
return str;
}
//有参数的
[WebMethod]
public string ShowData(string strValue) {
WriteLog("to Call and str= "+strValue);
if (strValue == null) {
strValue = "strValue is null";
} else if (strValue == "") {
strValue = "str is space";
}
return strValue;
}
在android中的code如下 private static final String SOAP_ACTION="http://tempuri.org/HelloWorld";
private static final String METHOD_NAME="HelloWorld";
private static final String SOAP_ACTION2="http://tempuri.org/ShowData";
private static final String METHOD_NAME2="ShowData";
private static final String NAMESPACE="http://tempuri.org";
private static final String URL="http://198.100.100.101/SampleService/Service.asmx";
//呼叫没有参数的 web service
private String CallNoneParamService(){
String result="";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport aht=new AndroidHttpTransport(URL);
aht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
result = response.toString();
}catch(Exception er){
result=er.toString();
}
return result;
}
//呼叫有参数的Web Service
private String CallParamService(){
String result="";
try{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
request.addProperty("strValue", "TEST");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport aht=new AndroidHttpTransport(URL);
aht.call(SOAP_ACTION2, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
result = response.toString();
}catch(Exception er){
result=er.toString();
}
return result;
}
我由ShowData这个Web Service Method所下的记录﹐确实可以知道这个Service有被呼叫﹐而回传值是"strValue is null"﹐可见呼叫时传递的参数有问题﹐不知道是什么原因。希望能有人可以解惑。
不论我怎么试, 没有错误讯息, webservice也有被呼叫,但参数就是传不进去, 有没有人有什么经验可以分享一下吗?
期望对你有帮助
目前傳遞string已無問題, 對於較複雜的型態可以再進一步來測試了。
我也是菜鸟,对soap原理不甚了解,我是这么解决这个问题的,有高人解释下为什么吗?我想NAMESPACE虽然看起来是个Url,但毕竟是个命名空间,应该是必须相同的。那么为什么只会影响参数的传递呢?
Content-Type: text/xml; charset=utf-8
Content-Length: length<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetFullPositionNameListResponse xmlns="http://abc.cc">
<GetFullPositionNameListResult>
<UserPosition>
<PositionName>string</PositionName>
<PositionID>string</PositionID>
</UserPosition>
<UserPosition>
<PositionName>string</PositionName>
<PositionID>string</PositionID>
</UserPosition>
</GetFullPositionNameListResult>
</GetFullPositionNameListResponse>
</soap:Body>
</soap:Envelope>比如返回这样的结构。如何取得到值呢。
大家可以先用浏览器访问一下你的webService , 那里会有所需参数的名称,记住,一个字母都不能错!!!
改成[WebService(Namespace = "http://tempuri.org")] 去掉最后/就好了