android 调用webService Demo

1、添加网络访问权限在AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

2、加入soap的jia包ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

下载地址http://download.csdn.net/detail/yusewuhen/8423905

3、 创建字段

// 名空间
private static final String NAMESPACE   = "http://WebXml.com.cn/";
// 网址
private static String  URL      = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
// 方法名
private static final String METHOD_NAME = "getWeather";
// SOAPACTION
private static String  SOAP_ACTION = "http://WebXml.com.cn/getWeather";

4、       获取Soap对象并设置传送参数值

SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty("theCityCode", cityName);

5、    创建SoapSerializationEnvelope对象

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
//其中SoapEnvelope.VER11是指SOAP 1.1,

6、创建HttpTransportsSE对象

HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
//对于部分不规范的webService需要使用ht.call(null, envelope);
 ht.call(SOAP_ACTION, envelope);

7、获得WebService方法的返回结果

方法一:

detail =(SoapObject) envelope.getResponse();

方法二:

SoapObject result = (SoapObject)envelope.bodyIn;

detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");

 8、解析SoapObject对象

if(envelope.getResponse()!=null){
SoapObject  detail =(SoapObject) envelope.bodyIn;

for (int i = 0; i < detail.getPropertyCount(); i++) {

     System.out.println("detail.getProperty(" + i + ")" + detail.getProperty(i));

}}

可测试的 webservice服务

http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

页面中有以下信息:

getWeather

获得天气预报数据

输入参数:城市/地区ID或名称,返回数据:一维字符串数组

点击getWeather

http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather

从上述页面可以获取以下信息:

SOAPAction: "http://WebXml.com.cn/getWeather"
<getWeather xmlns="http://WebXml.com.cn/">
其中xmlns就是NAMESPACE值以及传送参数名theCityCode




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值