]Android通过调用Webservice实现天气预报

分类:Android 550人阅读 评论(0) 收藏 举报

通过这一篇文章WebService的读书笔记对Web Service的认识,现在来写一个小应用Android通过调用Webservice实现天气预报来加强对Web Srevice的学习

在开发天气预报的Android应用之前,首先需要找到一个可以对外提供天气预报的Web Service,通过搜索发现站点http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx可以对外提供天气预报的Web Service,因此程序会调用此站点的Web Service来实现天气预报。(注意:如果该站点的天气预报Web Service服务已经停止,那么本程序将无法正常调用Web Service,那么天气预报的功能自然也就失效啦)

好啦,现在开始step by step地实现该应用程序。

step1:新建Android项目MyWeather


step2:获取并使用KSOAP包

在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。PC版本的WebService库非常丰富,但这些对Android来说过于庞大。适合手机的WebService客户端的SDK有一些,比较常用的是KSOAP2。

KSOAP2 地址:http://code.google.com/p/ksoap2-android/

我下载的最新的是: ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar

选择我们的项目,右键菜单中 Build Path –> Add External Archives… 增加这个下载的包




step3:设计应用的UI界面 /res/layout/main.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5. <LinearLayoutandroid:layout_width="fill_parent"
  6. android:layout_height="wrap_content">
  7. <TextViewandroid:layout_width="wrap_content"
  8. android:layout_height="wrap_content"android:hint="@string/province"/>
  9. <!--让用户选择省份的Spinner-->
  10. <Spinnerandroid:id="@+id/province"android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"/>
  12. </LinearLayout>
  13. <LinearLayoutandroid:layout_width="fill_parent"
  14. android:layout_height="wrap_content">
  15. <TextViewandroid:layout_width="wrap_content"
  16. android:layout_height="wrap_content"android:hint="@string/city"/>
  17. <!--让用户选择城市的Spinner-->
  18. <Spinnerandroid:id="@+id/city"android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"/>
  20. </LinearLayout>
  21. <!--显示今天天气的图片和文本框-->
  22. <LinearLayoutandroid:layout_width="fill_parent"
  23. android:layout_height="wrap_content">
  24. <ImageViewandroid:id="@+id/todayWhIcon1"
  25. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  26. <ImageViewandroid:id="@+id/todayWhIcon2"
  27. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  28. <TextViewandroid:id="@+id/weatherToday"
  29. android:layout_width="fill_parent"android:layout_height="wrap_content"
  30. android:layout_weight="1"/>
  31. </LinearLayout>
  32. <!--显示明天天气的图片和文本框-->
  33. <LinearLayoutandroid:layout_width="fill_parent"
  34. android:layout_height="wrap_content">
  35. <ImageViewandroid:id="@+id/tomorrowWhIcon1"
  36. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  37. <ImageViewandroid:id="@+id/tomorrowWhIcon2"
  38. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  39. <TextViewandroid:id="@+id/weatherTomorrow"
  40. android:layout_width="fill_parent"android:layout_height="wrap_content"
  41. android:layout_weight="1"/>
  42. </LinearLayout>
  43. <!--显示后天天气的图片和文本框-->
  44. <LinearLayoutandroid:layout_width="fill_parent"
  45. android:layout_height="wrap_content">
  46. <ImageViewandroid:id="@+id/afterdayWhIcon1"
  47. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  48. <ImageViewandroid:id="@+id/afterdayWhIcon2"
  49. android:layout_width="wrap_content"android:layout_height="wrap_content"/>
  50. <TextViewandroid:id="@+id/weatherAfterday"
  51. android:layout_width="fill_parent"android:layout_height="wrap_content"
  52. android:layout_weight="1"/>
  53. </LinearLayout>
  54. <TextViewandroid:id="@+id/weatherCurrent"
  55. android:layout_width="fill_parent"android:layout_height="wrap_content"/>
  56. </LinearLayout>


