Android使用webservice客户端实例

本文深入解析了使用SOAP协议调用WebService的七个关键步骤,从实例化SoapObject对象开始,到最终解析返回数据结束。通过具体示例,详细说明了如何设置SOAP请求信息、注册Envelope以及构建传输对象等过程,最后展示如何解析返回数据并将其呈现为字符串形式。文章以实际应用为例,指导开发者完成登录操作并输出“Hello World”,提供了一套完整的调用流程和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



 

调用 webservice分为七个步骤:

 

第一:实例化SoapObject  对象,指定 webService 的命名空间

String nameSpace =  "http://tempuri.org/" //空间名,可修改

String methodName =  "Login" //需调用 webservice 名称

SoapObject  request = new  SoapObject(nameSpace,methodName);

 

第二步:假设方法有参数的话, 设置调用方法参数 request.addProperty(" 参数名称 "," 参数值 ");

HashMap <String ,Object> params= new  HashMap<String ,Object>();

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 :命名空间 + 方法名称, 2Envelope 对象 ):

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



 源码见附件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值