现在的位置: 首页 > 综合 > 正文

Android平台调用WebService详解

2013年11月22日 ⁄ 综合 ⁄ 共 4529字 ⁄ 字号 评论关闭

 上篇文章已经对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      

[html] view
plain
copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:paddingTop="5dip"  
  7.     android:paddingLeft="5dip"  
  8.     android:paddingRight="5dip"  
  9.     >  
  10.     <TextView  
  11.         android:layout_width="fill_parent"   
  12.         android:layout_height="wrap_content"   
  13.         android:text="手机号码(段):"  
  14.     />  
  15.     <EditText android:id="@+id/phone_sec"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:inputType="textPhonetic"  
  19.         android:singleLine="true"  
  20.         android:hint="例如:1398547"  
  21.     />  
  22.     <Button android:id="@+id/query_btn"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_gravity="right"  
  26.         android:text="查询"  
  27.     />  
  28.     <TextView android:id="@+id/result_text"  
  29.         android:layout_width="wrap_content"   
  30.         android:layout_height="wrap_content"   
  31.         android:layout_gravity="center_horizontal|center_vertical"  
  32.     />  
  33. </LinearLayout>  

3)编写MainActivity类

[java] view
plain
copy

  1. package com.liufeng.ws.activity;  
  2.   
  3. import org.ksoap2.SoapEnvelope;  
  4. import org.ksoap2.serialization.SoapObject;  
  5. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  6. import org.ksoap2.transport.HttpTransportSE;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. import android.widget.TextView;  
  15.   
  16. /** 
  17.  * Android平台调用WebService(手机号码归属地查询) 
  18.  *  
  19.  * @author liufeng 
  20.  * @date 2011-05-18 
  21.  */  
  22. public class MainActivity extends Activity {  
  23.     private EditText phoneSecEditText;  
  24.     private TextView resultView;  
  25.     private Button queryButton;  
  26.   
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.   
  32.         phoneSecEditText = (EditText) findViewById(R.id.phone_sec);  
  33.         resultView = (TextView) findViewById(R.id.result_text);  
  34.         queryButton = (Button) findViewById(R.id.query_btn);  
  35.   
  36.         queryButton.setOnClickListener(new OnClickListener() {  
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 // 手机号码(段)  
  40.                 String phoneSec = phoneSecEditText.getText().toString().trim();  
  41.                 // 简单判断用户输入的手机号码(段)是否合法  
  42.                 if ("".equals(phoneSec) || phoneSec.length() < 7) {  
  43.                     // 给出错误提示  
  44.                     phoneSecEditText.setError("您输入的手机号码(段)有误!");  
  45.                     phoneSecEditText.requestFocus();  
  46.                     // 将显示查询结果的TextView清空  
  47.                     resultView.setText("");  
  48.                     return;  
  49.                 }  
  50.                 // 查询手机号码(段)信息  
  51.                 getRemoteInfo(phoneSec);  

抱歉!评论已关闭.