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>
*/
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Weather--Android.rar (35个子文件)
CityWeather
AndroidManifest.xml 774B
res
layout
main.xml 4KB
drawable
bg.jpg 2KB
icon.png 7KB
values
strings.xml 332B
src
com
yarin
android
CityWeather
CityWeather.java 4KB
WeatherCurrentCondition.java 2KB
GoogleWeatherHandler.java 5KB
WeatherForecastCondition.java 1KB
SingleWeatherInfoView.java 2KB
WeatherSet.java 1KB
ConstData.java 2KB
bin
com
yarin
android
ChinaWeather
CityWeather
CityWeather.class 5KB
R$color.class 453B
ConstData.class 3KB
WeatherForecastCondition.class 2KB
R.class 667B
CityWeather$2.class 2KB
SingleWeatherInfoView.class 2KB
R$layout.class 427B
R$attr.class 373B
R$id.class 1KB
WeatherCurrentCondition.class 2KB
R$string.class 516B
CityWeather$1.class 2KB
GoogleWeatherHandler.class 4KB
WeatherSet.class 1KB
R$drawable.class 459B
classes.dex 17KB
resources.ap_ 11KB
CityWeather.apk 20KB
.classpath 280B
assets
default.properties 449B
.project 848B
gen
com
yarin
android
ChinaWeather
CityWeather
R.java 2KB
共 35 条
- 1
资源评论
- Sundy_李2012-10-02api屏蔽了。。。这类都不能用了
- lsqdata2013-06-06代码写的倒是没错,但是无法使用,可能人家已经屏蔽了api吧
xiaomxiaom1991
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功