/res/values/strings.xml
[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="app_name">天气预报</string>
  4. <stringname="btn_apply">查询</string>
  5. <stringname="text_hint">城市中文名</string>
  6. <stringname="province">省份</string>
  7. <stringname="city">城市</string>
  8. </resources>

step3:编写调用Web Service的工具类 cn.roco.weather.WebServiceUtil.java

因为本程序主要需要调用如下三个Web Service操作:

a.获取省份:getRegionProvince方法

b.根据省份获取城市:getSupportCityString方法

c.根据城市获取天气:getWeather方法

为了让应用界面更加美观,可以访问http://www.webxml.com.cn/images/weather.zip下载各种天气图标,可以使用这些天气图标来美化应用。

[java] view plain copy
  1. packagecn.roco.weather;
  2. importjava.io.IOException;
  3. importjava.util.ArrayList;
  4. importjava.util.List;
  5. importorg.ksoap2.SoapEnvelope;
  6. importorg.ksoap2.serialization.SoapObject;
  7. importorg.ksoap2.serialization.SoapSerializationEnvelope;
  8. importorg.ksoap2.transport.HttpTransportSE;
  9. importorg.xmlpull.v1.XmlPullParserException;
  10. publicclassWebServiceUtil
  11. {
  12. //定义WebService的命名空间
  13. staticfinalStringSERVICE_NS="http://WebXml.com.cn/";
  14. //定义WebService提供服务的URL
  15. staticfinalStringSERVICE_URL=
  16. "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
  17. //调用远程WebService获取省份列表
  18. publicstaticList<String>getProvinceList()
  19. {
  20. /**
  21. *调用远程WebService的getRegionProvince方法:获得中国省份、直辖市、地区和与之对应的ID
  22. *<ArrayOfStringxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://WebXml.com.cn/">
  23. <string>黑龙江,3113</string>
  24. <string>吉林,3114</string>
  25. <string>辽宁,3115</string>
  26. <string>内蒙古,3116</string>
  27. <string>河北,3117</string>
  28. <string>河南,3118</string>
  29. <string>山东,3119</string>
  30. <string>山西,31110</string>
  31. <string>江苏,31111</string>
  32. <string>安徽,31112</string>
  33. <string>陕西,31113</string>
  34. <string>宁夏,31114</string>
  35. <string>甘肃,31115</string>
  36. <string>青海,31116</string>
  37. <string>湖北,31117</string>
  38. <string>湖南,31118</string>
  39. <string>浙江,31119</string>
  40. <string>江西,31120</string>
  41. <string>福建,31121</string>
  42. <string>贵州,31122</string>
  43. <string>四川,31123</string>
  44. <string>广东,31124</string>
  45. <string>广西,31125</string>
  46. <string>云南,31126</string>
  47. <string>海南,31127</string>
  48. <string>新疆,31128</string>
  49. <string>西藏,31129</string>
  50. <string>台湾,31130</string>
  51. <string>北京,311101</string>
  52. <string>上海,311102</string>
  53. <string>天津,311103</string>
  54. <string>重庆,311104</string>
  55. <string>香港,311201</string>
  56. <string>澳门,311202</string>
  57. <string>钓鱼岛,311203</string>
  58. </ArrayOfString>
  59. */
  60. StringmethodName="getRegionProvince";//获得中国省份、直辖市、地区和与之对应的ID
  61. //创建HttpTransportSE传输对象,该对象用于调用WebService操作
  62. HttpTransportSEht=newHttpTransportSE(SERVICE_URL);
  63. ht.debug=true;
  64. //使用SOAP1.1协议创建Envelop对象
  65. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  66. SoapEnvelope.VER11);
  67. //实例化SoapObject对象,传入所要调用的WebService的命名空间,WebService方法名
  68. SoapObjectsoapObject=newSoapObject(SERVICE_NS,methodName);
  69. //将soapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
  70. envelope.bodyOut=soapObject;
  71. /**
  72. *因为什么这个网站是通过.NET对外提供WebService的,
  73. *因此设置与.Net提供的WebService保持较好的兼容性
  74. */
  75. envelope.dotNet=true;
  76. try
  77. {
  78. //调用WebService
  79. ht.call(SERVICE_NS+methodName,envelope);
  80. if(envelope.getResponse()!=null)
  81. {
  82. //获取服务器响应返回的SOAP消息
  83. SoapObjectresult=(SoapObject)envelope.bodyIn;
  84. SoapObjectdetail=(SoapObject)result.getProperty(methodName
  85. +"Result");
  86. //解析服务器响应的SOAP消息。
  87. returnparseProvinceOrCity(detail);
  88. }
  89. }
  90. catch(IOExceptione)
  91. {
  92. e.printStackTrace();
  93. }
  94. catch(XmlPullParserExceptione)
  95. {
  96. e.printStackTrace();
  97. }
  98. returnnull;
  99. }
  100. //根据省份获取城市列表
  101. publicstaticList<String>getCityListByProvince(Stringprovince)
  102. {
  103. /**
  104. *调用的方法
  105. *获得支持的城市/地区名称和与之对应的ID
  106. 输入参数:theRegionCode=省市、国家ID或名称,返回数据:一维字符串数组。
  107. 如:输入北京的theRegionCode:311101得到的返回结果为:
  108. <ArrayOfStringxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://WebXml.com.cn/">
  109. <string>北京,792</string>
  110. <string>昌平,785</string>
  111. <string>大兴,826</string>
  112. <string>房山,827</string>
  113. <string>怀柔,752</string>
  114. <string>门头沟,788</string>
  115. <string>密云,751</string>
  116. <string>平谷,756</string>
  117. <string>顺义,741</string>
  118. <string>通州,3409</string>
  119. <string>延庆,746</string>
  120. <string>海淀,742</string>
  121. <string>朝阳,3408</string>
  122. <string>丰台,795</string>
  123. <string>石景山,794</string>
  124. </ArrayOfString>
  125. */
  126. StringmethodName="getSupportCityString";
  127. //创建HttpTransportSE传输对象
  128. HttpTransportSEht=newHttpTransportSE(SERVICE_URL);
  129. ht.debug=true;
  130. //实例化SoapObject对象
  131. SoapObjectsoapObject=newSoapObject(SERVICE_NS,methodName);
  132. //添加一个请求参数
  133. soapObject.addProperty("theRegionCode",province);
  134. //使用SOAP1.1协议创建Envelop对象
  135. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  136. SoapEnvelope.VER11);
  137. envelope.bodyOut=soapObject;
  138. //设置与.Net提供的WebService保持较好的兼容性
  139. envelope.dotNet=true;
  140. try
  141. {
  142. //调用WebService
  143. ht.call(SERVICE_NS+methodName,envelope);
  144. if(envelope.getResponse()!=null)
  145. {
  146. //获取服务器响应返回的SOAP消息
  147. SoapObjectresult=(SoapObject)envelope.bodyIn;
  148. SoapObjectdetail=(SoapObject)result.getProperty(methodName
  149. +"Result");
  150. //解析服务器响应的SOAP消息。
  151. returnparseProvinceOrCity(detail);
  152. }
  153. }
  154. catch(IOExceptione)
  155. {
  156. e.printStackTrace();
  157. }
  158. catch(XmlPullParserExceptione)
  159. {
  160. e.printStackTrace();
  161. }
  162. returnnull;
  163. }
  164. //解析服务器响应的SOAP消息。
  165. privatestaticList<String>parseProvinceOrCity(SoapObjectdetail)
  166. {
  167. List<String>result=newArrayList<String>();
  168. for(inti=0;i<detail.getPropertyCount();i++)
  169. {
  170. //解析出每个省份
  171. result.add(detail.getProperty(i).toString().split(",")[0]);
  172. }
  173. returnresult;
  174. }
  175. //根据城市获取城市具体天气情况
  176. publicstaticSoapObjectgetWeatherByCity(StringcityName)
  177. {
  178. /**
  179. *获得天气预报数据输入参数:城市/地区ID或名称,返回数据:一维字符串数组。
  180. 如:输入theCityCode:792(<string>北京,792</string>)得到的返回结果为:
  181. ThisXMLfiledoesnotappeartohaveanystyleinformationassociatedwithit.Thedocumenttreeisshownbelow.
  182. <ArrayOfStringxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://WebXml.com.cn/">
  183. <string>直辖市北京</string>
  184. <string>北京</string>
  185. <string>792</string>
  186. <string>2013/04/3003:47:53</string>
  187. <string>今日天气实况:气温:14℃;风向/风力:东风2级;湿度:21%</string>
  188. <string>空气质量:良;紫外线强度:强</string>
  189. <string>
  190. 穿衣指数:建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。过敏指数:天气条件极易诱发过敏,易过敏人群尽量减少外出,外出宜穿长衣长裤并佩戴好眼镜和口罩,外出归来时及时清洁手和口鼻。运动指数:天气较好,但由于风力较大,推荐您在室内进行低强度运动,若在户外运动请注意避风。洗车指数:适宜洗车,未来持续两天无雨天气较好,适合擦洗汽车,蓝天白云、风和日丽将伴您的车子连日洁净。晾晒指数:天气不错,适宜晾晒。赶紧把久未见阳光的衣物搬出来吸收一下太阳的味道吧!旅游指数:天气较好,风稍大,但温度适宜,是个好天气哦。很适宜旅游,您可以尽情地享受大自然的无限风光。路况指数:天气较好,路面比较干燥,路况较好。舒适度指数:白天天气晴好,您在这种天气条件下,会感觉早晚凉爽、舒适,午后偏热。空气污染指数:气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。紫外线指数:紫外线辐射强,建议涂擦SPF20左右、PA++的防晒护肤品。避免在10点至14点暴露于日光下。
  191. </string>
  192. <string>4月30日晴</string>
  193. <string>11℃/27℃</string>
  194. <string>北风3-4级转无持续风向微风</string>
  195. <string>0.gif</string>
  196. <string>0.gif</string>
  197. <string>5月1日晴转多云</string>
  198. <string>12℃/25℃</string>
  199. <string>无持续风向微风</string>
  200. <string>0.gif</string>
  201. <string>1.gif</string>
  202. <string>5月2日多云转晴</string>
  203. <string>13℃/26℃</string>
  204. <string>无持续风向微风</string>
  205. <string>1.gif</string>
  206. <string>0.gif</string>
  207. <string>5月3日多云转阴</string>
  208. <string>11℃/23℃</string>
  209. <string>无持续风向微风</string>
  210. <string>1.gif</string>
  211. <string>2.gif</string>
  212. <string>5月4日阴转多云</string>
  213. <string>14℃/27℃</string>
  214. <string>无持续风向微风</string>
  215. <string>2.gif</string>
  216. <string>1.gif</string>
  217. </ArrayOfString>
  218. */
  219. StringmethodName="getWeather";
  220. HttpTransportSEht=newHttpTransportSE(SERVICE_URL);
  221. ht.debug=true;
  222. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  223. SoapEnvelope.VER11);
  224. SoapObjectsoapObject=newSoapObject(SERVICE_NS,methodName);
  225. soapObject.addProperty("theCityCode",cityName);
  226. envelope.bodyOut=soapObject;
  227. //设置与.Net提供的WebService保持较好的兼容性
  228. envelope.dotNet=true;
  229. try
  230. {
  231. ht.call(SERVICE_NS+methodName,envelope);
  232. SoapObjectresult=(SoapObject)envelope.bodyIn;
  233. SoapObjectdetail=(SoapObject)result.getProperty(methodName
  234. +"Result");
  235. returndetail;
  236. }
  237. catch(Exceptione)
  238. {
  239. e.printStackTrace();
  240. }
  241. returnnull;
  242. }
  243. }

