上篇文章已经对Web Service及其相关知识进行了介绍(Android开发之WebService介绍 ),相信有的朋友已经忍耐不住想试试在Android应用中调用Web Service。本文将通过一个简单的示例讲解和演示Android平台的Web
Service开发。
Ksoap2-android简介
在Android平台调用Web Service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。认真读完对ksoap2的介绍你会发现并没有提及它应用于Android平台开发,没错,在Android平台中我们并不会直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一个高效、轻量级的SOAP开发包,等同于Android平台上的KSoap2的移植版本。
Ksoap2-android jar包下载
ksoap2 android当前的最新版本为2.5.4,名为ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar,它的下载地址是:http://code.google.com/p/ksoap2-android/,进入页面后,点击“Downloads”标签页,如下图所示:
在“Downloads”页面的下方,找到如下图所示的紫色的链接,然后在链接上点击右键,找到相关下载项进行下载即可。右键菜单中显示的下载项依据浏览器的不同而有所区别,比如我使用的360浏览器,在键接上点击右键,然后选择“使用360安全浏览器下载”即可弹出下载保存对话框。
Android平台调用Web Service示例
下面将通过一个示例讲解如何在Android平台调用Web Service。既然要调用Web Service,那就要先有Web Service。我们还是选择使用上篇文章中介绍的查询手机号码归属地的Web service,它的WSDL为http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl。
1)新建Android工程,引入上面下载的ksoap2-android类库
Android工程的创建就不多说了,主要想说明的是如何向Android工程中添加第三方jar包。当然,添加第3方jar的方式有多种,我个人比较喜欢用下面这种方式,即先将第三方jar包拷贝到工程某个目录下,再将其加入到工程的Build Path中。
例如,我创建的Android工程名为WSClient,在工程名上点击右键,新建一个Folder(目录或文件夹),名为libs,然后将ksoap2-android类库拷贝到libs目录中,如下图所示:
接着,在jar包ksoap2-android-assembly-2.4-jar-with-dependencies.jar上点击右键,依次选择“Build Path”-“Add to Build Path”。再在工程名上点击右键,依次选择“Build Path”-“Config Build Path...”,将看到如下所示界面:
选中ksoap2 jar包前面的选项框,点击OK,则完成了ksoap2 jar包的添加(说明:在Android工程中,添加其它jar包的方法完全一样,操作一两遍后,你会发现其实很简单的)。
2)编写布局文件res/layout/main.xml
- <?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:paddingTop="5dip"
- android:paddingLeft="5dip"
- android:paddingRight="5dip"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="手机号码(段):"
- />
- <EditText android:id="@+id/phone_sec"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="textPhonetic"
- android:singleLine="true"
- android:hint="例如:1398547"
- />
- <Button android:id="@+id/query_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:text="查询"
- />
- <TextView android:id="@+id/result_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal|center_vertical"
- />
- </LinearLayout>
3)编写MainActivity类
- package com.liufeng.ws.activity;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransportSE;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- /**
- * Android平台调用WebService(手机号码归属地查询)
- *
- * @author liufeng
- * @date 2011-05-18
- */
- public class MainActivity extends Activity {
- private EditText phoneSecEditText;
- private TextView resultView;
- private Button queryButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- phoneSecEditText = (EditText) findViewById(R.id.phone_sec);
- resultView = (TextView) findViewById(R.id.result_text);
- queryButton = (Button) findViewById(R.id.query_btn);
- queryButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 手机号码(段)
- String phoneSec = phoneSecEditText.getText().toString().trim();
- // 简单判断用户输入的手机号码(段)是否合法
- if ("".equals(phoneSec) || phoneSec.length() < 7) {
- // 给出错误提示
- phoneSecEditText.setError("您输入的手机号码(段)有误!");
- phoneSecEditText.requestFocus();
- // 将显示查询结果的TextView清空
- resultView.setText("");
- return;
- }
- // 查询手机号码(段)信息
- getRemoteInfo(phoneSec);