调用 webservice分为七个步骤:
第一:实例化SoapObject 对象,指定 webService 的命名空间
String nameSpace = "http://tempuri.org/" ; //空间名,可修改
String methodName = "Login" ; //需调用 webservice 名称
SoapObject request = new SoapObject(nameSpace,methodName);
第二步:假设方法有参数的话, 设置调用方法参数 request.addProperty(" 参数名称 "," 参数值 ");
HashMap
params.put( "userID" , editUid); //加入参数
params.put( "passWord" , editPwd);
if (params != null && !params.isEmpty()) {
for ( Iterator it = params.entrySet().iterator(); it.hasNext();){
Map.Entry e = ( Entry ) it.next();
request.addProperty(e.getKey().toString(),e.getValue());
}
}
第三步:设置SOAP 请求信息 ( 参数部分为 SOAP 协议版本号,与你要调用的 webService 中版本号一致 ):
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope. VER11 );
envelope . dotNet = true ; //.net 支持
envelope. bodyOut =request;
第四步:注册Envelope, (new MarshalBase64()).register(envelope) ;
第五步:构建传输对象,并指明WSDL 文档 URL
//url:WebService的地址
String url= "http://192.168.1.105/AndroidService/Service.asmx" ;
AndroidHttpTransport androidHttpTrandsport=
new AndroidHttpTransport(url) ;
androidHttpTrandsport .debug= true ;
第六步:调用WebService( 其中参数为 1 :命名空间 + 方法名称, 2 : Envelope 对象 ):
String SOAP_ACTION = nameSpace + methodName;
androidHttpTrandsport.call(SOAP_ACTION, envelope);
第七步:解析返回数据 :
String response = "" ;
Object temp=envelope. getResult () ;
response=temp.toString();
return response;
输入用户名密码,点击登录后返回 Hello World,在LogCat下输出HelloWorld信息
界面布局见源码中的main.xml