加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

利用WebService解析出的天气预报

发布时间:2020-12-17 00:34:22 所属栏目:安全 来源:网络整理
导读:? 利用WebService解析出的天气预报 事先声明,这个代码是《疯狂android讲义》上的,我只不过动手打了一遍,有些地方是拷贝的 布局文件 ? xml ? version = "1.0" ? encoding = "utf-8" ? ?? LinearLayout ? xmlns:android = "http://schemas.android.com/apk/

?

利用WebService解析出的天气预报

事先声明,这个代码是《疯狂android讲义》上的,我只不过动手打了一遍,有些地方是拷贝的

布局文件

  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:layout_width="fill_parent"??
  4. ????android:layout_height="wrap_content"??
  5. ????android:orientation="vertical"?>??
  6. ??
  7. ????<LinearLayout??
  8. ????????android:layout_width="fill_parent"??
  9. ????????android:layout_height="wrap_content"?>??
  10. ??
  11. ????????<TextView??
  12. ????????????android:layout_width="wrap_content"??
  13. ????????????android:layout_height="wrap_content"??
  14. ????????????android:hint="@string/province"?/>??
  15. ????????<!--?让用户选择省份的Spinner?-->??
  16. ??
  17. ????????<Spinner??
  18. ????????????android:id="@+id/province"??
  19. ????????????android:layout_width="fill_parent"??
  20. ????????????android:layout_height="wrap_content"?/>??
  21. ????</LinearLayout>??
  22. ??
  23. ????<LinearLayout??
  24. ????????android:layout_width="fill_parent"??
  25. ????????android:layout_height="wrap_content"?>??
  26. ??
  27. ????????<TextView??
  28. ????????????android:layout_width="wrap_content"??
  29. ????????????android:layout_height="wrap_content"??
  30. ????????????android:hint="@string/city"?/>??
  31. ????????<!--?让用户选择城市的Spinner?-->??
  32. ??
  33. ????????<Spinner??
  34. ????????????android:id="@+id/city"??
  35. ????????????android:layout_width="fill_parent"??
  36. ????????????android:layout_height="wrap_content"?/>??
  37. ????</LinearLayout>??
  38. ????<!--?显示今天天气的图片和文本框?-->??
  39. ??
  40. ????<LinearLayout??
  41. ????????android:layout_width="fill_parent"??
  42. ????????android:layout_height="wrap_content"?>??
  43. ??
  44. ????????<ImageView??
  45. ????????????android:id="@+id/todayWhIcon1"??
  46. ????????????android:layout_width="wrap_content"??
  47. ????????????android:layout_height="wrap_content"?/>??
  48. ??
  49. ????????<ImageView??
  50. ????????????android:id="@+id/todayWhIcon2"??
  51. ????????????android:layout_width="wrap_content"??
  52. ????????????android:layout_height="wrap_content"?/>??
  53. ??
  54. ????????<TextView??
  55. ????????????android:id="@+id/weatherToday"??
  56. ????????????android:layout_width="fill_parent"??
  57. ????????????android:layout_height="wrap_content"??
  58. ????????????android:layout_weight="1"?/>??
  59. ????</LinearLayout>??
  60. ????<!--?显示明天天气的图片和文本框?-->??
  61. ??
  62. ????<LinearLayout??
  63. ????????android:layout_width="fill_parent"??
  64. ????????android:layout_height="wrap_content"?>??
  65. ??
  66. ????????<ImageView??
  67. ????????????android:id="@+id/tomorrowWhIcon1"??
  68. ????????????android:layout_width="wrap_content"??
  69. ????????????android:layout_height="wrap_content"?/>??
  70. ??
  71. ????????<ImageView??
  72. ????????????android:id="@+id/tomorrowWhIcon2"??
  73. ????????????android:layout_width="wrap_content"??
  74. ????????????android:layout_height="wrap_content"?/>??
  75. ??
  76. ????????<TextView??
  77. ????????????android:id="@+id/weatherTomorrow"??
  78. ????????????android:layout_width="fill_parent"??
  79. ????????????android:layout_height="wrap_content"??
  80. ????????????android:layout_weight="1"?/>??
  81. ????</LinearLayout>??
  82. ????<!--?显示后天天气的图片和文本框?-->??
  83. ??
  84. ????<LinearLayout??
  85. ????????android:layout_width="fill_parent"??
  86. ????????android:layout_height="wrap_content"?>??
  87. ??
  88. ????????<ImageView??
  89. ????????????android:id="@+id/afterdayWhIcon1"??
  90. ????????????android:layout_width="wrap_content"??
  91. ????????????android:layout_height="wrap_content"?/>??
  92. ??
  93. ????????<ImageView??
  94. ????????????android:id="@+id/afterdayWhIcon2"??
  95. ????????????android:layout_width="wrap_content"??
  96. ????????????android:layout_height="wrap_content"?/>??
  97. ??
  98. ????????<TextView??
  99. ????????????android:id="@+id/weatherAfterday"??
  100. ????????????android:layout_width="fill_parent"??
  101. ????????????android:layout_height="wrap_content"??
  102. ????????????android:layout_weight="1"?/>??
  103. ????</LinearLayout>??
  104. ??
  105. ????<TextView??
  106. ????????android:id="@+id/weatherCurrent"??
  107. ????????android:layout_width="fill_parent"??
  108. ????????android:layout_height="wrap_content"?/>??
  109. ??
  110. </LinearLayout>??

