Android 通過WebService調用天氣預報接口


轉自:http://blog.csdn.net/xiaanming/article/details/16871117

轉帖請註明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),請尊重他人的辛勤勞動成果,謝謝!

相信大家在平常的開發中,對網絡的操作用到HTTP協議比較多,通過我們使用Get或者Post的方法調用一個數據接口,然後服務器給我們返回JSON格式的數據,我們解析JSON數據然後展現給用戶,相信很多人很喜歡服務器給我們返回JSON數據格式,因爲他解析方便,也有一些JSON的解析庫,例如Google提供的GSON,阿里巴巴的FastJson,不過還是推薦大家使用FastJson來解析,我自己開發中也是用FastJson來解析,FastJson的介紹http://code.alibabatech.com/wiki/display/FastJSON/Home,不過有時候我們用到WebService接口來獲取數據, WebService是一種基於SOAP協議的遠程調用標準,通過webservice可以將不同操作系統平臺、不同語言、不同技術整合到一塊。在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對於Android系統過於龐大,也未必很容易移植到Android系統中。因此,這些開發包並不是在我們的考慮範圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2進行下載,將jar包加入到libs目錄下就行了,接下來帶大家來調用WebService接口

首先我們新建一個工程,取名WebServiceDemo,我們從http://www.webxml.com.cn/zh_cn/web_services.aspx來獲取WebService接口,這裏面有一些免費的WebService接口,我們就用裏面的天氣接口吧http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

我們新建一個WebService的工具類,用於對WebService接口的調用,以後遇到調用WebService直接拷貝來用就行了

  1. package com.example.webservicedemo;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8. import org.ksoap2.SoapEnvelope;
  9. import org.ksoap2.serialization.SoapObject;
  10. import org.ksoap2.serialization.SoapSerializationEnvelope;
  11. import org.ksoap2.transport.HttpResponseException;
  12. import org.ksoap2.transport.HttpTransportSE;
  13. import org.xmlpull.v1.XmlPullParserException;
  14. import android.os.Handler;
  15. import android.os.Message;
  16. /**
  17. * 訪問WebService的工具類,
  18. *
  19. * @see http://blog.csdn.net/xiaanming
  20. *
  21. * @author xiaanming
  22. *
  23. */
  24. public class WebServiceUtils {
  25. public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
  26. // 含有3個線程的線程池
  27. private static final ExecutorService executorService = Executors
  28. .newFixedThreadPool(3);
  29. // 命名空間
  30. private static final String NAMESPACE = "http://WebXml.com.cn/";
  31. /**
  32. *
  33. * @param url
  34. * WebService服務器地址
  35. * @param methodName
  36. * WebService的調用方法名
  37. * @param properties
  38. * WebService的參數
  39. * @param webServiceCallBack
  40. * 回調接口
  41. */
  42. public static void callWebService(String url, final String methodName,
  43. HashMap<String, String> properties,
  44. final WebServiceCallBack webServiceCallBack) {
  45. // 創建HttpTransportSE對象,傳遞WebService服務器地址
  46. final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
  47. // 創建SoapObject對象
  48. SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
  49. // SoapObject添加參數
  50. if (properties != null) {
  51. for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
  52. .iterator(); it.hasNext();) {
  53. Map.Entry<String, String> entry = it.next();
  54. soapObject.addProperty(entry.getKey(), entry.getValue());
  55. }
  56. }
  57. // 實例化SoapSerializationEnvelope,傳入WebService的SOAP協議的版本號
  58. final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
  59. SoapEnvelope.VER11);
  60. // 設置是否調用的是.Net開發的WebService
  61. soapEnvelope.setOutputSoapObject(soapObject);
  62. soapEnvelope.dotNet = true;
  63. httpTransportSE.debug = true;
  64. // 用於子線程與主線程通信的Handler
  65. final Handler mHandler = new Handler() {
  66. @Override
  67. public void handleMessage(Message msg) {
  68. super.handleMessage(msg);
  69. // 將返回值回調到callBack的參數中
  70. webServiceCallBack.callBack((SoapObject) msg.obj);
  71. }
  72. };
  73. // 開啓線程去訪問WebService
  74. executorService.submit(new Runnable() {
  75. @Override
  76. public void run() {
  77. SoapObject resultSoapObject = null;
  78. try {
  79. httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
  80. if (soapEnvelope.getResponse() != null) {
  81. // 獲取服務器響應返回的SoapObject
  82. resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
  83. }
  84. } catch (HttpResponseException e) {
  85. e.printStackTrace();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. } catch (XmlPullParserException e) {
  89. e.printStackTrace();
  90. } finally {
  91. // 將獲取的消息利用Handler發送到主線程
  92. mHandler.sendMessage(mHandler.obtainMessage(0,
  93. resultSoapObject));
  94. }
  95. }
  96. });
  97. }
  98. /**
  99. *
  100. *
  101. * @author xiaanming
  102. *
  103. */
  104. public interface WebServiceCallBack {
  105. public void callBack(SoapObject result);
  106. }
  107. }