step4:编写适配器,用于显示数据 cn.roco.weather.ListAdapter.java

[java] view plain copy
  1. packagecn.roco.weather;
  2. importjava.util.List;
  3. importandroid.content.Context;
  4. importandroid.graphics.Color;
  5. importandroid.view.View;
  6. importandroid.view.ViewGroup;
  7. importandroid.widget.BaseAdapter;
  8. importandroid.widget.TextView;
  9. publicclassListAdapterextendsBaseAdapter
  10. {
  11. privateContextcontext;
  12. privateList<String>values;
  13. publicListAdapter(Contextcontext,List<String>values)
  14. {
  15. this.context=context;
  16. this.values=values;
  17. }
  18. @Override
  19. publicintgetCount()
  20. {
  21. returnvalues.size();
  22. }
  23. @Override
  24. publicObjectgetItem(intposition)
  25. {
  26. returnvalues.get(position);
  27. }
  28. @Override
  29. publiclonggetItemId(intposition)
  30. {
  31. returnposition;
  32. }
  33. @Override
  34. publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
  35. {
  36. TextViewtext=newTextView(context);
  37. text.setText(values.get(position));
  38. text.setTextSize(20);
  39. text.setTextColor(Color.BLACK);
  40. returntext;
  41. }
  42. }

