在Android中使用Android Ksoap2调用WebService

时间:2023-03-09 16:12:19
在Android中使用Android Ksoap2调用WebService
一、WebService介绍

  WebService是基于SOAP协议可实现web服务器与web服务器之间的通信,因采用SOAP协议传送XML数据具有平台无关性,也是成为解决异构平台之间通信的重要解决方案,比如Java平台与.net平台之间。因此在web应用中有着举足轻重的作用,很多机构、组织都在各自平台上对外发布了WebService(例如:天气预报、航班信息、股市行情等等),这样任何平台和客户都可以享受到这些服务,当然有些是要付费的。

二、Android ksoap2组件

  对于Android端调用WebService,有两种方式,一种自己编写代码主要通过URL获得 HttpUrlConnection的方式建立与webservice的连接,然后进行I/O读写传送和获得数据,并对获得数据进行XML解析,比较麻烦。另一种就是使用第三方组件,比较常用的就是ksoap2-android。

  ksoap2-android这个开源组件针对Android平台提供了一个轻量级和高效的SOAP类库,可方便实现Android端与WebService之间的通信

1、环境搭建

  ksoap2-android项目的地址:http://code.google.com/p/ksoap2-android/ 大家可以下载最新版本jar,然后将jar加入到项目中即可。

  我这里使用是ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

2、Ksoap2 使用的主要步骤

  1)web服务参数准备

// webservice服务地址

String url= “http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx”;

//web服务的命名空间

String namespace=” http://WebXml.com.cn/”;

//请求服务的方法名称

String methodName=”getMobileCodeInfo”;

//soap请求地址

String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo";

  2)创建HttpTransportSE,该组件可发送请求

HttpTransportSE transport = new HttpTransportSE(url);

  3)创建SoapObject,添加要传送的数据(信息载体)

SoapObject soapObject = new SoapObject(namespace,methodName);

soapObject.addProperty(name,value);//添加数据

…

4)创建SoapSerializationEnvelope对象,指定xml版本,以及request中body

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut = soapObject;

envelope.setOutputSoapObject(soapObject);

5)发送请求,调用webserivce中的方法

httpTransportSE.call(soapActionAddress, envelope);//服务传回的信息,会放在envelope的bodyIn属性中

6) 获取服务传回的数据

SoapObject object = (SoapObject) envelope.bodyIn;
三、实现案例——通过调用webservice查询手机号码的归属地

 执行效果如下:

在Android中使用Android Ksoap2调用WebService

完整代码实现:

public class MainActivity extends Activity {
///手机归属地Webservice的参数信息
private static final String nameSpaceAddress = "http://WebXml.com.cn/";
private static final String urlAddress
= "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
private static final String methodNameAddress = "getMobileCodeInfo";
private static final String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo";
private TextView telAddress = null;
private EditText tel = null;
private Button btnAddress = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAddress = (Button) this.findViewById(R.id.btnSearchAddress);
telAddress = (TextView) this.findViewById(R.id.telAddress);
tel = (EditText) this.findViewById(R.id.telNo);
btnAddress.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
getTelAddress();
}
}).start(); }
});
/**
* 请求WebService并获得返回的手机号码归属地信息
*/
public void getTelAddress() {
SoapObject soapObject = new
SoapObject(nameSpaceAddress, methodNameAddress);//创建SOAP对象
//设置属性,这些属性值通过SOAP协议传送给服务器
soapObject.addProperty("mobileCode", tel.getText().toString());//要查询的电话号码
soapObject.addProperty("userId", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(urlAddress);
try {
//调用服务
httpTransportSE.call(soapActionAddress, envelope);
} catch (Exception e) {
e.printStackTrace();
}
//获取服务传回的数据,手机归属地信息
SoapObject object = (SoapObject) envelope.bodyIn;
txtAddress = object.getProperty(0).toString();
//向主线程发送消息成功,getTelAddress函数执行完毕
handlerAddress.sendEmptyMessage(0); }
Handler handlerAddress = new Handler() {
public void handleMessage(Message msg) {
telAddress.setText(txtAddress);
Toast.makeText(MainActivity.this,
"获取号码归属地成功"+txtAddress, Toast.LENGTH_LONG).show();
}
};
}
四、附:常见的WebService服务URL

手机归属地服务

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

天气预报Web服务,数据来源于中国气象局

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

IP地址来:

http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx

中文 <-> 英文双向翻译 WEB 服务:

http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx

火车时刻表

http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx

航班查询服务

http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx

中国股票行情数据 WEB 服务

http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx

中国电视节目预告

http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。