我們通過調用裏面的callWebService(String url, final String methodName,HashMap<String, String> properties,final WebServiceCallBack webServiceCallBack)就可以來獲取我們想要的數據,現在講解下里面的實現思路

  • 創建HttpTransportsSE對象。通過HttpTransportsSE類的構造方法可以指定WebService的WSDL文檔的URL
  • 創建SoapObject對象,裏面的參數分別是WebService的命名空間和調用方法名
  • 設置調用方法的參數值,如果沒有參數,就不設置,有參數的話調用SoapObject對象的addProperty(String name, Object value)方法將參數加入到SoapObject對象中
  • 實例化SoapSerializationEnvelope,傳入WebService的SOAP協議的版本號,將上面的SoapObject對象通過setOutputSoapObject(Object soapObject)設置到裏面,並設置是否調用的是.Net開發的WebService和是否debug等信息
  • 因爲涉及到網絡操作,所以我們使用了線程池來異步操作調用WebService接口,我們在線程中調用HttpTransportsSE對象的call(String soapAction, SoapEnvelope envelope)方法就能實現對WebService的調用,並且通過soapEnvelope.bodyIn獲取WebService返回的信息,但是返回的信息是在子線程中,我們需要利用Handler來實現子線程與主線程進行轉換,然後在Handler的handleMessage(Message msg)中將結果回調到callBack的參數中,總體思路就是這個樣子,接下來我們來使用這個工具類吧

我們先用一個ListView來顯示所有的省份,然後點擊每個省進去到市。市也用一個ListView來顯示,最後點擊市用TextView來顯示獲取的WebService天氣情況,思路很簡單

用來顯示省份的佈局,裏面只有一個ListView

  1. <RelativeLayout 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. <ListView
  6. android:id="@+id/province_list"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:cacheColorHint="@android:color/transparent"
  10. android:fadingEdge="none" >
  11. </ListView>
  12. </RelativeLayout>
接下來就是Activity的代碼,先用工具類調用WebService方法,然後在回調方法callBack(SoapObject result)中解析數據到一個List<String>中,在設置ListView的適配器

  1. package com.example.webservicedemo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.ksoap2.serialization.SoapObject;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13. import android.widget.Toast;
  14. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
  15. /**
  16. * 顯示天氣省份的Activity
  17. *
  18. * @see http://blog.csdn.net/xiaanming
  19. *
  20. * @author xiaanming
  21. *
  22. */
  23. public class MainActivity extends Activity {
  24. private List<String> provinceList = new ArrayList<String>();
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. init();
  30. }
  31. private void init() {
  32. final ListView mProvinceList = (ListView) findViewById(R.id.province_list);
  33. //顯示進度條
  34. ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
  35. //通過工具類調用WebService接口
  36. WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportProvince", null, new WebServiceCallBack() {
  37. //WebService接口返回的數據回調到這個方法中
  38. @Override
  39. public void callBack(SoapObject result) {
  40. //關閉進度條
  41. ProgressDialogUtils.dismissProgressDialog();
  42. if(result != null){
  43. provinceList = parseSoapObject(result);
  44. mProvinceList.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provinceList));
  45. }else{
  46. Toast.makeText(MainActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. });
  50. mProvinceList.setOnItemClickListener(new OnItemClickListener() {
  51. @Override
  52. public void onItemClick(AdapterView<?> parent, View view,
  53. int position, long id) {
  54. Intent intent = new Intent(MainActivity.this, CityActivity.class);
  55. intent.putExtra("province", provinceList.get(position));
  56. startActivity(intent);
  57. }
  58. });
  59. }
  60. /**
  61. * 解析SoapObject對象
  62. * @param result
  63. * @return
  64. */
  65. private List<String> parseSoapObject(SoapObject result){
  66. List<String> list = new ArrayList<String>();
  67. SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportProvinceResult");
  68. if(provinceSoapObject == null) {
  69. return null;
  70. }
  71. for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
  72. list.add(provinceSoapObject.getProperty(i).toString());
  73. }
  74. return list;
  75. }
  76. }
