我使用ksoap2套件要呼叫.Net Webservice﹐由网络上找来的范例﹐如果呼叫的是没有参数的webservice是可以﹐但如果是有参数的webservice却是参数一直无法正确的传递。
我的.net webservice如下
- C# code
//没有参数的 [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如下
- Java 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"﹐可见呼叫时传递的参数有问题﹐不知道是什么原因。希望能有人可以解惑。
------解决方案--------------------
http://blog.csdn.net/kylixlu/archive/2010/03/12/5372846.aspx看下这个博客
期望对你有帮助