kSOAP调用Web Service介绍:
SoapObject,一个高度抽象化的类,让无线设备完成SOAP调用。可以调用它的addProperty方法填写要调用的Web Service方法的参数。如下面代码所示:
SoapObject soap = new SoapObject(serviceNamespace,methodName);
SoapObject构造函数的两个参数的意思分别是:
serviceNamespace – Web Service的命名空间,严格和wsdl文件里一致
methodName – 要调用的方法名。
然后,按照Web Service方法参数的顺序,依次调用
soap.addProperty( "username","user" );
soap.addProperty( "password","pass" );
一般来说,对于仅仅是String的返回值,还用不着ClassMap。
接下来就要声明
HttpTransport tx = new HttpTransport(serviceUrl,soapAction );
这是一个强大的helper类,来完成Http-call transport process,它封装了network的一切,你完全不用考虑序列化消息。方法HttpTransport.call()自己就能够发送请求给服务器、接收服务器响应并序列化SOAP消息,如下所示:
Object Response = tx.call(request);
客户端的MIDlet的按键事件函数这么写,即可:
?
-
import ?org.ksoap.SoapObject;
-
import ?org.ksoap.transport.HttpTransport;
-
-
public ? class ?KSoapProcess?{
-
???? public ?KSoapProcess()?{
-
????}
-
-
????
-
-
-
-
???? public ?String?process()?{
-
????????String?result?=? null ;
-
???????? try ?{
-
-
????????????SoapObject?soap?=? new ?SoapObject( "http://tempuri.org/" ,
-
???????????????????? "GetDeptList" );
-
-
????????????HttpTransport?ht?=? new ?HttpTransport(
-
???????????????????? "http://localhost:88/LawWebService/Service.asmx" ,
-
???????????????????? "http://tempuri.org/GetDeptList" );
-
????????????ht.debug?=? true ;
-
-
????????????result?=?ht.call(soap).toString();
-
-
????????}? catch ?(Exception?e)?{
-
????????????e.printStackTrace();
-
????????}
-
???????? return ?result;
-
????}
-
}