主要activity文件



[java]? view plain copy print ?
  1. package?com.example.weatherservicedemo;??
  2. ??
  3. import?java.util.List;??
  4. import?org.ksoap2.serialization.SoapObject;??
  5. import?android.app.Activity;??
  6. import?android.os.Bundle;??
  7. import?android.view.View;??
  8. import?android.widget.AdapterView;??
  9. import?android.widget.AdapterView.OnItemSelectedListener;??
  10. import?android.widget.ImageView;??
  11. import?android.widget.Spinner;??
  12. import?android.widget.TextView;??
  13. ??
  14. /**?
  15. ?*?Description:?<br/>?
  16. ?*?site:?<a?href="http://www.crazyit.org">crazyit.org</a>?<br/>?
  17. ?*?Copyright?(C),?2001-2012,?Yeeku.H.Lee?<br/>?
  18. ?*?This?program?is?protected?by?copyright?laws.?<br/>?
  19. ?*?Program?Name:?<br/>?
  20. ?*?Date:?
  21. ?*??
  22. ?*?@author?Yeeku.H.Lee?kongyeeku@163.com?
  23. ?*?@version?1.0?
  24. ?*/??
  25. public?class?MyWeather?extends?Activity?{??
  26. ????private?Spinner?provinceSpinner;??
  27. ????private?Spinner?citySpinner;??
  28. ????private?ImageView?todayWhIcon1;??
  29. ????private?ImageView?todayWhIcon2;??
  30. ????private?TextView?textWeatherToday;??
  31. ????private?ImageView?tomorrowWhIcon1;??
  32. ????private?ImageView?tomorrowWhIcon2;??
  33. ????private?TextView?textWeatherTomorrow;??
  34. ????private?ImageView?afterdayWhIcon1;??
  35. ????private?ImageView?afterdayWhIcon2;??
  36. ????private?TextView?textWeatherAfterday;??
  37. ????private?TextView?textWeatherCurrent;??
  38. ??
  39. ????@Override??
  40. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  41. ????????super.onCreate(savedInstanceState);??
  42. ????????setContentView(R.layout.activity_main);??
  43. ??
  44. ????????todayWhIcon1?=?(ImageView)?findViewById(R.id.todayWhIcon1);??
  45. ????????todayWhIcon2?=?(ImageView)?findViewById(R.id.todayWhIcon2);??
  46. ????????textWeatherToday?=?(TextView)?findViewById(R.id.weatherToday);??
  47. ????????tomorrowWhIcon1?=?(ImageView)?findViewById(R.id.tomorrowWhIcon1);??
  48. ????????tomorrowWhIcon2?=?(ImageView)?findViewById(R.id.tomorrowWhIcon2);??
  49. ????????textWeatherTomorrow?=?(TextView)?findViewById(R.id.weatherTomorrow);??
  50. ????????afterdayWhIcon1?=?(ImageView)?findViewById(R.id.afterdayWhIcon1);??
  51. ????????afterdayWhIcon2?=?(ImageView)?findViewById(R.id.afterdayWhIcon2);??
  52. ????????textWeatherAfterday?=?(TextView)?findViewById(R.id.weatherAfterday);??
  53. ????????textWeatherCurrent?=?(TextView)?findViewById(R.id.weatherCurrent);??
  54. ??
  55. ????????//?获取程序界面中选择省份、城市的Spinner组件??
  56. ????????provinceSpinner?=?(Spinner)?findViewById(R.id.province);??
  57. ????????citySpinner?=?(Spinner)?findViewById(R.id.city);??
  58. ????????//?调用远程Web?Service获取省份列表??
  59. ????????List<String>?provinces?=?WebServiceUtil.getProvinceList();??
  60. ????????ListAdapter?adapter?=?new?ListAdapter(this,?provinces);??
  61. ????????//?使用Spinner显示省份列表??
  62. ????????provinceSpinner.setAdapter(adapter);??
  63. ????????//?当省份Spinner的选择项被改变时??
  64. ????????provinceSpinner.setOnItemSelectedListener(new?OnItemSelectedListener()?{??
  65. ????????????public?void?onItemSelected(AdapterView<?>?source,?View?parent,??
  66. ????????????????????int?position,?long?id)?{??
  67. ????????????????List<String>?cities?=?WebServiceUtil??
  68. ????????????????????????.getCityListByProvince(provinceSpinner??
  69. ????????????????????????????????.getSelectedItem().toString());??
  70. ????????????????ListAdapter?cityAdapter?=?new?ListAdapter(MyWeather.this,??
  71. ????????????????????????cities);??
  72. ????????????????//?使用Spinner显示城市列表??
  73. ????????????????citySpinner.setAdapter(cityAdapter);??
  74. ????????????}??
  75. ??
  76. ????????????public?void?onNothingSelected(AdapterView<?>?arg0)?{??
  77. ????????????}??
  78. ????????});??
  79. ????????//?当城市Spinner的选择项被改变时??
  80. ????????citySpinner.setOnItemSelectedListener(new?OnItemSelectedListener()?{??
  81. ????????????public?void?onItemSelected(AdapterView<?>?source,?long?id)?{??
  82. ????????????????showWeather(citySpinner.getSelectedItem().toString());??
  83. ????????????}??
  84. ??
  85. ????????????public?void?onNothingSelected(AdapterView<?>?arg0)?{??
  86. ????????????}??
  87. ????????});??
  88. ????}??
  89. ??
  90. ????private?void?showWeather(String?city)?{??
  91. ????????String?weatherToday?=?null;??
  92. ????????String?weatherTomorrow?=?null;??
  93. ????????String?weatherAfterday?=?null;??
  94. ????????String?weatherCurrent?=?null;??
  95. ????????int?iconToday[]?=?new?int[2];??
  96. ????????int?iconTomorrow[]?=?new?int[2];??
  97. ????????int?iconAfterday[]?=?new?int[2];??
  98. ????????//?获取远程Web?Service返回的对象??
  99. ????????SoapObject?detail?=?WebServiceUtil.getWeatherByCity(city);??
  100. ????????//?获取天气实况??
  101. ????????weatherCurrent?=?detail.getProperty(4).toString();??
  102. ????????//?解析今天的天气情况??
  103. ????????String?date?=?detail.getProperty(7).toString();??
  104. ????????weatherToday?=?"今天:"?+?date.split("?")[0];??
  105. ????????weatherToday?=?weatherToday?+?"n天气:"?+?date.split("?")[1];??
  106. ????????weatherToday?=?weatherToday?+?"n气温:"??
  107. ????????????????+?detail.getProperty(8).toString();??
  108. ????????weatherToday?=?weatherToday?+?"n风力:"??
  109. ????????????????+?detail.getProperty(9).toString()?+?"n";??
  110. ????????iconToday[0]?=?parseIcon(detail.getProperty(10).toString());??
  111. ????????iconToday[1]?=?parseIcon(detail.getProperty(11).toString());??
  112. ????????//?解析明天的天气情况??
  113. ????????date?=?detail.getProperty(12).toString();??
  114. ????????weatherTomorrow?=?"明天:"?+?date.split("?")[0];??
  115. ????????weatherTomorrow?=?weatherTomorrow?+?"n天气:"?+?date.split("?")[1];??
  116. ????????weatherTomorrow?=?weatherTomorrow?+?"n气温:"??
  117. ????????????????+?detail.getProperty(13).toString();??
  118. ????????weatherTomorrow?=?weatherTomorrow?+?"n风力:"??
  119. ????????????????+?detail.getProperty(14).toString()?+?"n";??
  120. ????????iconTomorrow[0]?=?parseIcon(detail.getProperty(15).toString());??
  121. ????????iconTomorrow[1]?=?parseIcon(detail.getProperty(16).toString());??
  122. ????????//?解析后天的天气情况??
  123. ????????date?=?detail.getProperty(17).toString();??
  124. ????????weatherAfterday?=?"后天:"?+?date.split("?")[0];??
  125. ????????weatherAfterday?=?weatherAfterday?+?"n天气:"?+?date.split("?")[1];??
  126. ????????weatherAfterday?=?weatherAfterday?+?"n气温:"??
  127. ????????????????+?detail.getProperty(18).toString();??
  128. ????????weatherAfterday?=?weatherAfterday?+?"n风力:"??
  129. ????????????????+?detail.getProperty(19).toString()?+?"n";??
  130. ????????iconAfterday[0]?=?parseIcon(detail.getProperty(20).toString());??
  131. ????????iconAfterday[1]?=?parseIcon(detail.getProperty(21).toString());??
  132. ????????//?更新当天的天气实况??
  133. ????????textWeatherCurrent.setText(weatherCurrent);??
  134. ????????//?更新显示今天天气的图标和文本框??
  135. ????????textWeatherToday.setText(weatherToday);??
  136. ????????todayWhIcon1.setImageResource(iconToday[0]);??
  137. ????????todayWhIcon2.setImageResource(iconToday[1]);??
  138. ????????//?更新显示明天天气的图标和文本框??
  139. ????????textWeatherTomorrow.setText(weatherTomorrow);??
  140. ????????tomorrowWhIcon1.setImageResource(iconTomorrow[0]);??
  141. ????????tomorrowWhIcon2.setImageResource(iconTomorrow[1]);??
  142. ????????//?更新显示后天天气的图标和文本框??
  143. ????????textWeatherAfterday.setText(weatherAfterday);??
  144. ????????afterdayWhIcon1.setImageResource(iconAfterday[0]);??
  145. ????????afterdayWhIcon2.setImageResource(iconAfterday[1]);??
  146. ????}??
  147. ??
  148. ????//?工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。??
  149. ????private?int?parseIcon(String?strIcon)?{??
  150. ????????if?(strIcon?==?null)??
  151. ????????????return?-1;??
  152. ????????if?("0.gif".equals(strIcon))??
  153. ????????????return?R.drawable.a_0;??
  154. ????????if?("1.gif".equals(strIcon))??
  155. ????????????return?R.drawable.a_1;??
  156. ????????if?("2.gif".equals(strIcon))??
  157. ????????????return?R.drawable.a_2;??
  158. ????????if?("3.gif".equals(strIcon))??
  159. ????????????return?R.drawable.a_3;??
  160. ????????if?("4.gif".equals(strIcon))??
  161. ????????????return?R.drawable.a_4;??
  162. ????????if?("5.gif".equals(strIcon))??
  163. ????????????return?R.drawable.a_5;??
  164. ????????if?("6.gif".equals(strIcon))??
  165. ????????????return?R.drawable.a_6;??
  166. ????????if?("7.gif".equals(strIcon))??
  167. ????????????return?R.drawable.a_7;??
  168. ????????if?("8.gif".equals(strIcon))??
  169. ????????????return?R.drawable.a_8;??
  170. ????????if?("9.gif".equals(strIcon))??
  171. ????????????return?R.drawable.a_9;??
  172. ????????if?("10.gif".equals(strIcon))??
  173. ????????????return?R.drawable.a_10;??
  174. ????????if?("11.gif".equals(strIcon))??
  175. ????????????return?R.drawable.a_11;??
  176. ????????if?("12.gif".equals(strIcon))??
  177. ????????????return?R.drawable.a_12;??
  178. ????????if?("13.gif".equals(strIcon))??
  179. ????????????return?R.drawable.a_13;??
  180. ????????if?("14.gif".equals(strIcon))??
  181. ????????????return?R.drawable.a_14;??
  182. ????????if?("15.gif".equals(strIcon))??
  183. ????????????return?R.drawable.a_15;??
  184. ????????if?("16.gif".equals(strIcon))??
  185. ????????????return?R.drawable.a_16;??
  186. ????????if?("17.gif".equals(strIcon))??
  187. ????????????return?R.drawable.a_17;??
  188. ????????if?("18.gif".equals(strIcon))??
  189. ????????????return?R.drawable.a_18;??
  190. ????????if?("19.gif".equals(strIcon))??
  191. ????????????return?R.drawable.a_19;??
  192. ????????if?("20.gif".equals(strIcon))??
  193. ????????????return?R.drawable.a_20;??
  194. ????????if?("21.gif".equals(strIcon))??
  195. ????????????return?R.drawable.a_21;??
  196. ????????if?("22.gif".equals(strIcon))??
  197. ????????????return?R.drawable.a_22;??
  198. ????????if?("23.gif".equals(strIcon))??
  199. ????????????return?R.drawable.a_23;??
  200. ????????if?("24.gif".equals(strIcon))??
  201. ????????????return?R.drawable.a_24;??
  202. ????????if?("25.gif".equals(strIcon))??
  203. ????????????return?R.drawable.a_25;??
  204. ????????if?("26.gif".equals(strIcon))??
  205. ????????????return?R.drawable.a_26;??
  206. ????????if?("27.gif".equals(strIcon))??
  207. ????????????return?R.drawable.a_27;??
  208. ????????if?("28.gif".equals(strIcon))??
  209. ????????????return?R.drawable.a_28;??
  210. ????????if?("29.gif".equals(strIcon))??
  211. ????????????return?R.drawable.a_29;??
  212. ????????if?("30.gif".equals(strIcon))??
  213. ????????????return?R.drawable.a_30;??
  214. ????????if?("31.gif".equals(strIcon))??
  215. ????????????return?R.drawable.a_31;??
  216. ????????return?0;??
  217. ????}??
  218. }??

