Android 通过WebService调用天气预报接口

来源:互联网 发布:最好的网络摄像头 编辑:程序博客网 时间:2024/04/27 02:36

 转自: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直接拷贝来用就行了

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.webservicedemo;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5. import java.util.Iterator;  
  6. import java.util.Map;  
  7. import java.util.concurrent.ExecutorService;  
  8. import java.util.concurrent.Executors;  
  9.   
  10. import org.ksoap2.SoapEnvelope;  
  11. import org.ksoap2.serialization.SoapObject;  
  12. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  13. import org.ksoap2.transport.HttpResponseException;  
  14. import org.ksoap2.transport.HttpTransportSE;  
  15. import org.xmlpull.v1.XmlPullParserException;  
  16.   
  17. import android.os.Handler;  
  18. import android.os.Message;  
  19.   
  20. /** 
  21.  * 访问WebService的工具类, 
  22.  *  
  23.  * @see http://blog.csdn.net/xiaanming 
  24.  *  
  25.  * @author xiaanming 
  26.  *  
  27.  */  
  28. public class WebServiceUtils {  
  29.     public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";  
  30.       
  31.       
  32.     // 含有3个线程的线程池  
  33.     private static final ExecutorService executorService = Executors  
  34.             .newFixedThreadPool(3);  
  35.   
  36.     // 命名空间  
  37.     private static final String NAMESPACE = "http://WebXml.com.cn/";  
  38.   
  39.     /** 
  40.      *  
  41.      * @param url 
  42.      *            WebService服务器地址 
  43.      * @param methodName 
  44.      *            WebService的调用方法名 
  45.      * @param properties 
  46.      *            WebService的参数 
  47.      * @param webServiceCallBack 
  48.      *            回调接口 
  49.      */  
  50.     public static void callWebService(String url, final String methodName,  
  51.             HashMap<String, String> properties,  
  52.             final WebServiceCallBack webServiceCallBack) {  
  53.         // 创建HttpTransportSE对象,传递WebService服务器地址  
  54.         final HttpTransportSE httpTransportSE = new HttpTransportSE(url);  
  55.         // 创建SoapObject对象  
  56.         SoapObject soapObject = new SoapObject(NAMESPACE, methodName);  
  57.   
  58.         // SoapObject添加参数  
  59.         if (properties != null) {  
  60.             for (Iterator<Map.Entry<String, String>> it = properties.entrySet()  
  61.                     .iterator(); it.hasNext();) {  
  62.                 Map.Entry<String, String> entry = it.next();  
  63.                 soapObject.addProperty(entry.getKey(), entry.getValue());  
  64.             }  
  65.         }  
  66.   
  67.         // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号  
  68.         final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(  
  69.                 SoapEnvelope.VER11);  
  70.         // 设置是否调用的是.Net开发的WebService  
  71.         soapEnvelope.setOutputSoapObject(soapObject);  
  72.         soapEnvelope.dotNet = true;  
  73.         httpTransportSE.debug = true;  
  74.   
  75.         // 用于子线程与主线程通信的Handler  
  76.         final Handler mHandler = new Handler() {  
  77.   
  78.             @Override  
  79.             public void handleMessage(Message msg) {  
  80.                 super.handleMessage(msg);  
  81.                 // 将返回值回调到callBack的参数中  
  82.                 webServiceCallBack.callBack((SoapObject) msg.obj);  
  83.             }  
  84.   
  85.         };  
  86.   
  87.         // 开启线程去访问WebService  
  88.         executorService.submit(new Runnable() {  
  89.   
  90.             @Override  
  91.             public void run() {  
  92.                 SoapObject resultSoapObject = null;  
  93.                 try {  
  94.                     httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);  
  95.                     if (soapEnvelope.getResponse() != null) {  
  96.                         // 获取服务器响应返回的SoapObject  
  97.                         resultSoapObject = (SoapObject) soapEnvelope.bodyIn;  
  98.                     }  
  99.                 } catch (HttpResponseException e) {  
  100.                     e.printStackTrace();  
  101.                 } catch (IOException e) {  
  102.                     e.printStackTrace();  
  103.                 } catch (XmlPullParserException e) {  
  104.                     e.printStackTrace();  
  105.                 } finally {  
  106.                     // 将获取的消息利用Handler发送到主线程  
  107.                     mHandler.sendMessage(mHandler.obtainMessage(0,  
  108.                             resultSoapObject));  
  109.                 }  
  110.             }  
  111.         });  
  112.     }  
  113.   
  114.     /** 
  115.      *  
  116.      *  
  117.      * @author xiaanming 
  118.      *  
  119.      */  
  120.     public interface WebServiceCallBack {  
  121.         public void callBack(SoapObject result);  
  122.     }  
  123.   
  124. }  

