flash as3 与webservice通信制作天气预报代码

经常制作一些带有天气预报的flash,所以,干脆把相关代码提取出来,写成几个文档类。

关于webservice,本人是在是个菜鸟,从网上查的一些资料,加上自己写几句as脚本,马马虎虎可以实现相关功能。先介绍一个网址:http://www.webxml.com.cn/zh_cn/index.aspx。这个是一个免费的资源,可以获取很多有用的webservice服务。当然也有收费项目。付费的功能应该更全一些。

下面先来看看,简单的天气预报调用。(时间仓促,下面的flash仅展现代码的功能,至于它的美观性,大家不要追究了吧。)

首先是,获取IP地址的方式,在上面介绍的web服务中,可以自动获取浏览器所在的IP并根据IP获取所在地。打开http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx。然后调用getGeoIPContext这个函数就可以得到一个XML数据。由于这个函数不需要参数,因此可以直接访问http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext。

我们可以直接采用一个简单的方式加载此xml数据,比如,用URLLoader类,直接加载此网页。代码如下:

public function getCity()
{
cityLoad=new URLLoader();
cityLoad.load(new URLRequest("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getGeoIPContext"));
cityLoad.addEventListener(Event.COMPLETE, xmlLoaded);
cityLoad.addEventListener(IOErrorEvent.IO_ERROR,ioErrorEventHandler);

}

加载完毕,解析数据,得出我们所需要的内容:


private function xmlLoaded(e:Event):void
{
cityLoad.removeEventListener(Event.COMPLETE, xmlLoaded);
dress_xml = XML(cityLoad.data);
trace(dress_xml);
ip=dress_xml.children()[0];
cityname=dress_xml.children()[1].split("省")[1].split("市")[0];
trace(ip)
trace(cityname)
//获取天气情况;
getweather();
}

private function ioErrorEventHandler(e:Event):void
{
cityLoad.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorEventHandler);
cityLoad.dispatchEvent(new Event("ERRORLOAD"));
}

现在,我们已经得到IP地址和所在的城市,那么我们就可以使用webservice根据所在城市查询其天气状况。webservice调用方法如下:(注:这个web使用方法是本人在网上找的,首先感谢原创作者的分享精神。)


//调用webservice
private function getweather()
{
var ws = new MyWebService("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
ws.addEventListener("callComplete", wsCompleteHandler);
ws.addEventListener("callError", wsIOErrorHandler);

function wsIOErrorHandler(e):void
{
trace(e.eventInfo.method)
e.MyEventerGC();
}

function wsCompleteHandler(e):void
{
listXML= XML(e.eventInfo.data.toString());
showWeather();//在舞台上显示天气
e.MyEventerGC();
}

ws.load("getWeatherbyCityName","theCityName="+cityname);
}

其中有几个自定义类:MyWebService.as,MyGC.as;

MyWebService.as类:

package {
import flash.net.*;
import flash.events.*;

public class MyWebService extends EventDispatcher {

private var wsUrl:String;
private var targetmethodName:String;

public function MyWebService(url:String):void {
wsUrl = url;
}
public function load(methodName:String, ...args):void {
//trace(methodName);
targetmethodName = methodName;
var param:String="";
//trace(args.length)
for (var i:int = 0; i<args.length; i++) {
if (param!="") {
param += "&"+args[i];
} else {
param = args[i];
}
}
//trace("param::"+param);
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest();
urlLoader.addEventListener("complete", completeHandler);
urlLoader.addEventListener("ioError", ioerrorHandler);
urlRequest.url = wsUrl + "/" + methodName;
urlRequest.method = URLRequestMethod.POST;
urlRequest.requestHeaders.push(new URLRequestHeader("Content-Type", "application/x-www-form-urlencoded"));
urlRequest.data = param;
urlLoader.load(urlRequest);
//args = null;
//MyGC.GC();
}
private function completeHandler(e:Event):void {
e.target.removeEventListener("complete", completeHandler);
e.target.removeEventListener("ioError", ioerrorHandler);
//urlLoader = null;
//urlRequest = null;
//MyGC.GC();
//trace(XML(e.target.data).toString());
MyGC.GC();
dispatchEvent(new MyEventer("callComplete", {method:targetmethodName,data:e.target.data}));
}

private function ioerrorHandler(e:IOErrorEvent):void {

e.target.removeEventListener("complete", completeHandler);
e.target.removeEventListener("ioError", ioerrorHandler);
//urlLoader = null;
MyGC.GC();
//urlRequest = null;
//MyGC.GC();
//trace(e)
dispatchEvent(new MyEventer("callError", {method:targetmethodName,data:null}));
}
}
}

import flash.events.*;
import flash.net.*;
class MyEventer extends Event {
private var info:Object;
public function MyEventer(type:String, info:Object, bubbles:Boolean = false, cancelable:Boolean = false):void {
super(type, bubbles, cancelable);
this.info = info;
}
public function get eventInfo():Object {
return this.info;
}
public function MyEventerGC():void {
this.info = null;
MyGC.GC();
}
public override function toString():String {
return formatToString("Event:","type","bubbles","cancelable","eventInfo");
}
}
MyGC.as类:

package
{
import flash.net.LocalConnection;
//import flash.system.System;
public class MyGC
{
public static function GC()
{
//trace("内存begin:"+System.totalMemory);
try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
} catch (e:*) {}
//trace("内存 end:"+System.totalMemory);
}
}
}

获取到相应的天气数据是xml格式的,自己解析下显示所需要的数据就可以了。

private function showWeather()
{
var _xml:XML=listXML;
ip_txt.text="当前IP地址为:"+ip;
city_txt.text="所在城市:"+cityname;
var kqdj:String=_xml.children()[10].split(":")[6].split(";")[0];

weather1_txt.text=_xml.children()[6].split(" ")[1];
day1_txt.text="温度:"+_xml.children()[5].toString()+"\n"+"风向:"+_xml.children()[7].toString()+"\n"+"空气等级:"+kqdj;
weather2_txt.text=_xml.children()[13].split(" ")[1];
day2_txt.text="温度:"+_xml.children()[12].toString()+"\n"+"风向:"+_xml.children()[14].toString()+"\n"+"空气等级:"+kqdj;
weather3_txt.text=_xml.children()[18].split(" ")[1];
day3_txt.text="温度:"+_xml.children()[17].toString()+"\n"+"风向:"+_xml.children()[19].toString()+"\n"+"空气等级:"+kqdj;
var load1:LoadPic=new LoadPic("http://www.webxml.com.cn/images/weather/b_"+_xml.children()[8]);
load1.loader.addEventListener("LoadPicComplete",addpic1);
function addpic1(e:Event):void
{
addChild(load1.bitmap)
load1.bitmap.x=400;
load1.bitmap.y=83.5;
}
var load2:LoadPic=new LoadPic("http://www.webxml.com.cn/images/weather/b_"+_xml.children()[15]);
load2.loader.addEventListener("LoadPicComplete",addpic2);
function addpic2(e:Event):void
{
addChild(load2.bitmap)
load2.bitmap.x=400;
load2.bitmap.y=224.5;
}
var load3:LoadPic=new LoadPic("http://www.webxml.com.cn/images/weather/b_"+_xml.children()[20]);
load3.loader.addEventListener("LoadPicComplete",addpic3);
function addpic3(e:Event):void
{
addChild(load3.bitmap)
load3.bitmap.x=400;
load3.bitmap.y=355.5;
}
}
 

 

你可能感兴趣的:(webservice)