经常有网友问:“在Android平台如何调用WebService”?经过沟通我发现,甚至有些朋友连什么是WebSerivce都不知道就在问怎么使用,更别说和WebService有关的SOAP、WSDL这类“火星”名词了。所以,我就想在讲解Android平台如何调用WebSerivce之前,先来介绍下WebService,看看它到底有多神秘。
记得我的硕士论文题目中就包含“Web Service”这个词,当时还是花了大量时间去研究Web Service在系统集成、企业应用整合方面的应用;在工作中,接触的几个项目全都用到了Web Service;现在在工作之余抽点时间学习Android,又是Web Service。看来Web Service真是无处不在,有编程语言的地方,总能找到它的身影(提示:如果你以前没接触过WebService,这里需要知道WebService并不是Android的专利,10几年前就已经出现了)。
根据W3C的定义,Web Services(Web服务)是一个用于支持网络间不同机器互操作的软件系统,它是一种自包含、自描述和模块化的应用程序,它可以在网络中被描述、发布和调用,可以将它看作是基于网络的、分布式的模块化组件。
Web Services是建立在通用协议的基础之上,如HTTP、SOAP、UDDI、WSDL等,这些协议在操作系统、编程语言和对象模型的选择上没有任何倾向,因此有着很强的生命力。
Web Services的优势在于提供了不同应用程序平台之间的互操作,它使得基于组件的开发和Web相结合的效果达到最佳。它是基于HTTP协议的,调用请求和回应消息都可以穿过防火墙,不需要更改防火墙的设置,这样就避免了使用特殊端口进行通信时无法穿越防火墙的问题。
简单的理解:通常我们所说的WebService都是远程的某个服务器对外公开了某种服务,或者理解为对外公开了某个功能或者方法,而我们可以通过编程来调用该服务以获得我们需要的信息。例如:www.webxml.com.cn对外公开了手机号码归属地查询服务,我们只需要在调用该服务时传入一个手机号段(号码),就能立即获取该号段的归属地信息。
更通俗的理解:通过使用WebService,我们能够像调用本地方法一样去调用远程服务器上的方法。我们并不需要关心远程的那个方法是Java写的,还是PHP或C#写的;我们并不需要关心远程的方法是基于Unix平台,还是Windows平台,也就是说WebService与平台和语言无关。
说到WebSerivce,就必须要知道SOAP和WSDL,它们到底和WebSerice有着怎么的关系?上面已经提到,Web Services是建立在HTTP、SOAP、WSDL等通用协议的基础之上。
SOAP(Simple Object Access Protocol,简单对象访问协议)是一种轻量级的、简单的、基于XML的协议,被设计用于在分布式环境中交换格式化和固化信息的简单协议。也就是说,要进行通信,进行数据访问传输,就必须依赖于一定的协议,而SOAP正是WebService通信中所依赖的一种协议。目前经常使用的SOAP协议有两个版本:SOAP 1.1 和 SOAP 1.2。
WSDL(Web Services Description Language,即Web服务描述语言)是一种用来描述Web服务的XML语言,它描述了Web服务的功能、接口、参数、返回值等,便于用户绑定和调用服务。它以一种和具体语言无关的方式定义了给定Web服务调用和应答的相关操作和消息。
WSDL是我们能够实实在在看到的东西,它是一份xml文档,用于描述某个WebSerivce的方方面面。例如,上面曾提到www.webxml.com.cn网站提供了手机号码归属地查询的WebSerivce,我们怎么来使用这个WebSerivce呢?它是基于哪个版本的SOAP协议?调用它需要传入什么参数?它会返回什么值?是一个字符串还是xml文档?这一系列的问题都能在WSDL中找到答案。上面这个服务的WSDL地址是:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl,在浏览器上访问它,你将会看到如下所示的xml文档:
以下是C#中开发android:
效果图:
(注:界面简陋凑着看,部分手机号抹去)
详细实现:如下
1. 创建Android项目,不细说了
2. 添加web引用
3. 打开资源文件Resources——Layout——Main.axml
添加控件
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<EditText
android:id="@+id/txttel"
android:layout_width="166.0dp"
android:layout_height="40.0dp"
android:phoneNumber="true"
android:gravity="top"
android:hint="请输入手机号" />
<Button
android:text="查询"
android:layout_width="162.7dp"
android:layout_height="wrap_content"
android:id="@+id/btncx"
android:layout_marginRight="0.0dp" />
<TextView
android:text=""
android:layout_width="284.7dp"
android:layout_height="83.3dp"
android:id="@+id/txtjg" />
</LinearLayout>
4. 打开Activity1.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Atel
{
[Activity(Label = "Atel", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.btncx);
EditText txttel = FindViewById<EditText>(Resource.Id.txttel);
TextView tjg = FindViewById<TextView>(Resource.Id.txtjg);
button.Click += delegate {
//button.Text = string.Format("{0} clicks!", count++);
try
{
cn.com.webxml.webservice.MobileCodeWS motel = new cn.com.webxml.webservice.MobileCodeWS();
string strs = motel.getMobileCodeInfo(txttel.Text.ToString(), "");
tjg.Text = strs;
}
catch (Exception ex)
{
}
};
}
}
}
运行模拟器,看看效果吧
--资料--------------------
其他 火车查询的接口 http://my.oschina.net/simaben/blog/109806