點擊省份進入該省份下面的市。也用一個ListView來顯示市的數據,佈局跟上面一樣,Activity裏面的代碼也差不多相似,我就不過多說明了,直接看代碼

  1. package com.example.webservicedemo;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import org.ksoap2.serialization.SoapObject;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.ListView;
  14. import android.widget.Toast;
  15. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
  16. /**
  17. * 顯示城市的Activity
  18. *
  19. * @see http://blog.csdn.net/xiaanming
  20. *
  21. * @author xiaanming
  22. *
  23. */
  24. public class CityActivity extends Activity {
  25. private List<String> cityStringList;
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. init();
  31. }
  32. private void init() {
  33. final ListView mCityList = (ListView) findViewById(R.id.province_list);
  34. //顯示進度條
  35. ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
  36. //添加參數
  37. HashMap<String, String> properties = new HashMap<String, String>();
  38. properties.put("byProvinceName", getIntent().getStringExtra("province"));
  39. WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportCity", properties, new WebServiceCallBack() {
  40. @Override
  41. public void callBack(SoapObject result) {
  42. ProgressDialogUtils.dismissProgressDialog();
  43. if(result != null){
  44. cityStringList = parseSoapObject(result);
  45. mCityList.setAdapter(new ArrayAdapter<String>(CityActivity.this, android.R.layout.simple_list_item_1, cityStringList));
  46. }else{
  47. Toast.makeText(CityActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
  48. }
  49. }
  50. });
  51. mCityList.setOnItemClickListener(new OnItemClickListener() {
  52. @Override
  53. public void onItemClick(AdapterView<?> parent, View view,
  54. int position, long id) {
  55. Intent intent = new Intent(CityActivity.this, WeatherActivity.class);
  56. intent.putExtra("city", cityStringList.get(position));
  57. startActivity(intent);
  58. }
  59. });
  60. }
  61. /**
  62. * 解析SoapObject對象
  63. * @param result
  64. * @return
  65. */
  66. private List<String> parseSoapObject(SoapObject result){
  67. List<String> list = new ArrayList<String>();
  68. SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportCityResult");
  69. for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
  70. String cityString = provinceSoapObject.getProperty(i).toString();
  71. list.add(cityString.substring(0, cityString.indexOf("(")).trim());
  72. }
  73. return list;
  74. }
  75. }
接下來就是點擊相對應的城市調用WebService接口來獲取該城市下面的天氣詳情啦,爲了簡單起見,我用一個TextView來顯示天氣信息,因爲天氣信息很多,一個屏幕顯示不完,所以我們考慮在外面加一個ScrollView來進行滾動

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <ScrollView
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent" >
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" >
  11. <TextView
  12. android:id="@+id/weather"
  13. android:textColor="#336598"
  14. android:textSize="16sp"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent" />
  17. </LinearLayout>
  18. </ScrollView>
  19. </RelativeLayout>
Activity的代碼就不做過多說明,跟上面的大同小異

  1. package com.example.webservicedemo;
  2. import java.util.HashMap;
  3. import org.ksoap2.serialization.SoapObject;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
  9. /**
  10. * 顯示天氣的Activity
  11. *
  12. * @see http://blog.csdn.net/xiaanming
  13. *
  14. * @author xiaanming
  15. *
  16. */
  17. public class WeatherActivity extends Activity{
  18. @Override
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.weather_layout);
  22. init();
  23. }
  24. private void init() {
  25. final TextView mTextWeather = (TextView) findViewById(R.id.weather);
  26. ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
  27. HashMap<String, String> properties = new HashMap<String, String>();
  28. properties.put("theCityName", getIntent().getStringExtra("city"));
  29. WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getWeatherbyCityName", properties, new WebServiceCallBack() {
  30. @Override
  31. public void callBack(SoapObject result) {
  32. ProgressDialogUtils.dismissProgressDialog();
  33. if(result != null){
  34. SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
  35. StringBuilder sb = new StringBuilder();
  36. for(int i=0; i<detail.getPropertyCount(); i++){
  37. sb.append(detail.getProperty(i)).append("\r\n");
  38. }
  39. mTextWeather.setText(sb.toString());
  40. }else{
  41. Toast.makeText(WeatherActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
  42. }
  43. }
  44. });
  45. }
  46. }
到這裏我們就完成了編碼工作,在運行程序之前我們需要在AndroidManifest.xml註冊Activity,以及添加訪問網絡的權限

  1. <application
  2. android:icon="@drawable/ic_launcher"
  3. android:label="@string/app_name"
  4. android:theme="@style/AppTheme" >
  5. <activity
  6. android:name="com.example.webservicedemo.MainActivity"
  7. android:label="@string/title_activity_main" >
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10. <category android:name="android.intent.category.LAUNCHER" />
  11. </intent-filter>
  12. </activity>
  13. <activity android:name=".CityActivity"/>
  14. <activity android:name=".WeatherActivity"></activity>
  15. </application>
  16. <uses-permission android:name="android.permission.INTERNET"/>
運行結果:



省份,城市列表可以加上A-Z的排序功能,可以參考下Android實現ListView的A-Z字母排序和過濾搜索功能,實現漢字轉成拼音,我這裏就不添加了,需要添加的朋友自行實現,好了,今天的講解到此結束,有疑問的朋友請在下面留言。

項目源碼,點擊下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章