step5:程序的主应用cn.roco.weather.MyWeather.java

[java] view plain copy
  1. packagecn.roco.weather;
  2. importjava.util.List;
  3. importorg.ksoap2.serialization.SoapObject;
  4. importandroid.app.Activity;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.widget.AdapterView;
  8. importandroid.widget.ImageView;
  9. importandroid.widget.Spinner;
  10. importandroid.widget.TextView;
  11. importandroid.widget.AdapterView.OnItemSelectedListener;
  12. publicclassMyWeatherextendsActivity
  13. {
  14. privateSpinnerprovinceSpinner;
  15. privateSpinnercitySpinner;
  16. privateImageViewtodayWhIcon1;
  17. privateImageViewtodayWhIcon2;
  18. privateTextViewtextWeatherToday;
  19. privateImageViewtomorrowWhIcon1;
  20. privateImageViewtomorrowWhIcon2;
  21. privateTextViewtextWeatherTomorrow;
  22. privateImageViewafterdayWhIcon1;
  23. privateImageViewafterdayWhIcon2;
  24. privateTextViewtextWeatherAfterday;
  25. privateTextViewtextWeatherCurrent;
  26. @Override
  27. publicvoidonCreate(BundlesavedInstanceState)
  28. {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.main);
  31. todayWhIcon1=(ImageView)findViewById(R.id.todayWhIcon1);
  32. todayWhIcon2=(ImageView)findViewById(R.id.todayWhIcon2);
  33. textWeatherToday=(TextView)findViewById(R.id.weatherToday);
  34. tomorrowWhIcon1=(ImageView)findViewById(R.id.tomorrowWhIcon1);
  35. tomorrowWhIcon2=(ImageView)findViewById(R.id.tomorrowWhIcon2);
  36. textWeatherTomorrow=(TextView)findViewById(R.id.weatherTomorrow);
  37. afterdayWhIcon1=(ImageView)findViewById(R.id.afterdayWhIcon1);
  38. afterdayWhIcon2=(ImageView)findViewById(R.id.afterdayWhIcon2);
  39. textWeatherAfterday=(TextView)findViewById(R.id.weatherAfterday);
  40. textWeatherCurrent=(TextView)findViewById(R.id.weatherCurrent);
  41. //获取程序界面中选择省份、城市的Spinner组件
  42. provinceSpinner=(Spinner)findViewById(R.id.province);
  43. citySpinner=(Spinner)findViewById(R.id.city);
  44. //调用远程WebService获取省份列表
  45. List<String>provinces=WebServiceUtil.getProvinceList();
  46. ListAdapteradapter=newListAdapter(this,provinces);
  47. //使用Spinner显示省份列表
  48. provinceSpinner.setAdapter(adapter);
  49. //当省份Spinner的选择项被改变时
  50. provinceSpinner.setOnItemSelectedListener(newOnItemSelectedListener()
  51. {
  52. @Override
  53. publicvoidonItemSelected(AdapterView<?>source,Viewparent,
  54. intposition,longid)
  55. {
  56. //根据省份获取城市列表
  57. List<String>cities=WebServiceUtil
  58. .getCityListByProvince(provinceSpinner.getSelectedItem()
  59. .toString());
  60. ListAdaptercityAdapter=newListAdapter(MyWeather.this,
  61. cities);
  62. //使用Spinner显示城市列表
  63. citySpinner.setAdapter(cityAdapter);
  64. }
  65. @Override
  66. publicvoidonNothingSelected(AdapterView<?>arg0)
  67. {
  68. }
  69. });
  70. //当城市Spinner的选择项被改变时
  71. citySpinner.setOnItemSelectedListener(newOnItemSelectedListener()
  72. {
  73. @Override
  74. publicvoidonItemSelected(AdapterView<?>source,Viewparent,
  75. intposition,longid)
  76. {
  77. //展现天气预报的具体数据
  78. showWeather(citySpinner.getSelectedItem().toString());
  79. }
  80. @Override
  81. publicvoidonNothingSelected(AdapterView<?>arg0)
  82. {
  83. }
  84. });
  85. }
  86. //展现天气预报的具体数据
  87. privatevoidshowWeather(Stringcity)
  88. {
  89. StringweatherToday=null;
  90. StringweatherTomorrow=null;
  91. StringweatherAfterday=null;
  92. StringweatherCurrent=null;
  93. inticonToday[]=newint[2];
  94. inticonTomorrow[]=newint[2];
  95. inticonAfterday[]=newint[2];
  96. //获取远程WebService返回的对象
  97. SoapObjectdetail=WebServiceUtil.getWeatherByCity(city);//根据城市获取城市具体天气情况
  98. //获取天气实况
  99. weatherCurrent=detail.getProperty(4).toString();
  100. //解析今天的天气情况
  101. Stringdate=detail.getProperty(7).toString();
  102. weatherToday="今天:"+date.split("")[0];
  103. weatherToday=weatherToday+"\n天气:"+date.split("")[1];
  104. weatherToday=weatherToday+"\n气温:"
  105. +detail.getProperty(8).toString();
  106. weatherToday=weatherToday+"\n风力:"
  107. +detail.getProperty(9).toString()+"\n";
  108. iconToday[0]=parseIcon(detail.getProperty(10).toString());
  109. iconToday[1]=parseIcon(detail.getProperty(11).toString());
  110. //解析明天的天气情况
  111. date=detail.getProperty(12).toString();
  112. weatherTomorrow="明天:"+date.split("")[0];
  113. weatherTomorrow=weatherTomorrow+"\n天气:"+date.split("")[1];
  114. weatherTomorrow=weatherTomorrow+"\n气温:"
  115. +detail.getProperty(13).toString();
  116. weatherTomorrow=weatherTomorrow+"\n风力:"
  117. +detail.getProperty(14).toString()+"\n";
  118. iconTomorrow[0]=parseIcon(detail.getProperty(15).toString());
  119. iconTomorrow[1]=parseIcon(detail.getProperty(16).toString());
  120. //解析后天的天气情况
  121. date=detail.getProperty(17).toString();
  122. weatherAfterday="后天:"+date.split("")[0];
  123. weatherAfterday=weatherAfterday+"\n天气:"+date.split("")[1];
  124. weatherAfterday=weatherAfterday+"\n气温:"
  125. +detail.getProperty(18).toString();
  126. weatherAfterday=weatherAfterday+"\n风力:"
  127. +detail.getProperty(19).toString()+"\n";
  128. iconAfterday[0]=parseIcon(detail.getProperty(20).toString());
  129. iconAfterday[1]=parseIcon(detail.getProperty(21).toString());
  130. //更新当天的天气实况
  131. textWeatherCurrent.setText(weatherCurrent);
  132. //更新显示今天天气的图标和文本框
  133. textWeatherToday.setText(weatherToday);
  134. todayWhIcon1.setImageResource(iconToday[0]);
  135. todayWhIcon2.setImageResource(iconToday[1]);
  136. //更新显示明天天气的图标和文本框
  137. textWeatherTomorrow.setText(weatherTomorrow);
  138. tomorrowWhIcon1.setImageResource(iconTomorrow[0]);
  139. tomorrowWhIcon2.setImageResource(iconTomorrow[1]);
  140. //更新显示后天天气的图标和文本框
  141. textWeatherAfterday.setText(weatherAfterday);
  142. afterdayWhIcon1.setImageResource(iconAfterday[0]);
  143. afterdayWhIcon2.setImageResource(iconAfterday[1]);
  144. }
  145. //工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。
  146. privateintparseIcon(StringstrIcon)
  147. {
  148. if(strIcon==null)
  149. return-1;
  150. if("0.gif".equals(strIcon))
  151. returnR.drawable.a_0;
  152. if("1.gif".equals(strIcon))
  153. returnR.drawable.a_1;
  154. if("2.gif".equals(strIcon))
  155. returnR.drawable.a_2;
  156. if("3.gif".equals(strIcon))
  157. returnR.drawable.a_3;
  158. if("4.gif".equals(strIcon))
  159. returnR.drawable.a_4;
  160. if("5.gif".equals(strIcon))
  161. returnR.drawable.a_5;
  162. if("6.gif".equals(strIcon))
  163. returnR.drawable.a_6;
  164. if("7.gif".equals(strIcon))
  165. returnR.drawable.a_7;
  166. if("8.gif".equals(strIcon))
  167. returnR.drawable.a_8;
  168. if("9.gif".equals(strIcon))
  169. returnR.drawable.a_9;
  170. if("10.gif".equals(strIcon))
  171. returnR.drawable.a_10;
  172. if("11.gif".equals(strIcon))
  173. returnR.drawable.a_11;
  174. if("12.gif".equals(strIcon))
  175. returnR.drawable.a_12;
  176. if("13.gif".equals(strIcon))
  177. returnR.drawable.a_13;
  178. if("14.gif".equals(strIcon))
  179. returnR.drawable.a_14;
  180. if("15.gif".equals(strIcon))
  181. returnR.drawable.a_15;
  182. if("16.gif".equals(strIcon))
  183. returnR.drawable.a_16;
  184. if("17.gif".equals(strIcon))
  185. returnR.drawable.a_17;
  186. if("18.gif".equals(strIcon))
  187. returnR.drawable.a_18;
  188. if("19.gif".equals(strIcon))
  189. returnR.drawable.a_19;
  190. if("20.gif".equals(strIcon))
  191. returnR.drawable.a_20;
  192. if("21.gif".equals(strIcon))
  193. returnR.drawable.a_21;
  194. if("22.gif".equals(strIcon))
  195. returnR.drawable.a_22;
  196. if("23.gif".equals(strIcon))
  197. returnR.drawable.a_23;
  198. if("24.gif".equals(strIcon))
  199. returnR.drawable.a_24;
  200. if("25.gif".equals(strIcon))
  201. returnR.drawable.a_25;
  202. if("26.gif".equals(strIcon))
  203. returnR.drawable.a_26;
  204. if("27.gif".equals(strIcon))
  205. returnR.drawable.a_27;
  206. if("28.gif".equals(strIcon))
  207. returnR.drawable.a_28;
  208. if("29.gif".equals(strIcon))
  209. returnR.drawable.a_29;
  210. if("30.gif".equals(strIcon))
  211. returnR.drawable.a_30;
  212. if("31.gif".equals(strIcon))
  213. returnR.drawable.a_31;
  214. return0;
  215. }
  216. }