我们通过调用里面的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

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  6.     <ListView  
  7.         android:id="@+id/province_list"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:cacheColorHint="@android:color/transparent"  
  11.         android:fadingEdge="none" >  
  12.     </ListView>  
  13.   
  14. </RelativeLayout>  
接下来就是Activity的代码,先用工具类调用WebService方法,然后在回调方法callBack(SoapObject result)中解析数据到一个List<String>中,在设置ListView的适配器

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.webservicedemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.ksoap2.serialization.SoapObject;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.ListView;  
  16. import android.widget.Toast;  
  17.   
  18. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;  
  19.   
  20. /** 
  21.  * 显示天气省份的Activity 
  22.  *  
  23.  * @see http://blog.csdn.net/xiaanming 
  24.  *  
  25.  * @author xiaanming 
  26.  * 
  27.  */  
  28. public class MainActivity extends Activity {  
  29.     private List<String> provinceList = new ArrayList<String>();  
  30.   
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.         init();  
  36.     }  
  37.   
  38.     private void init() {  
  39.         final ListView mProvinceList = (ListView) findViewById(R.id.province_list);  
  40.           
  41.         //显示进度条  
  42.         ProgressDialogUtils.showProgressDialog(this"数据加载中...");  
  43.           
  44.         //通过工具类调用WebService接口  
  45.         WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportProvince"nullnew WebServiceCallBack() {  
  46.               
  47.             //WebService接口返回的数据回调到这个方法中  
  48.             @Override  
  49.             public void callBack(SoapObject result) {  
  50.                 //关闭进度条  
  51.                 ProgressDialogUtils.dismissProgressDialog();  
  52.                 if(result != null){  
  53.                     provinceList = parseSoapObject(result);  
  54.                     mProvinceList.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provinceList));  
  55.                 }else{  
  56.                     Toast.makeText(MainActivity.this"获取WebService数据错误", Toast.LENGTH_SHORT).show();  
  57.                 }  
  58.             }  
  59.         });  
  60.           
  61.         mProvinceList.setOnItemClickListener(new OnItemClickListener() {  
  62.   
  63.             @Override  
  64.             public void onItemClick(AdapterView<?> parent, View view,  
  65.                     int position, long id) {  
  66.                 Intent intent = new Intent(MainActivity.this, CityActivity.class);  
  67.                 intent.putExtra("province", provinceList.get(position));  
  68.                 startActivity(intent);  
  69.                   
  70.             }  
  71.         });  
  72.           
  73.           
  74.     }  
  75.       
  76.     /** 
  77.      * 解析SoapObject对象 
  78.      * @param result 
  79.      * @return 
  80.      */  
  81.     private List<String> parseSoapObject(SoapObject result){  
  82.         List<String> list = new ArrayList<String>();  
  83.         SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportProvinceResult");  
  84.         if(provinceSoapObject == null) {  
  85.             return null;  
  86.         }  
  87.         for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){  
  88.             list.add(provinceSoapObject.getProperty(i).toString());  
  89.         }  
  90.           
  91.         return list;  
  92.     }  
  93.   
  94. }  