帮助文件



[java]? view plain copy print ?
  1. package?com.example.weatherservicedemo;??
  2. ??
  3. import?java.io.IOException;??
  4. import?java.util.ArrayList;??
  5. import?java.util.List;??
  6. ??
  7. import?org.ksoap2.SoapEnvelope;??
  8. import?org.ksoap2.serialization.SoapObject;??
  9. import?org.ksoap2.serialization.SoapSerializationEnvelope;??
  10. import?org.ksoap2.transport.HttpTransportSE;??
  11. import?org.xmlpull.v1.XmlPullParserException;??
  12. ??
  13. /**?
  14. ?*?Description:?<br/>?
  15. ?*?网站:?<a?href="http://www.crazyit.org">疯狂Java联盟</a>?<br/>?
  16. ?*?Copyright?(C),?Yeeku.H.Lee?<br/>?
  17. ?*?This?program?is?protected?by?copyright?laws.?<br/>?
  18. ?*?Program?Name:?<br/>?
  19. ?*?Date:?
  20. ?*??
  21. ?*?@author?Yeeku.H.Lee?kongyeeku@163.com?
  22. ?*?@version?1.0?
  23. ?*/??
  24. public?class?WebServiceUtil?{??
  25. ????//?定义Web?Service的命名空间??
  26. ????static?final?String?SERVICE_NS?=?"http://WebXml.com.cn/";??
  27. ????//?定义Web?Service提供服务的URL??
  28. ????static?final?String?SERVICE_URL?=?"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";??
  29. ??
  30. ????//?调用远程Web?Service获取省份列表??
  31. ????public?static?List<String>?getProvinceList()?{??
  32. ????????//?调用的方法??
  33. ????????String?methodName?=?"getRegionProvince";??
  34. ????????//?创建HttpTransportSE传输对象??
  35. ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
  36. ????????ht.debug?=?true;??
  37. ????????//?使用SOAP1.1协议创建Envelop对象??
  38. ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
  39. ????????????????SoapEnvelope.VER11);??
  40. ????????//?实例化SoapObject对象??
  41. ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
  42. ????????envelope.bodyOut?=?soapObject;??
  43. ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
  44. ????????envelope.dotNet?=?true;??
  45. ????????try?{??
  46. ????????????//?调用Web?Service??
  47. ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
  48. ????????????if?(envelope.getResponse()?!=?null)?{??
  49. ????????????????//?获取服务器响应返回的SOAP消息??
  50. ????????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
  51. ????????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
  52. ????????????????????????+?"Result");??
  53. ????????????????//?解析服务器响应的SOAP消息。??
  54. ????????????????return?parseProvinceOrCity(detail);??
  55. ????????????}??
  56. ????????}?catch?(IOException?e)?{??
  57. ????????????e.printStackTrace();??
  58. ????????}?catch?(XmlPullParserException?e)?{??
  59. ????????????e.printStackTrace();??
  60. ????????}??
  61. ????????return?null;??
  62. ????}??
  63. ??
  64. ????//?根据省份获取城市列表??
  65. ????public?static?List<String>?getCityListByProvince(String?province)?{??
  66. ????????//?调用的方法??
  67. ????????String?methodName?=?"getSupportCityString";??
  68. ????????//?创建HttpTransportSE传输对象??
  69. ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
  70. ????????ht.debug?=?true;??
  71. ????????//?实例化SoapObject对象??
  72. ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
  73. ????????//?添加一个请求参数??
  74. ????????soapObject.addProperty("theRegionCode",?province);??
  75. ????????//?使用SOAP1.1协议创建Envelop对象??
  76. ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
  77. ????????????????SoapEnvelope.VER11);??
  78. ????????envelope.bodyOut?=?soapObject;??
  79. ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
  80. ????????envelope.dotNet?=?true;??
  81. ????????try?{??
  82. ????????????//?调用Web?Service??
  83. ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
  84. ????????????if?(envelope.getResponse()?!=?null)?{??
  85. ????????????????//?获取服务器响应返回的SOAP消息??
  86. ????????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
  87. ????????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
  88. ????????????????????????+?"Result");??
  89. ????????????????//?解析服务器响应的SOAP消息。??
  90. ????????????????return?parseProvinceOrCity(detail);??
  91. ????????????}??
  92. ????????}?catch?(IOException?e)?{??
  93. ????????????e.printStackTrace();??
  94. ????????}?catch?(XmlPullParserException?e)?{??
  95. ????????????e.printStackTrace();??
  96. ????????}??
  97. ????????return?null;??
  98. ????}??
  99. ??
  100. ????private?static?List<String>?parseProvinceOrCity(SoapObject?detail)?{??
  101. ????????ArrayList<String>?result?=?new?ArrayList<String>();??
  102. ????????for?(int?i?=?0;?i?<?detail.getPropertyCount();?i++)?{??
  103. ????????????//?解析出每个省份??
  104. ????????????result.add(detail.getProperty(i).toString().split(",")[0]);??
  105. ????????}??
  106. ????????return?result;??
  107. ????}??
  108. ??
  109. ????public?static?SoapObject?getWeatherByCity(String?cityName)?{??
  110. ????????String?methodName?=?"getWeather";??
  111. ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
  112. ????????ht.debug?=?true;??
  113. ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
  114. ????????????????SoapEnvelope.VER11);??
  115. ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
  116. ????????soapObject.addProperty("theCityCode",?cityName);??
  117. ????????envelope.bodyOut?=?soapObject;??
  118. ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
  119. ????????envelope.dotNet?=?true;??
  120. ????????try?{??
  121. ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
  122. ????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
  123. ????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
  124. ????????????????????+?"Result");??
  125. ????????????return?detail;??
  126. ????????}?catch?(Exception?e)?{??
  127. ????????????e.printStackTrace();??
  128. ????????}??
  129. ????????return?null;??
  130. ????}??
  131. }??

