Flash 新浪天气预报的xml文件 as3.0

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("宁波");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值