欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发调用WebService的方法示例

程序员文章站 2023-12-09 19:55:51
本文实例讲述了android开发调用webservice的方法。分享给大家供大家参考,具体如下: webservice是一种基于soap协议的远程调用标准,通过webse...

本文实例讲述了android开发调用webservice的方法。分享给大家供大家参考,具体如下:

webservice是一种基于soap协议的远程调用标准,通过webservice可以将不同操作系统平台、不同语言、不同技术整合到一块。在android sdk中并没有提供调用webservice的库,因此,需要使用第三方的sdk来调用webservice。pc版本的webservice客户端库非常丰富,例如axis2,cxf等,但这些开发包对于android系统过于庞大,也未必很容易移植到android系统中。因此,这些开发包并不是在我们的考虑范围内。适合手机的webservice客户端的sdk有一些,比较常用的有ksoap2,可以从http://code.google.com/p/ksoap2-android/downloads/list进行下载;将下载的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包复制到eclipse工程的lib目录中,当然也可以放在其他的目录里。同时在eclipse工程中引用这个jar包。

具体调用调用webservice的方法为:

(1) 指定webservice的命名空间和调用的方法名,如:

soapobject request =new soapobject(http://service,"getname");

soapobject类的第一个参数表示webservice的命名空间,可以从wsdl文档中找到webservice的命名空间。第二个参数表示要调用的webservice方法名。

(2) 设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:

request.addproperty("param1","value");
request.addproperty("param2","value");

要注意的是,addproperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的webservice类中的方法参数名一致,只要设置参数的顺序一致即可。

(3) 生成调用webservice方法的soap请求信息。该信息由soapserializationenvelope对象描述,代码为:

soapserializationenvelope envelope=new
soapserializationenvelope(soapenvelope.ver11);
envelope.bodyout = request;

创建soapserializationenvelope对象时需要通过soapserializationenvelope类的构造方法设置soap协议的版本号。该版本号需要根据服务端webservice的版本号设置。在创建soapserializationenvelope对象后,不要忘了设置soapsoapserializationenvelope类的bodyout属性,该属性的值就是在第一步创建的soapobject对象。

(4) 创建httptransportsse对象。通过httptransportsse类的构造方法可以指定webservice的wsdl文档的url:

httptransportse ht=new httptransportse("http://192.168.18.17:80
/axis2/service/searchnewsservice?wsdl");

(5)使用call方法调用webservice方法,代码:

ht.call(null,envelope);

call方法的第一个参数一般为null,第2个参数就是在第3步创建的soapserializationenvelope对象。

(6)使用getresponse方法获得webservice方法的返回结果,代码:

soapobject soapobject =( soapobject) envelope.getresponse();

以下为简单的实现一个天气查看功能的例子:

public class webservice extends activity {
private static final string namespace = "http://webxml.com.cn/";
// webservice地址
private static string url = "http://www.webxml.com.cn/
webservices/weatherwebservice.asmx";
private static final string method_name = "getweatherbycityname";
private static string soap_action = "http://webxml.com.cn/
getweatherbycityname";
private string weathertoday;
private button okbutton;
private soapobject detail;
@override
public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 okbutton = (button) findviewbyid(r.id.ok);
 okbutton.setonclicklistener(new button.onclicklistener() {
 public void onclick(view v) {
 showweather();
 }
 });
}
private void showweather() {
 string city = "武汉";
 getweather(city);
}
@suppresswarnings("deprecation")
public void getweather(string cityname) {
try {
 system.out.println("rpc------");
 soapobject rpc = new soapobject(namespace, method_name);
 system.out.println("rpc" + rpc);
 system.out.println("cityname is " + cityname);
 rpc.addproperty("thecityname", cityname);
 androidhttptransport ht = new androidhttptransport(url);
 ht.debug = true;
soapserializationenvelope envelope = new soapserializationenvelope(
 soapenvelope.ver11);
 envelope.bodyout = rpc;
 envelope.dotnet = true;
 envelope.setoutputsoapobject(rpc);
 ht.call(soap_action, envelope);
 soapobject result = (soapobject) envelope.bodyin;
 detail = (soapobject) result
 .getproperty("getweatherbycitynameresult");
 system.out.println("result" + result);
 system.out.println("detail" + detail);
 toast.maketext(webservice.this, detail.tostring(),
 toast.length_long).show();
 parseweather(detail);
 return;
} catch (exception e) {
 e.printstacktrace();
 }
}
private void parseweather(soapobject detail)
 throws unsupportedencodingexception {
 string date = detail.getproperty(6).tostring();
 weathertoday = "今天:" + date.split(" ")[0];
 weathertoday = weathertoday + "\n天气:" + date.split(" ")[1];
 weathertoday = weathertoday + "\n气温:"
 + detail.getproperty(5).tostring();
 weathertoday = weathertoday + "\n风力:"
 + detail.getproperty(7).tostring() + "\n";
 system.out.println("weathertoday is " + weathertoday);
 toast.maketext(webservice.this, weathertoday,
 toast.length_long).show();
 }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android视图view技巧总结》、《android资源操作技巧汇总》、《android操作json格式数据技巧总结》、《android开发入门与进阶教程》、《android编程之activity操作技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。