适配器文件



[java]? view plain copy print ?
  1. package?com.example.weatherservicedemo;??
  2. ??
  3. import?java.util.List;??
  4. import?android.content.Context;??
  5. import?android.graphics.Color;??
  6. import?android.view.View;??
  7. import?android.view.ViewGroup;??
  8. import?android.widget.BaseAdapter;??
  9. import?android.widget.TextView;??
  10. ??
  11. /**?
  12. ?*?Description:?<br/>?
  13. ?*?网站:?<a?href="http://www.crazyit.org">疯狂Java联盟</a>?<br/>?
  14. ?*?Copyright?(C),?Yeeku.H.Lee?<br/>?
  15. ?*?This?program?is?protected?by?copyright?laws.?<br/>?
  16. ?*?Program?Name:?<br/>?
  17. ?*?Date:?
  18. ?*??
  19. ?*?@author?Yeeku.H.Lee?kongyeeku@163.com?
  20. ?*?@version?1.0?
  21. ?*/??
  22. public?class?ListAdapter?extends?BaseAdapter?{??
  23. ????private?Context?context;??
  24. ????private?List<String>?values;??
  25. ??
  26. ????public?ListAdapter(Context?context,?List<String>?values)?{??
  27. ????????this.context?=?context;??
  28. ????????this.values?=?values;??
  29. ????}??
  30. ??
  31. ????public?int?getCount()?{??
  32. ????????return?values.size();??
  33. ????}??
  34. ??
  35. ????public?Object?getItem(int?position)?{??
  36. ????????return?values.get(position);??
  37. ????}??
  38. ??
  39. ????public?long?getItemId(int?position)?{??
  40. ????????return?position;??
  41. ????}??
  42. ??
  43. ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  44. ????????TextView?text?=?new?TextView(context);??
  45. ????????text.setText(values.get(position));??
  46. ????????text.setTextSize(20);??
  47. ????????text.setTextColor(Color.BLACK);??
  48. ????????return?text;??
  49. ????}??
  50. }??

