package com.yarin.android.CityWeather;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
/**
<xml_api_reply version="1">
-<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
-<forecast_information>
<city data="Chengdu, Sichuan"/>
<postal_code data="chengdu"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2009-08-18"/>
<current_date_time data="2009-08-19 00:00:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
-<current_conditions>
<condition data="多云"/>
<temp_f data="88"/>
<temp_c data="31"/>
<humidity data="湿度: 58%"/>
<icon data="/ig/images/weather/cn_cloudy.gif"/>
<wind_condition data="风向: 东、风速:4 米/秒"/>
</current_conditions>
-<forecast_conditions>
<day_of_week data="周二"/>
<low data="24"/>
<high data="33"/>
<icon data="/ig/images/weather/chance_of_rain.gif"/>
<condition data="可能有雨"/>
</forecast_conditions>
-<forecast_conditions>
<day_of_week data="周三"/>
<low data="21"/>
<high data="31"/>
<icon data="/ig/images/weather/cn_heavyrain.gif"/>
<condition data="雨"/>
</forecast_conditions>
-<forecast_conditions>
<day_of_week data="周四"/>
<low data="19"/>
<high data="29"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="晴间多云"/>
</forecast_conditions>
-<forecast_conditions>
<day_of_week data="周五"/>
<low data="21"/>
<high data="31"/>
<icon data="/ig/images/weather/chance_of_rain.gif"/>
<condition data="可能有雨"/>
</forecast_conditions>
</weather>
</xml_api_reply>
*/
public class GoogleWeatherHandler extends DefaultHandler
{
//天气信息
private WeatherSet myWeatherSet = null;
//实时天气信息
private boolean is_Current_Conditions = false;
//预报天气信息
private boolean is_Forecast_Conditions = false;
private final String CURRENT_CONDITIONS = "current_conditions";
private final String FORECAST_CONDITIONS = "forecast_conditions";
public GoogleWeatherHandler()
{
}
//返回天气信息对象
public WeatherSet getMyWeatherSet()
{
return myWeatherSet;
}
@Override
public void endDocument() throws SAXException
{
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException
{
if (localName.equals(CURRENT_CONDITIONS))
{
this.is_Current_Conditions = false;
}
else if (localName.equals(FORECAST_CONDITIONS))
{
this.is_Forecast_Conditions = false;
}
}
@Override
public void startDocument() throws SAXException
{
this.myWeatherSet = new WeatherSet();
}
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
{
if (localName.equals(CURRENT_CONDITIONS))
{//实时天气
Log.i("localName+CURRENT", localName);
this.myWeatherSet.setMyCurrentCondition(new WeatherCurrentCondition());
Log.i("localName+CURRENT+1", localName);
this.is_Current_Conditions = true;
}
else if (localName.equals(FORECAST_CONDITIONS))
{//预报天气
this.myWeatherSet.getMyForecastConditions().add(new WeatherForecastCondition());
this.is_Forecast_Conditions = true;
}
else
{
//分别将得到的信息设置到指定的对象中
if (localName.equals(CURRENT_CONDITIONS))
{
Log.i("localName+CURRENT", localName);
}
String dataAttribute = attributes.getValue("data");
if (localName.equals("icon"))
{
if (this.is_Current_Conditions)
{
this.myWeatherSet.getMyCurrentCondition().setIcon(dataAttribute);
}
else if (this.is_Forecast_Conditions)
{
this.myWeatherSet.getLastForecastCondition().setIcon(dataAttribute);
}
}
else if (localName.equals("condition"))
{
if (this.is_Current_Conditions)
{
this.myWeatherSet.getMyCurrentCondition().setCondition(dataAttribute);
}
else if (this.is_Forecast_Conditions)
{
this.myWeatherSet.getLastForecastCondition().setCondition(dataAttribute);
}
}
else if (localName.equals("temp_c"))
{
this.myWeatherSet.getMyCurrentCondition().setTemp_celcius(dataAttribute);
}
else if (localName.equals("temp_f"))
{
this.myWeatherSet.getMyCurrentCondition().setTemp_fahrenheit(dataAttribute);
}
else if (localName.equals("humidity"))
{
this.myWeatherSet.getMyCurrentCondition().setHumidity(dataAttribute);
}
else if (localName.equals("wind_condition"))
{
this.myWeatherSet.getMyCurrentCondition().setWind_condition(dataAttribute);
}// Tags is forecast_conditions
else if (localName.equals("day_of_week"))
{
this.myWeatherSet.getLastForecastCondition().setDay_of_week(dataAttribute);
}
else if (localName.equals("low"))
{
this.myWeatherSet.getLastForecastCondition().setLow(dataAttribute);
}
else if (localName.equals("high"))
{
this.myWeatherSet.getLastForecastCondition().setHigh(dataAttribute);
}
}
}
@Override
public void characters(char ch[], int start, int length)
{
/*
* Would be called on the following structure:
* <element>characters</element>
*/
}
}
小程序 城市天气预报源码.rar
版权申诉
40 浏览量
2023-02-03
11:42:25
上传
评论
收藏 219KB RAR 举报
大富大贵7
- 粉丝: 313
- 资源: 8869
最新资源
- 钢琴块2 v3.1.0.1054 Mod.apk
- 蓝桥杯第十一届省赛(第一场)
- 隐马尔可夫实践(生物序列)
- CLShanYanSDKDataList.sqlite
- 2024年度乐材教育春季高中数学教师专业测试答案.docx
- 选择题-数组&类I.docx
- cn-msdn-library-for-visual-studio-2008-service-pack-1-x86-dvd-x1
- cn-msdn-library-for-visual-studio-2008-service-pack-1-x86-dvd-x1
- cn-msdn-library-for-visual-studio-2008-service-pack-1-x86-dvd-x1
- Screenshot_20240517_181056.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