Google为Android平台开发Web Service客户端提供了ksoap2-android项目,在这个网址下载开发包http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/3.1.0/ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar

使用 kspoap2-android调用webserice操作的步骤如下:

1、创建HttpTransportSE传输对象 传入webservice服务器地址

  1. final HttpTransportSE httpSE = new HttpTransportSE(SERVER_URL);

2、 创建SoapObject对象,创建该对象时需要传入所要调用Wb Service的命名空间、Web Service方法名;如果有参数要传给Web Service服务器,调用SoapObject对象的addProperty(String name,Object value)方法来设置参数,该方法的name参数指定参数名;value参数指定参数值

  1. SoapObject soapObject = new SoapObject(PACE, M_NAME);
  1. soapObject.addProperty("byProvinceName ", citys);

3、创建SoapSerializationEnelope对象,并传入SOAP协议的版本号;并设置对象的bodyOut属性

  1. final SoapSerializationEnvelope soapserial = new SoapSerializationEnvelope(
  2. SoapEnvelope.VER11);
  3. soapserial.bodyOut = soapObject;
  4. // 设置与.NET提供的Web service保持有良好的兼容性
  5. soapserial.dotNet = true;

6、调用HttpTransportSE对象的call()方法,其中call的第一个参数soapAction,第二个为SoapSerializationEvelope对象 调用远程Web Service;

  1. // 调用HttpTransportSE对象的call方法来调用 webserice
  2. httpSE.call(PACE + M_NAME, soapserial);

7、获取返回的信息,并解析

  1. // 获取服务器响应返回的SOAP消息
  2. SoapObject result = (SoapObject) soapserial.bodyIn;
  3. SoapObject detail = (SoapObject) result.getProperty("getSupportProvinceResult");
  4. //解析返回信息
  5. for (int i = 0; i < detail.getPropertyCount(); i++) {
  6. citys.add(detail.getProperty(i).toString());
  7. }

实例:通过天气预报 Web 服务 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

MainActivity.java

  1. package com.example.webserviceteset;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import android.widget.AdapterView.OnItemSelectedListener;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Spinner;
  13. public class MainActivity extends Activity {
  14. private Spinner city, citys;
  15. private List<String> listcity, listcitys, weater;
  16. // 分别显示近三天的天气信息和城市介绍
  17. private TextView cityNames1, cityNames2, cityNames3, cityjie;
  18. private ImageView weateImage1, weateImage2, weateImage3;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. citys = (Spinner) findViewById(R.id.citys);
  24. city = (Spinner) findViewById(R.id.city);
  25. cityNames1 = (TextView) findViewById(R.id.cityNames1);
  26. cityNames2 = (TextView) findViewById(R.id.cityNames2);
  27. cityNames3 = (TextView) findViewById(R.id.cityNames3);
  28. cityjie = (TextView) findViewById(R.id.cityjie);
  29. weateImage1 = (ImageView) findViewById(R.id.weateImage1);
  30. weateImage2 = (ImageView) findViewById(R.id.weateImage2);
  31. weateImage3 = (ImageView) findViewById(R.id.weateImage3);
  32. // cityNames1=(TextView)findViewById(R.i)
  33. listcitys = WebServiceTest.getCitys();
  34. ArrayAdapter<String> citysAdater = new ArrayAdapter<String>(this,
  35. android.R.layout.simple_list_item_multiple_choice, listcitys);
  36. citys.setAdapter(citysAdater);
  37. listcity = WebServiceTest.getCity(citys.getSelectedItem().toString());
  38. ArrayAdapter<String> cityAdater = new ArrayAdapter<String>(this,
  39. android.R.layout.simple_list_item_multiple_choice, listcity);
  40. city.setAdapter(cityAdater);
  41. citys.setOnItemSelectedListener(new OnItemSelectedListener() {
  42. @Override
  43. public void onItemSelected(AdapterView<?> arg0, View arg1,
  44. int arg2, long arg3) {
  45. listcity = WebServiceTest.getCity(citys.getSelectedItem()
  46. .toString());
  47. ArrayAdapter<String> cityAdater1 = new ArrayAdapter<String>(
  48. MainActivity.this,
  49. android.R.layout.simple_list_item_multiple_choice,
  50. listcity);
  51. city.setAdapter(cityAdater1);
  52. }
  53. @Override
  54. public void onNothingSelected(AdapterView<?> arg0) {
  55. // TODO Auto-generated method stub
  56. }
  57. });
  58. city.setOnItemSelectedListener(new OnItemSelectedListener() {
  59. // 返回数据: 一个一维数组 String(22),共有23个元素。
  60. @Override
  61. public void onItemSelected(AdapterView<?> arg0, View arg1,
  62. int arg2, long arg3) {
  63. weater = WebServiceTest.getWeather(city.getSelectedItem()
  64. .toString());
  65. for (int i = 0; i < weater.size(); i++) {
  66. System.out.println("i=" + i + ":" + weater.get(i));
  67. }
  68. cityNames1.setText(weater.get(6) + "\n" + weater.get(5) + "\n"
  69. + weater.get(7));
  70. cityNames1.setBackgroundResource(ChangeImageView.imageId(weater
  71. .get(8)));
  72. weateImage1.setImageResource(ChangeImageView.imageId(weater
  73. .get(8)));
  74. cityNames2.setText(weater.get(13) + "\n" + weater.get(12) + "\n"
  75. + weater.get(14));
  76. cityNames2.setBackgroundResource(ChangeImageView.imageId(weater
  77. .get(15)));
  78. weateImage2.setImageResource(ChangeImageView.imageId(weater
  79. .get(15)));
  80. cityNames3.setText(weater.get(18) + "\n" + weater.get(17) + "\n"
  81. + weater.get(19));
  82. cityNames3.setBackgroundResource(ChangeImageView.imageId(weater
  83. .get(20)));
  84. weateImage3.setImageResource(ChangeImageView.imageId(weater
  85. .get(21)));
  86. cityjie.setText(weater.get(22));
  87. }
  88. @Override
  89. public void onNothingSelected(AdapterView<?> arg0) {
  90. // TODO Auto-generated method stub
  91. }
  92. });
  93. }
  94. @Override
  95. public boolean onCreateOptionsMenu(Menu menu) {
  96. getMenuInflater().inflate(R.menu.main, menu);
  97. return true;
  98. }
  99. }