还有访问网络的权限呀,事先说明一下,这个只有在wif下运行成功,移动网络运行失败,不知道怎么回事,有明白的,给哥们呢我留个言,大恩不言谢呀。



[html]? view plain copy print ?
  1. <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  2. ????package="com.example.weatherservicedemo"??
  3. ????android:versionCode="1"??
  4. ????android:versionName="1.0"?>??
  5. ??
  6. ????<uses-sdk?android:minSdkVersion="8"?/>??
  7. ??
  8. ????<uses-permission?android:name="android.permission.INTERNET"?/>??
  9. ????<uses-permission?android:name="android.permission.ACCESS_NETWORK_STATE"?/>??
  10. ????<uses-permission?android:name="android.permission.READ_PHONE_STATE"?/>??
  11. ??
  12. ????<application??
  13. ????????android:icon="@drawable/ic_launcher"??
  14. ????????android:label="@string/app_name"??
  15. ????????android:theme="@style/AppTheme"?>??
  16. ????????<activity??
  17. ????????????android:name=".MyWeather"??
  18. ????????????android:label="@string/title_activity_main"?>??
  19. ????????????<intent-filter>??
  20. ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  21. ??
  22. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  23. ????????????</intent-filter>??
  24. ????????</activity>??
  25. ????</application>??
  26. ??
  27. </manifest>??

后面我会把源代码上传,因为还有一个架包没办法贴出来。也可以百度搜索一下,ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar架包


运行的时候可能会有no class not found 的错误,说找不到那个类文件,告诉你一个解决的办法,如下图

也可以看这篇文章解决---http://blog.csdn.net/wang6279026/article/details/8098100

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读