布局文件
主要activity文件
帮助文件
- package?com.example.weatherservicedemo;??
- ??
- import?java.io.IOException;??
- import?java.util.ArrayList;??
- import?java.util.List;??
- ??
- import?org.ksoap2.SoapEnvelope;??
- import?org.ksoap2.serialization.SoapObject;??
- import?org.ksoap2.serialization.SoapSerializationEnvelope;??
- import?org.ksoap2.transport.HttpTransportSE;??
- import?org.xmlpull.v1.XmlPullParserException;??
- ??
- /**?
- ?*?Description:?<br/>?
- ?*?网站:?<a?href="http://www.crazyit.org">疯狂Java联盟</a>?<br/>?
- ?*?Copyright?(C),?Yeeku.H.Lee?<br/>?
- ?*?This?program?is?protected?by?copyright?laws.?<br/>?
- ?*?Program?Name:?<br/>?
- ?*?Date:?
- ?*??
- ?*?@author?Yeeku.H.Lee?kongyeeku@163.com?
- ?*?@version?1.0?
- ?*/??
- public?class?WebServiceUtil?{??
- ????//?定义Web?Service的命名空间??
- ????static?final?String?SERVICE_NS?=?"http://WebXml.com.cn/";??
- ????//?定义Web?Service提供服务的URL??
- ????static?final?String?SERVICE_URL?=?"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";??
- ??
- ????//?调用远程Web?Service获取省份列表??
- ????public?static?List<String>?getProvinceList()?{??
- ????????//?调用的方法??
- ????????String?methodName?=?"getRegionProvince";??
- ????????//?创建HttpTransportSE传输对象??
- ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
- ????????ht.debug?=?true;??
- ????????//?使用SOAP1.1协议创建Envelop对象??
- ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
- ????????????????SoapEnvelope.VER11);??
- ????????//?实例化SoapObject对象??
- ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
- ????????envelope.bodyOut?=?soapObject;??
- ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
- ????????envelope.dotNet?=?true;??
- ????????try?{??
- ????????????//?调用Web?Service??
- ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
- ????????????if?(envelope.getResponse()?!=?null)?{??
- ????????????????//?获取服务器响应返回的SOAP消息??
- ????????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
- ????????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
- ????????????????????????+?"Result");??
- ????????????????//?解析服务器响应的SOAP消息。??
- ????????????????return?parseProvinceOrCity(detail);??
- ????????????}??
- ????????}?catch?(IOException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?catch?(XmlPullParserException?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?null;??
- ????}??
- ??
- ????//?根据省份获取城市列表??
- ????public?static?List<String>?getCityListByProvince(String?province)?{??
- ????????//?调用的方法??
- ????????String?methodName?=?"getSupportCityString";??
- ????????//?创建HttpTransportSE传输对象??
- ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
- ????????ht.debug?=?true;??
- ????????//?实例化SoapObject对象??
- ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
- ????????//?添加一个请求参数??
- ????????soapObject.addProperty("theRegionCode",?province);??
- ????????//?使用SOAP1.1协议创建Envelop对象??
- ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
- ????????????????SoapEnvelope.VER11);??
- ????????envelope.bodyOut?=?soapObject;??
- ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
- ????????envelope.dotNet?=?true;??
- ????????try?{??
- ????????????//?调用Web?Service??
- ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
- ????????????if?(envelope.getResponse()?!=?null)?{??
- ????????????????//?获取服务器响应返回的SOAP消息??
- ????????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
- ????????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
- ????????????????????????+?"Result");??
- ????????????????//?解析服务器响应的SOAP消息。??
- ????????????????return?parseProvinceOrCity(detail);??
- ????????????}??
- ????????}?catch?(IOException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?catch?(XmlPullParserException?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?null;??
- ????}??
- ??
- ????private?static?List<String>?parseProvinceOrCity(SoapObject?detail)?{??
- ????????ArrayList<String>?result?=?new?ArrayList<String>();??
- ????????for?(int?i?=?0;?i?<?detail.getPropertyCount();?i++)?{??
- ????????????//?解析出每个省份??
- ????????????result.add(detail.getProperty(i).toString().split(",")[0]);??
- ????????}??
- ????????return?result;??
- ????}??
- ??
- ????public?static?SoapObject?getWeatherByCity(String?cityName)?{??
- ????????String?methodName?=?"getWeather";??
- ????????HttpTransportSE?ht?=?new?HttpTransportSE(SERVICE_URL);??
- ????????ht.debug?=?true;??
- ????????SoapSerializationEnvelope?envelope?=?new?SoapSerializationEnvelope(??
- ????????????????SoapEnvelope.VER11);??
- ????????SoapObject?soapObject?=?new?SoapObject(SERVICE_NS,?methodName);??
- ????????soapObject.addProperty("theCityCode",?cityName);??
- ????????envelope.bodyOut?=?soapObject;??
- ????????//?设置与.Net提供的Web?Service保持较好的兼容性??
- ????????envelope.dotNet?=?true;??
- ????????try?{??
- ????????????ht.call(SERVICE_NS?+?methodName,?envelope);??
- ????????????SoapObject?result?=?(SoapObject)?envelope.bodyIn;??
- ????????????SoapObject?detail?=?(SoapObject)?result.getProperty(methodName??
- ????????????????????+?"Result");??
- ????????????return?detail;??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????????return?null;??
- ????}??
- }??
适配器文件
- package?com.example.weatherservicedemo;??
- ??
- import?java.util.List;??
- import?android.content.Context;??
- import?android.graphics.Color;??
- import?android.view.View;??
- import?android.view.ViewGroup;??
- import?android.widget.BaseAdapter;??
- import?android.widget.TextView;??
- ??
- /**?
- ?*?Description:?<br/>?
- ?*?网站:?<a?href="http://www.crazyit.org">疯狂Java联盟</a>?<br/>?
- ?*?Copyright?(C),?Yeeku.H.Lee?<br/>?
- ?*?This?program?is?protected?by?copyright?laws.?<br/>?
- ?*?Program?Name:?<br/>?
- ?*?Date:?
- ?*??
- ?*?@author?Yeeku.H.Lee?kongyeeku@163.com?
- ?*?@version?1.0?
- ?*/??
- public?class?ListAdapter?extends?BaseAdapter?{??
- ????private?Context?context;??
- ????private?List<String>?values;??
- ??
- ????public?ListAdapter(Context?context,?List<String>?values)?{??
- ????????this.context?=?context;??
- ????????this.values?=?values;??
- ????}??
- ??
- ????public?int?getCount()?{??
- ????????return?values.size();??
- ????}??
- ??
- ????public?Object?getItem(int?position)?{??
- ????????return?values.get(position);??
- ????}??
- ??
- ????public?long?getItemId(int?position)?{??
- ????????return?position;??
- ????}??
- ??
- ????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
- ????????TextView?text?=?new?TextView(context);??
- ????????text.setText(values.get(position));??
- ????????text.setTextSize(20);??
- ????????text.setTextColor(Color.BLACK);??
- ????????return?text;??
- ????}??
- }??
还有访问网络的权限呀,事先说明一下,这个只有在wif下运行成功,移动网络运行失败,不知道怎么回事,有明白的,给哥们呢我留个言,大恩不言谢呀。
- <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????package="com.example.weatherservicedemo"??
- ????android:versionCode="1"??
- ????android:versionName="1.0"?>??
- ??
- ????<uses-sdk?android:minSdkVersion="8"?/>??
- ??
- ????<uses-permission?android:name="android.permission.INTERNET"?/>??
- ????<uses-permission?android:name="android.permission.ACCESS_NETWORK_STATE"?/>??
- ????<uses-permission?android:name="android.permission.READ_PHONE_STATE"?/>??
- ??
- ????<application??
- ????????android:icon="@drawable/ic_launcher"??
- ????????android:label="@string/app_name"??
- ????????android:theme="@style/AppTheme"?>??
- ????????<activity??
- ????????????android:name=".MyWeather"??
- ????????????android:label="@string/title_activity_main"?>??
- ????????????<intent-filter>??
- ????????????????<action?android:name="android.intent.action.MAIN"?/>??
- ??
- ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
- ????????????</intent-filter>??
- ????????</activity>??
- ????</application>??
- ??
- </manifest>??
后面我会把源代码上传,因为还有一个架包没办法贴出来。也可以百度搜索一下,ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar架包
运行的时候可能会有no class not found 的错误,说找不到那个类文件,告诉你一个解决的办法,如下图
也可以看这篇文章解决---http://blog.csdn.net/wang6279026/article/details/8098100