点击省份进入该省份下面的市。也用一个ListView来显示市的数据,布局跟上面一样,Activity里面的代码也差不多相似,我就不过多说明了,直接看代码

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.webservicedemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import org.ksoap2.serialization.SoapObject;  
  8.   
  9. import android.app.Activity;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.ArrayAdapter;  
  16. import android.widget.ListView;  
  17. import android.widget.Toast;  
  18.   
  19. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;  
  20.   
  21. /** 
  22.  * 显示城市的Activity 
  23.  *  
  24.  * @see http://blog.csdn.net/xiaanming 
  25.  *  
  26.  * @author xiaanming 
  27.  * 
  28.  */  
  29. public class CityActivity extends Activity {  
  30.     private List<String> cityStringList;  
  31.   
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_main);  
  36.         init();  
  37.     }  
  38.   
  39.     private void init() {  
  40.         final ListView mCityList = (ListView) findViewById(R.id.province_list);  
  41.           
  42.         //显示进度条  
  43.         ProgressDialogUtils.showProgressDialog(this"数据加载中...");  
  44.           
  45.         //添加参数  
  46.         HashMap<String, String> properties = new HashMap<String, String>();  
  47.         properties.put("byProvinceName", getIntent().getStringExtra("province"));  
  48.           
  49.         WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportCity", properties, new WebServiceCallBack() {  
  50.               
  51.             @Override  
  52.             public void callBack(SoapObject result) {  
  53.                 ProgressDialogUtils.dismissProgressDialog();  
  54.                 if(result != null){  
  55.                     cityStringList = parseSoapObject(result);  
  56.                     mCityList.setAdapter(new ArrayAdapter<String>(CityActivity.this, android.R.layout.simple_list_item_1, cityStringList));  
  57.                 }else{  
  58.                     Toast.makeText(CityActivity.this"获取WebService数据错误", Toast.LENGTH_SHORT).show();  
  59.                 }  
  60.             }  
  61.         });  
  62.           
  63.         mCityList.setOnItemClickListener(new OnItemClickListener() {  
  64.   
  65.             @Override  
  66.             public void onItemClick(AdapterView<?> parent, View view,  
  67.                     int position, long id) {  
  68.                 Intent intent = new Intent(CityActivity.this, WeatherActivity.class);  
  69.                 intent.putExtra("city", cityStringList.get(position));  
  70.                 startActivity(intent);  
  71.             }  
  72.         });  
  73.     }  
  74.       
  75.     /** 
  76.      * 解析SoapObject对象 
  77.      * @param result 
  78.      * @return 
  79.      */  
  80.     private List<String> parseSoapObject(SoapObject result){  
  81.         List<String> list = new ArrayList<String>();  
  82.         SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportCityResult");  
  83.         for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){  
  84.             String cityString = provinceSoapObject.getProperty(i).toString();  
  85.             list.add(cityString.substring(0, cityString.indexOf("(")).trim());  
  86.         }  
  87.           
  88.         return list;  
  89.     }  
  90. }  
接下来就是点击相对应的城市调用WebService接口来获取该城市下面的天气详情啦,为了简单起见,我用一个TextView来显示天气信息,因为天气信息很多,一个屏幕显示不完,所以我们考虑在外面加一个ScrollView来进行滚动

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  6.     <ScrollView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent" >  
  9.   
  10.         <LinearLayout  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="match_parent" >  
  13.   
  14.             <TextView  
  15.                 android:id="@+id/weather"  
  16.                 android:textColor="#336598"  
  17.                 android:textSize="16sp"  
  18.                 android:layout_width="match_parent"  
  19.                 android:layout_height="match_parent" />  
  20.         </LinearLayout>  
  21.     </ScrollView>  
  22.   
  23. </RelativeLayout>  
Activity的代码就不做过多说明,跟上面的大同小异

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.webservicedemo;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import org.ksoap2.serialization.SoapObject;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;  
  13.   
  14. /** 
  15.  * 显示天气的Activity 
  16.  *  
  17.  * @see http://blog.csdn.net/xiaanming 
  18.  *  
  19.  * @author xiaanming 
  20.  * 
  21.  */  
  22. public class WeatherActivity extends Activity{  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.weather_layout);  
  27.         init();  
  28.     }  
  29.   
  30.     private void init() {  
  31.         final TextView mTextWeather = (TextView) findViewById(R.id.weather);  
  32.         ProgressDialogUtils.showProgressDialog(this"数据加载中...");  
  33.         HashMap<String, String> properties = new HashMap<String, String>();  
  34.         properties.put("theCityName", getIntent().getStringExtra("city"));  
  35.           
  36.         WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getWeatherbyCityName", properties, new WebServiceCallBack() {  
  37.               
  38.             @Override  
  39.             public void callBack(SoapObject result) {  
  40.                 ProgressDialogUtils.dismissProgressDialog();  
  41.                 if(result != null){  
  42.                     SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");  
  43.                     StringBuilder sb = new StringBuilder();  
  44.                     for(int i=0; i<detail.getPropertyCount(); i++){  
  45.                         sb.append(detail.getProperty(i)).append("\r\n");  
  46.                     }  
  47.                     mTextWeather.setText(sb.toString());  
  48.                 }else{  
  49.                     Toast.makeText(WeatherActivity.this"获取WebService数据错误", Toast.LENGTH_SHORT).show();  
  50.                 }  
  51.             }  
  52.         });  
  53.     }  
  54. }  
到这里我们就完成了编码工作,在运行程序之前我们需要在AndroidManifest.xml注册Activity,以及添加访问网络的权限

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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.   
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.         <activity android:name=".CityActivity"/>  
  15.         <activity android:name=".WeatherActivity"></activity>  
  16.     </application>  
  17.       
  18.     <uses-permission android:name="android.permission.INTERNET"/>  
运行结果:



省份,城市列表可以加上A-Z的排序功能,可以参考下Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音,我这里就不添加了,需要添加的朋友自行实现,好了,今天的讲解到此结束,有疑问的朋友请在下面留言。

项目源码,点击下载


1 0