step6:AndroidManifest.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:versionCode="1"android:versionName="1.0"package="cn.roco.weather">
  4. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  5. <activityandroid:name=".MyWeather"android:label="@string/app_name">
  6. <intent-filter>
  7. <actionandroid:name="android.intent.action.MAIN"/>
  8. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  9. </intent-filter>
  10. </activity>
  11. </application>
  12. <uses-permissionandroid:name="android.permission.INTERNET"/>
  13. </manifest>

step7:部署应用,观看运行效果





附注:本应用的源码在:http://pan.baidu.com/share/link?shareid=419671&uk=805959799

关于Web Service的应用还可以查看Android通过调用Webservice实现手机号码归属地查询进行学习

分享到:

更多相关文章

  1. Android完全退出应用程序
  2. Android(安卓)Uevent 分析,从kernel到framework
  3. Android(安卓)基础总结:(三)Activity
  4. 两分钟彻底让你明白Android中onInterceptTouchEvent与onTouchEve
  5. 大话企业级android读书笔记(二)
  6. Android中的Binder机制一(实名Binder)
  7. Android(安卓)AsyncTask
  8. Android(安卓)Power Management
  9. Android(安卓)recovery UI实现分析

随机推荐

  1. Android简介
  2. Android(安卓)TextView 如何判断是否已经
  3. 【Android进阶】android:configChanges属
  4. Android(安卓)4.4 KitKat 支持 u 盘功能
  5. android基本架构
  6. android 实现静默安装、卸载(图)
  7. Android项目运行提示
  8. Android(安卓)进阶之 Android消息机制Han
  9. android 中activity 属性说明
  10. Android中Activity的四种启动模式详解