你的位置:首页 > 操作系统

[操作系统]在Android中使用Android Ksoap2调用WebService


一、WebService介绍

  WebService是基于SOAP协议可实现web服务器与web服务器之间的通信,因采用SOAP协议传送

二、Android ksoap2组件

  对于Android端调用WebService,有两种方式,一种自己编写代码主要通过URL获得 HttpUrlConnection的方式建立与webservice的连接,然后进行I/O读写传送和获得数据,并对获得数据进行

  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.web//web服务的命名空间String namespace=” http://Web//请求服务的方法名称String methodName=”getMobileCodeInfo”;//soap请求地址String soapActionAddress = "http://Web

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

HttpTransportSE transport = new HttpTransportSE(url);

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

SoapObject soapObject = new SoapObject(namespace,methodName);soapObject.addProperty(name,value);//添加数据…

4)创建SoapSerializationEnvelope对象,指定

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查询手机号码的归属地

 执行效果如下:

 

完整代码实现:

public class MainActivity extends Activity {  ///手机归属地Webservice的参数信息  private static final String nameSpaceAddress = "http://Web;private static final String urlAddress = "http://webservice.web;  private static final String methodNameAddress = "getMobileCodeInfo";  private static final String soapActionAddress = "http://Web;  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.web

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

http://www.web

IP地址来:

http://www.web

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

http://www.web

火车时刻表

http://www.web

航班查询服务

http://webservice.web

中国股票行情数据 WEB 服务

http://www.web

中国电视节目预告

http://www.web