WebServiceTest.java

  1. package com.example.webserviceteset;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.Callable;
  5. import java.util.concurrent.ExecutionException;
  6. import java.util.concurrent.FutureTask;
  7. import org.ksoap2.SoapEnvelope;
  8. import org.ksoap2.serialization.SoapObject;
  9. import org.ksoap2.serialization.SoapSerializationEnvelope;
  10. import org.ksoap2.transport.HttpTransportSE;
  11. public class WebServiceTest {
  12. // Webservice服务器地址
  13. private static final String SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
  14. // 调用的webservice命令空间
  15. private static final String PACE = "http://WebXml.com.cn/";
  16. // 获取所有省份的方法名
  17. private static final String M_NAME = "getSupportProvince";
  18. // 获取省份包含的城市的方法名
  19. private static final String MC_NAME = "getSupportCity";
  20. // 获取天气详情的方法名
  21. private static final String W_NAME = "getWeatherbyCityName";
  22. /**
  23. *
  24. * @return 所有省份
  25. */
  26. public static List<String> getCitys() {
  27. // 创建HttpTransportSE传说对象 传入webservice服务器地址
  28. final HttpTransportSE httpSE = new HttpTransportSE(SERVER_URL);
  29. httpSE.debug = true;
  30. // 创建soapObject对象并传入命名空间和方法名
  31. SoapObject soapObject = new SoapObject(PACE, M_NAME);
  32. // 创建SoapSerializationEnvelope对象并传入SOAP协议的版本号
  33. final SoapSerializationEnvelope soapserial = new SoapSerializationEnvelope(
  34. SoapEnvelope.VER11);
  35. soapserial.bodyOut = soapObject;
  36. // 设置与.NET提供的Web service保持有良好的兼容性
  37. soapserial.dotNet = true;
  38. // 使用Callable与Future来创建启动线程
  39. FutureTask<List<String>> future = new FutureTask<List<String>>(
  40. new Callable<List<String>>() {
  41. @Override
  42. public List<String> call() throws Exception {
  43. List<String> citys = new ArrayList<String>();
  44. // 调用HttpTransportSE对象的call方法来调用 webserice
  45. httpSE.call(PACE + M_NAME, soapserial);
  46. if (soapserial.getResponse() != null) {
  47. // 获取服务器响应返回的SOAP消息
  48. SoapObject result = (SoapObject) soapserial.bodyIn;
  49. SoapObject detail = (SoapObject) result
  50. .getProperty("getSupportProvinceResult");
  51. // 解析返回信息
  52. for (int i = 0; i < detail.getPropertyCount(); i++) {
  53. citys.add(detail.getProperty(i).toString());
  54. }
  55. return citys;
  56. }
  57. return null;
  58. }
  59. });
  60. new Thread(future).start();
  61. try {
  62. return future.get();
  63. } catch (InterruptedException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. } catch (ExecutionException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. return null;
  71. }
  72. /**
  73. *
  74. * @param citys
  75. *            省份
  76. * @return 该省下的所有城市
  77. */
  78. public static List<String> getCity(String citys) {
  79. // 创建HttpTransportSE对象
  80. final HttpTransportSE httpSE = new HttpTransportSE(SERVER_URL);
  81. httpSE.debug = true;
  82. // 创建SoapObject对象
  83. SoapObject soapObject = new SoapObject(PACE, MC_NAME);
  84. // 添加参数
  85. soapObject.addProperty("byProvinceName ", citys);
  86. // 创建SoapSerializationEnvelope
  87. final SoapSerializationEnvelope serializa = new SoapSerializationEnvelope(
  88. SoapEnvelope.VER11);
  89. serializa.bodyOut = soapObject;
  90. serializa.dotNet = true;
  91. FutureTask<List<String>> future = new FutureTask<List<String>>(
  92. new Callable<List<String>>() {
  93. @Override
  94. public List<String> call() throws Exception {
  95. List<String> city = new ArrayList<String>();
  96. // 调用Web Service
  97. httpSE.call(PACE + MC_NAME, serializa);
  98. // 获取返回信息
  99. if (serializa.getResponse() != null) {
  100. SoapObject restul = (SoapObject) serializa.bodyIn;
  101. SoapObject detial = (SoapObject) restul
  102. .getProperty("getSupportCityResult");
  103. // 解析返回信息
  104. for (int i = 0; i < detial.getPropertyCount(); i++) {
  105. // 获取城市名
  106. String str = detial.getPropertyAsString(i)
  107. .toString();
  108. String strCity = str.substring(0,
  109. str.indexOf("(") - 1);
  110. city.add(strCity);
  111. }
  112. return city;
  113. }
  114. return null;
  115. }
  116. });
  117. new Thread(future).start();
  118. try {
  119. return future.get();
  120. } catch (InterruptedException e) {
  121. // TODO Auto-generated catch block
  122. e.printStackTrace();
  123. } catch (ExecutionException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. }
  127. return null;
  128. }
  129. // 获取三天之内的天气详情
  130. public static List<String> getWeather(String citys) {
  131. final HttpTransportSE httpSe = new HttpTransportSE(SERVER_URL);
  132. httpSe.debug = true;
  133. SoapObject soapObject = new SoapObject(PACE, W_NAME);
  134. soapObject.addProperty("theCityName", citys);
  135. final SoapSerializationEnvelope serializa = new SoapSerializationEnvelope(
  136. SoapEnvelope.VER11);
  137. serializa.bodyOut = soapObject;
  138. serializa.dotNet = true;
  139. FutureTask<List<String>> future = new FutureTask<List<String>>(
  140. new Callable<List<String>>() {
  141. @Override
  142. public List<String> call() throws Exception {
  143. List<String> list = new ArrayList<String>();
  144. // 调用webservice
  145. httpSe.call(PACE+W_NAME, serializa);
  146. // 获取返回信息
  147. if (serializa.getResponse() != null) {
  148. SoapObject result = (SoapObject) serializa.bodyIn;
  149. SoapObject deialt = (SoapObject) result
  150. .getProperty("getWeatherbyCityNameResult");
  151. // 解析数据
  152. for (int i = 0; i < deialt.getPropertyCount(); i++) {
  153. list.add(deialt.getProperty(i).toString());
  154. }
  155. }
  156. return list;
  157. }
  158. });
  159. new Thread(future).start();
  160. try {
  161. return future.get();
  162. } catch (InterruptedException e) {
  163. // TODO Auto-generated catch block
  164. e.printStackTrace();
  165. } catch (ExecutionException e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. return null;
  170. }
  171. }

ChangeImageView.java

  1. package com.example.webserviceteset;
  2. public class ChangeImageView {
  3. // 由于下载下来的图片分 大中小 三种 而调用远程webserivce获取的值是小的图片名 所以的 根据获取的图片名称来获取向对应的大图片ID
  4. public static int imageId(String ids) {
  5. int id = R.drawable.a_0;
  6. int ided =Integer.parseInt(ids.substring(0, ids.indexOf(".")));
  7. switch (ided) {
  8. case 1:
  9. id = R.drawable.a_1;
  10. break;
  11. case 2:
  12. id = R.drawable.a_2;
  13. break;
  14. case 3:
  15. id = R.drawable.a_3;
  16. break;
  17. case 4:
  18. id = R.drawable.a_4;
  19. break;
  20. case 5:
  21. id = R.drawable.a_5;
  22. break;
  23. case 6:
  24. id = R.drawable.a_6;
  25. break;
  26. case 7:
  27. id = R.drawable.a_7;
  28. break;
  29. case 8:
  30. id = R.drawable.a_8;
  31. break;
  32. case 9:
  33. id = R.drawable.a_9;
  34. break;
  35. case 10:
  36. id = R.drawable.a_10;
  37. break;
  38. case 11:
  39. id = R.drawable.a_11;
  40. break;
  41. case 12:
  42. id = R.drawable.a_12;
  43. break;
  44. case 13:
  45. id = R.drawable.a_13;
  46. break;
  47. case 14:
  48. id = R.drawable.a_1;
  49. break;
  50. case 15:
  51. id = R.drawable.a_15;
  52. break;
  53. case 16:
  54. id = R.drawable.a_16;
  55. break;
  56. case 17:
  57. id = R.drawable.a_17;
  58. break;
  59. case 18:
  60. id = R.drawable.a_18;
  61. break;
  62. case 19:
  63. id = R.drawable.a_19;
  64. break;
  65. case 20:
  66. id = R.drawable.a_20;
  67. break;
  68. case 21:
  69. id = R.drawable.a_21;
  70. break;
  71. case 22:
  72. id = R.drawable.a_22;
  73. break;
  74. case 23:
  75. id = R.drawable.a_23;
  76. break;
  77. case 24:
  78. id = R.drawable.a_24;
  79. break;
  80. case 25:
  81. id = R.drawable.a_25;
  82. break;
  83. case 26:
  84. id = R.drawable.a_26;
  85. break;
  86. case 27:
  87. id = R.drawable.a_27;
  88. break;
  89. case 28:
  90. id = R.drawable.a_28;
  91. break;
  92. case 29:
  93. id = R.drawable.a_29;
  94. break;
  95. case 30:
  96. id = R.drawable.a_30;
  97. break;
  98. }
  99. return id;
  100. }
  101. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity" >
  7. <LinearLayout
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal" >
  11. <Spinner
  12. android:id="@+id/citys"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content" />
  15. <Spinner
  16. android:id="@+id/city"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal" >
  24. <TextView
  25. android:id="@+id/cityNames1"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content" />
  28. <ImageView
  29. android:id="@+id/weateImage1"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content" />
  32. </LinearLayout>
  33. <LinearLayout
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:orientation="horizontal" >
  37. <TextView
  38. android:id="@+id/cityNames2"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content" />
  41. <ImageView
  42. android:id="@+id/weateImage2"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content" />
  45. </LinearLayout>
  46. <LinearLayout
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:orientation="horizontal" >
  50. <TextView
  51. android:id="@+id/cityNames3"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content" />
  54. <ImageView
  55. android:id="@+id/weateImage3"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content" />
  58. </LinearLayout>
  59. <LinearLayout
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:orientation="horizontal" >
  63. <TextView
  64. android:id="@+id/cityjie"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:lines="6" />
  68. </LinearLayout>
  69. </LinearLayout>
  70. AndroidManifest.xmlAndroidManifest.xml

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.webserviceteset"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="11"
  8. android:targetSdkVersion="17" />
  9. <uses-permission
  10. android:name="android.permission.INTERNET"/>
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.example.webserviceteset.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </manifest>

源代码下载的地址:http://files.cnblogs.com/android100/WebServiceTeset.rar

Android之ksoap2-android详解与调用天气预报Webservice完整实例相关推荐

  1. android jar 包 意见反馈功能,android重点jar包详解.docx

    android重点jar包详解 深入理解View(一):从setContentView谈起 我们都知道?MVC,在Android中,这个?V?即指View,那我们今天就来探探View的究竟.在onCr ...

  2. Android应用坐标系统全面详解

    Android应用坐标系统全面详解 原文链接:CSDN@工匠若水,http://blog.csdn.net/yanbober/article/details/50419117 1. 背景 去年有很多人 ...

  3. android ------- 开发者的 RxJava 详解

    在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github:  https://github.com/ReactiveX/RxJava  https://githu ...

  4. 宏锦软件 Android 的 ListView 使用详解

     宏锦软件爱好者在开发Android软件时,对ListView的使用有点陌生,于是翻了许多资料,这里给大家一份比较好的教程,希望有用. 在android开发中ListView是比较常用的组件,它以 ...

  5. Android开发入门一之Android应用程序架构详解

    Android应用程序架构详解如下: src/ java源代码存放目录 gen/自动生成目录 gen 目录中存放所有由Android开发工具自动生成的文件.目录中最重要的就是R.java文件.这个文件 ...

  6. Android 系统(199)---Android事件分发机制详解

    Android事件分发机制详解 前言 Android事件分发机制是Android开发者必须了解的基础 网上有大量关于Android事件分发机制的文章,但存在一些问题:内容不全.思路不清晰.无源码分析. ...

  7. Android JNI作用及其详解

    Android JNI作用及其详解 Java Native Interface (JNI)标准是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 J ...

  8. android调webview的方法,Android中的WebView详解

    Android中的WebView详解 WebView详解 基本用法 布局文件配置WebView android:id="@+id/wv_news_detail" android:l ...

  9. Android 吸入动画效果详解(仿mac退出效果)

    转载自:http://m.blog.csdn.net/blog/leehong2005/9127095 [转]Android 吸入动画效果详解 1,背景 吸入(Inhale)效果,最初我是在iOS上面 ...

最新文章

  1. 2022-2028年中国塑料鞋行业市场发展调研及未来前景规划报告
  2. RecyclerView 滑动显示返回按钮,点击返回到顶部
  3. VMware Server 2.0简单学习!
  4. THU – team project final review score
  5. 使用命令编译为jar包
  6. 231. Power of Two
  7. 学生系统优化(三)- -细节优化
  8. VTK:Utilities之ArrayWriter
  9. graph driver-device mapper-04libdevmapper基本操作
  10. tensorflow中关于vgg16的项目
  11. mediawiki 搭建
  12. Linux多线程开发-线程同步-条件变量pthread_cond_t
  13. linux 浏览器 links,linux下的命令行浏览器links
  14. Java集合之List的equals方法
  15. C 设计语言编译生成的是中间语言IL,一、源代码-面向CLR的编译器-托管模块-(元数据IL代码)...
  16. [Android系列—] 3. 启动另外的活动(Activity)
  17. lisp如何将度分秒转换为弧度_1/16怎么转换成角度(度分秒)??
  18. 管理,就是做减法!聊聊 “奥卡姆剃刀定律”
  19. bitcoin rpc command
  20. python数据分析案例实战——融360客户贷款风险预测(信用卡)

热门文章

  1. ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline - Part II
  2. Python高级爬虫开发,高难度JS解密教程,绝地求生模拟登陆!
  3. 鸿蒙系统手机mate40,鸿蒙系统来了!华为Mate40首批,2年前手机将被淘汰
  4. three.js 贴图只显示颜色_C4D作品“花里胡哨”?我怀疑你贴图方式有问题……
  5. PyCaret-低代码ML库使用指南
  6. android京东秒杀倒计时,js实现京东秒杀倒计时功能
  7. sqlalchemy数据库中的offset偏移查询的使用
  8. 莫队 ---- CF 135D. Jeff and Removing Periods (等差数列预处理 + 莫队)
  9. 李超线段树(Li-Chao Segment Tree)
  10. 差分 ---- Codeforces Round #672 (Div. 2):C2. Pokémon Army (hard version)[差分的思想]