基于安卓WebServicw天气预报demo
来源:互联网 发布:iphone当前网络不可用 编辑:程序博客网 时间:2024/06/05 21:17
UI设计
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/province" /> <Spinner android:id="@+id/province" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/city" /> <Spinner android:id="@+id/city" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <!-- 显示今日天气的图片和文本框 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/todayWhIcon1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/todayWhIcon2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/weatherToday" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <!-- 显示明天天气的图片和文本框 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/tomorrowWhIcon1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/tomorrowWhIcon2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/weatherTomorrow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <!-- 显示后天天气的图片和文本框 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/afterdayWhIcon1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/afterdayWhIcon2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/weatherAfterday" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <TextView android:id="@+id/weatherCurrent" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
ListAdapter
package com.example.dashu_weather;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;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;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn values.size();}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn values.get(arg0);}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {TextView text = new TextView(context);text.setText(values.get(arg0));text.setTextSize(20);text.setTextColor(Color.GREEN);return text;}}
WebServiceUtil
package com.example.dashu_weather;import java.util.ArrayList;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.FutureTask;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;public class WebServiceUtil {// 定义Web Service命名空间static final String SERVICE_NS = "http://WebXml.com.cn/";// Web Services服务的URLstatic final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";/** * 调用远程Web Service获取省份列表 * */public static List<String> getProvinceList() {// 调用Wbe Services服务的方法名final String methodName = "getRegionProvince";// 创建HttpTransportSE传输对象,该对象用于调用wbe servicesfinal HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);ht.debug = true;// 使用SOAP1.1协议创建envelope对象,该对象是HttpTransportSE调用web service信息载体final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// 实例化SoapObject对象,参数命名空间和调用的方法名字SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);// 客户端通过SoapSerializationEnvelope类对象的envelope.bodyOut把客户端要输入的参数传给服务器,即soapObject对象envelope.bodyOut = soapObject;// 设置与.net提供的web services保持良好的兼容性envelope.dotNet = true;FutureTask<List<String>> task = new FutureTask<List<String>>(new Callable<List<String>>() {@Overridepublic List<String> call() throws Exception {// 调用web serviceht.call(SERVICE_NS + methodName, envelope);if (envelope.getResponse() != null) {// 获取服务器响应返回的SOAP消息SoapObject result = (SoapObject) envelope.bodyIn;SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");// 解析服务器响应的消息return parseProvinceOrCity(detail);}return null;}});new Thread(task).start();try {return task.get();} catch (Exception e) {e.printStackTrace();}return null;}/** * 获取省份城市列表 * */public static List<String> getCityListByProvince(String province) {// 调用Wbe Services服务的方法名final String methodName = "getSupportCityString";// 创建HttpTransportSE传输对象,该对象用于调用wbe servicesfinal HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);ht.debug = true;// 使用SOAP1.1协议创建envelope对象,该对象是HttpTransportSE调用web service信息载体final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// 实例化SoapObject对象,参数命名空间和调用的方法名字SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);// 添加一个请求参数soapObject.addProperty("theRegionCode", province);// 客户端通过SoapSerializationEnvelope类对象的envelope.bodyOut把客户端要输入的参数传给服务器,即soapObject对象envelope.bodyOut = soapObject;// 设置与.net提供的web services保持良好的兼容性envelope.dotNet = true;FutureTask<List<String>> task = new FutureTask<List<String>>(new Callable<List<String>>() {@Overridepublic List<String> call() throws Exception {// 调用web serviceht.call(SERVICE_NS + methodName, envelope);if (envelope.getResponse() != null) {// 获取服务器响应返回的SOAP消息SoapObject result = (SoapObject) envelope.bodyIn;SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");// 解析服务器响应的消息return parseProvinceOrCity(detail);}return null;}});new Thread(task).start();try {return task.get();} catch (Exception 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) {final String methodName = "getWeather";final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);ht.debug = true;final 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;FutureTask<SoapObject> task = new FutureTask<SoapObject>(new Callable<SoapObject>() {@Overridepublic SoapObject call() throws Exception {ht.call(SERVICE_NS + methodName, envelope);SoapObject result = (SoapObject) envelope.bodyIn;SoapObject detail = (SoapObject) result.getProperty(methodName + "Result");return detail;}});new Thread(task).start();try {return task.get();} catch (Exception e) {e.printStackTrace();}return null;}}
MainActivity
package com.example.dashu_weather;import java.util.List;import com.example.dashu_weather.R;import com.example.dashu_weather.WebServiceUtil;import org.ksoap2.serialization.SoapObject;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ImageView;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends Activity {private Spinner provinceSpinner;private Spinner citySpinner;private ImageView todayWhIcon1;private ImageView todayWhIcon2;private TextView textWeatherToday;private ImageView tomorrowWhIcon1;private ImageView tomorrowWhIcon2;private TextView textWeatherTomorrow;private ImageView afterdayWhIcon1;private ImageView afterdayWhIcon2;private TextView textWeatherAfterday;private TextView textWeatherCurrent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);todayWhIcon1 = (ImageView) findViewById(R.id.todayWhIcon1);todayWhIcon2 = (ImageView) findViewById(R.id.todayWhIcon2);textWeatherToday = (TextView) findViewById(R.id.weatherToday);tomorrowWhIcon1 = (ImageView) findViewById(R.id.tomorrowWhIcon1);tomorrowWhIcon2 = (ImageView) findViewById(R.id.tomorrowWhIcon2);textWeatherTomorrow = (TextView) findViewById(R.id.weatherTomorrow);afterdayWhIcon1 = (ImageView) findViewById(R.id.afterdayWhIcon1);afterdayWhIcon2 = (ImageView) findViewById(R.id.afterdayWhIcon2);textWeatherAfterday = (TextView) findViewById(R.id.weatherAfterday);textWeatherCurrent = (TextView) findViewById(R.id.weatherCurrent);// 获取程序界面中选择省份、城市的Spinner组件provinceSpinner = (Spinner) findViewById(R.id.province);citySpinner = (Spinner) findViewById(R.id.city);// 调用远程Web Service获取省份列表List<String> provinces = WebServiceUtil.getProvinceList();ListAdapter adapter = new ListAdapter(this, provinces);// 使用Spinner显示省份列表provinceSpinner.setAdapter(adapter);// 当省份Spinner的选择项被改变时provinceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {List<String> cities = WebServiceUtil.getCityListByProvince(provinceSpinner.getSelectedItem().toString());ListAdapter cityAdapter = new ListAdapter(MainActivity.this,cities);// 使用Spinner显示城市列表citySpinner.setAdapter(cityAdapter);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});citySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubshowWeather(citySpinner.getSelectedItem().toString());}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});}private void showWeather(String city){String weatherToday = null;String weatherTomorrow = null;String weatherAfterday = null;String weatherCurrent = null;int iconToday[] = new int[2];int iconTomorrow[] = new int[2];int iconAfterday[] = new int[2];// 获取远程Web Service返回的对象SoapObject detail = WebServiceUtil.getWeatherByCity(city);// 获取天气实况weatherCurrent = detail.getProperty(4).toString();// 解析今天的天气情况String date = detail.getProperty(7).toString();weatherToday = "明天:" + date.split(" ")[0];weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];weatherToday = weatherToday + "\n气温:"+ detail.getProperty(8).toString();weatherToday = weatherToday + "\n风力:"+ detail.getProperty(9).toString() + "\n";iconToday[0] = parseIcon(detail.getProperty(10).toString());iconToday[1] = parseIcon(detail.getProperty(11).toString());// 解析明天的天气情况date = detail.getProperty(12).toString();weatherTomorrow = "后天:" + date.split(" ")[0];weatherTomorrow = weatherTomorrow + "\n天气:" + date.split(" ")[1];weatherTomorrow = weatherTomorrow + "\n气温:"+ detail.getProperty(13).toString();weatherTomorrow = weatherTomorrow + "\n风力:"+ detail.getProperty(14).toString() + "\n";iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());// 解析后天的天气情况date = detail.getProperty(17).toString();weatherAfterday = "大后天:" + date.split(" ")[0];weatherAfterday = weatherAfterday + "\n天气:" + date.split(" ")[1];weatherAfterday = weatherAfterday + "\n气温:"+ detail.getProperty(18).toString();weatherAfterday = weatherAfterday + "\n风力:"+ detail.getProperty(19).toString() + "\n";iconAfterday[0] = parseIcon(detail.getProperty(20).toString());iconAfterday[1] = parseIcon(detail.getProperty(21).toString());// 更新当天的天气实况textWeatherCurrent.setText(weatherCurrent);// 更新显示今天天气的图标和文本框textWeatherToday.setText(weatherToday);todayWhIcon1.setImageResource(iconToday[0]);todayWhIcon2.setImageResource(iconToday[1]);// 更新显示明天天气的图标和文本框textWeatherTomorrow.setText(weatherTomorrow);tomorrowWhIcon1.setImageResource(iconTomorrow[0]);tomorrowWhIcon2.setImageResource(iconTomorrow[1]);// 更新显示后天天气的图标和文本框textWeatherAfterday.setText(weatherAfterday);afterdayWhIcon1.setImageResource(iconAfterday[0]);afterdayWhIcon2.setImageResource(iconAfterday[1]);}// 工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源ID。private int parseIcon(String strIcon){if (strIcon == null)return -1;if ("0.gif".equals(strIcon))return R.drawable.a_0;if ("1.gif".equals(strIcon))return R.drawable.a_1;if ("2.gif".equals(strIcon))return R.drawable.a_2;if ("3.gif".equals(strIcon))return R.drawable.a_3;if ("4.gif".equals(strIcon))return R.drawable.a_4;if ("5.gif".equals(strIcon))return R.drawable.a_5;if ("6.gif".equals(strIcon))return R.drawable.a_6;if ("7.gif".equals(strIcon))return R.drawable.a_7;if ("8.gif".equals(strIcon))return R.drawable.a_8;if ("9.gif".equals(strIcon))return R.drawable.a_9;if ("10.gif".equals(strIcon))return R.drawable.a_10;if ("11.gif".equals(strIcon))return R.drawable.a_11;if ("12.gif".equals(strIcon))return R.drawable.a_12;if ("13.gif".equals(strIcon))return R.drawable.a_13;if ("14.gif".equals(strIcon))return R.drawable.a_14;if ("15.gif".equals(strIcon))return R.drawable.a_15;if ("16.gif".equals(strIcon))return R.drawable.a_16;if ("17.gif".equals(strIcon))return R.drawable.a_17;if ("18.gif".equals(strIcon))return R.drawable.a_18;if ("19.gif".equals(strIcon))return R.drawable.a_19;if ("20.gif".equals(strIcon))return R.drawable.a_20;if ("21.gif".equals(strIcon))return R.drawable.a_21;if ("22.gif".equals(strIcon))return R.drawable.a_22;if ("23.gif".equals(strIcon))return R.drawable.a_23;if ("24.gif".equals(strIcon))return R.drawable.a_24;if ("25.gif".equals(strIcon))return R.drawable.a_25;if ("26.gif".equals(strIcon))return R.drawable.a_26;if ("27.gif".equals(strIcon))return R.drawable.a_27;if ("28.gif".equals(strIcon))return R.drawable.a_28;if ("29.gif".equals(strIcon))return R.drawable.a_29;if ("30.gif".equals(strIcon))return R.drawable.a_30;if ("31.gif".equals(strIcon))return R.drawable.a_31;return 0;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
manifest加网络访问权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dashu_weather" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dashu_weather.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <!-- 网络访问权限 --> <uses-permission android:name="android.permission.INTERNET" > </uses-permission></manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">天气预报</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="btn_apply">查询</string> <string name="text_hint">城市中文名</string> <string name="province">省份</string> <string name="city">城市</string></resources>
0 0
- 基于安卓WebServicw天气预报demo
- 安卓 天气预报
- 安卓实现天气预报功能
- 基于MVC开发模式的天气预报小Demo
- 指南针安卓demo
- 安卓定位Demo
- 安卓 闹钟demo
- 安卓的demo
- 安卓指纹识别demo
- 安卓指纹识别demo
- 安卓Web Service实现天气预报功能
- 安卓-天气预报(逐渐整理)
- Android天气预报 简单Demo
- iOS天气预报Demo
- 天气预报小Demo
- Android获取天气预报Demo
- 基于安卓客户端的百度云消息推送实现DEMO
- 极光推送---安卓Demo
- 实用xcode5
- 自反ACL总结
- 代码执行覆盖率工具Emma、cobertura简单比较
- 南洋理工学院--括号匹配问题(2题号)
- JSP应用的自我理解之三:JSP+Servlet实现表单验证
- 基于安卓WebServicw天气预报demo
- c/c++笔试题整理
- 经典sql
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法
- C#数据类型
- 南洋理工学院(8题号)————一种排序
- visual studio对于未赋值的int型的处理
- 图像分割___图像分割方法综述
- 【java 大数】hdu java 大数