package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.display.MovieClip;
import flash.net.*;
import flash.utils.ByteArray;
/**
* 获得天气预报情况以及生活指数
* ...
* @author
*/
public class WeatherService
{
public var weatherXML:XML = new XML();
/**
*根据城市名查找天气
* theCityName:城市名
*/
public function WeatherService(theCityName:String)
{
var cityName:String = urlencodeGB2312(theCityName);
var url:String = "http://php.weather.sina.com.cn/xml.php?city="+cityName+"&password=DJOYnieT8234jlsK&day=0";
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = url;
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener("complete", completeHandler);
urlLoader.addEventListener("IoError", ioErrorHandler);
}
private function completeHandler(e:Event):void
{
//及时释放掉
e.target.removeEventListener("complete", completeHandler);
e.target.removeEventListener("ioError", ioErrorHandler);
// trace(e.target.data);
var xml:XML = XML(e.target.data);
weatherXML = XML(xml.Weather);
//trace(weatherXML);
showWeather();
}
private function ioErrorHandler(e):void
{
e.target.removeEventListener("complete", completeHandler);
e.target.removeEventListener("ioError", ioErrorHandler);
}
//获得天气的所有信息
public function showWeather()
{
trace(getHighTemp() + "===" + getLowTemp() + "===" + getTemp_state1() + "===" + getTemp_state2());
trace(getWind_Direct1() + "===" + getWind_Direct2() + "===" + getWind_Power1() + "===" + getWind_Power2() + "===" + getSomatosensory_Temp());
trace(getultraviolet() + "===" + getAirconditioning_index() + "===" + getPollution() + "===" + getCar_Wash());
trace(getDress() + "===" + getCold() + "===" + getSport());
}
//获得当天最高气温
public function getHighTemp():String
{
return weatherXML.child("temperature1");
}
//获得当天最低气温
public function getLowTemp():String
{
return weatherXML.child("temperature2");
}
//获得当天天气状况1
public function getTemp_state1():String
{
return weatherXML.child("status1");
}
//获得当天天气状况2
public function getTemp_state2():String
{
return weatherXML.child("status2");
}
//获得当天天气风向1
public function getWind_Direct1():String
{
return weatherXML.child("direction1");
}
//获得当天天气风向2
public function getWind_Direct2():String
{
return weatherXML.child("direction2");
}
//获得当天天气风力1
public function getWind_Power1():String
{
return weatherXML.child("power1");
}
//获得当天天气风力2
public function getWind_Power2():String
{
return weatherXML.child("power2");
}
//获得体感温度
public function getSomatosensory_Temp():String
{
return weatherXML.child("tgd1");
}
//获得紫外线指数
public function getultraviolet():String
{
return weatherXML.child("zwx_l");
}
//获得空调指数
public function getAirconditioning_index():String
{
return weatherXML.child("ktk_l");
}
//获得污染指数
public function getPollution():String
{
return weatherXML.child("pollution_s");
}
//获得洗车指数
public function getCar_Wash():String
{
return weatherXML.child("xcz_l");
}
//获得穿衣指数
public function getDress():String
{
return weatherXML.child("chy_l");
}
//获得感冒指数
public function getCold():String
{
return weatherXML.child("gm_l");
}
//获得运动指数
public function getSport():String
{
return weatherXML.child("yd_l");
}
//string转换成gb2312类型
public function urlencodeGB2312(str:String):String
{
var result:String ="";
var byte:ByteArray =new ByteArray();
//如果需要转换成其他的类型,如gbk,big5直接把gb2312改成gbk或者big5就行了
byte.writeMultiByte(str, "gb2312");
for(var i:int;i<byte.length;i++){
result += escape(String.fromCharCode(byte[i]));
}
return result;
}
}
}
//主文件代码
var ws:WeatherService=new WeatherService("宁波");