开发者

How to Send Multiple Addresses to URLRequest

I'm using the weather.com XML service to retrieve forecast data. I created an instance of UrlLoader and passed the entire HTTP address including credentials, location and other parameters to it. This works very well in tandem with the XML(urlLoader.data) functionality in that I get the result for the specified location ID. I then use E4X to trace just the nodes needed. This is all good.

The tricky part is that I have to do this for 19 additional cities. I've done the initial work in the Main class, Main function, private function onXmlLoaded. I tried creating a new public function getCityName making the new URLRequest, tracing XML and adding values to my components. However, when I attempt this, I receive:

Error 5006: An ActionScript file cannot have more than one externally visible definition.

How can I easily setup the URLRequest to request data for all 20 cities, and parse the results thereafter? Do I pass an array of location IDs to URLRequest, because that's the only part of the HTTP request that's different. Do I create separate ActionScript files with their own class and functions for each of the 20 queries?

Here's the code:

public class Main extends MovieClip
{
    public function Main()
    {
        var urlRequest:URLRequest = new URLRequest("http://xoap.weather.com/weather/local/ASXX0274?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a");
        var urlLoader:URLLoader=new URLLoader();

        urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
        urlLoader.load(urlRequest);
    }
    public function onXmlLoaded(event:Event): void 
    {
        XML.ignoreWhitespace = true;
        var urlLoader:URLLoader = event.currentTarget as URLLoader;
        var resultXML:XML = XML(urlLoader.data);
        trace(resultXML.dayf.day.hi);
        trace(resultXML.dayf.day.low);
        trace(resultXML.dayf.day.part.(@p=="d").icon);

        lbllowtemp.text= String(resultXML.dayf.day.low);
        lblhitemp.text=String(resultXML.dayf.day.hi);
        uicondicon.source=String(resultXML.dayf.day.part.(@p=="d").icon)+".png";

        var tf:TextFormat = new TextFormat;
        tf.color=0xFFFFFF;
        tf.font="Arial";
        tf.size=16;
        tf.bold=true;
        lbllowtemp.setStyle("textFormat",tf);
        lblhitemp.setStyle("textFormat",tf);
        }

    public function getCanberra()
    {
        var urlRequest:URLRequest = new URLRequest("http://xoap.weather.com/weather/local/ASXX023?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a");
        var urlLoader:URLLoader=new URLLoader();

        urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
        urlLoader.load(urlRequest);

        XML.ignoreWhitespac开发者_JS百科e = true;
        var urlLoader:URLLoader = event.currentTarget as URLLoader;
        var resultXML:XML = XML(urlLoader.data);
        trace(resultXML.dayf.day.hi);
        trace(resultXML.dayf.day.low);
        trace(resultXML.dayf.day.part.(@p=="d").icon);

        lblCBlowtemp.text= String(resultXML.dayf.day.low);
        lblCBhitemp.text=String(resultXML.dayf.day.hi);
        uiCBcondicon.source=String(resultXML.dayf.day.part.(@p=="d").icon)+".png";

        var tf:TextFormat = new TextFormat;
        tf.color=0xFFFFFF;
        tf.font="Arial";
        tf.size=16;
        tf.bold=true;
        lblCBlowtemp.setStyle("textFormat",tf);
        lblCBhitemp.setStyle("textFormat",tf);
    }
}

When I debug now, I receive error 1120 access of undefined property event in my getCanberra function on the line:

var urlLoader:URLLoader = event.currentTarget as URLLoader;


Just loop.
Obviously change links for your needs

var weather:Weather = new Weather( );


package {    
  public class Weather{
    public var aVars:Array = new Array();
    aVars[0] = "http://xoap.weather.com/weather/local/ASXX0274?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a"
    aVars[1] = "http://xoap.weather.com/weather/local/ASXX0274?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a"
    aVars[2] = "http://xoap.weather.com/weather/local/ASXX0274?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a"
    aVars[3] = "http://xoap.weather.com/weather/local/ASXX0274?dayf=1&link=xoap&par=1253350999&key=9a25cc224522c34a"

    public function Weather() {
      for( var i:int = 0; i<aVars.length; i++){
        var urlRequest:URLRequest=new URLRequest(aVars[i]);
        var urlLoader:URLLoader=new URLLoader();
        urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
        urlLoader.load(urlRequest);
      }
    }
    public function onXmlLoaded(event:Event):void {
      XML.ignoreWhitespace=true;
      var urlLoader:URLLoader=event.currentTarget as URLLoader;
      var resultXML:XML=XML(urlLoader.data);
      trace(resultXML.dayf.day.hi);
      trace(resultXML.dayf.day.low);
      trace(resultXML.dayf.day.part.(@p=="d").icon);

      lbllowtemp.text=String(resultXML.dayf.day.low);
      lblhitemp.text=String(resultXML.dayf.day.hi);
      uicondicon.source=String(resultXML.dayf.day.part.(@p=="d").icon)+".png";


      // note that tf has not been added to the display list so will not be visible
      var tf:TextFormat=new TextFormat  ;
      tf.color=0xFFFFFF;
      tf.font="Arial";
      tf.size=16;
      tf.bold=true;
      lbllowtemp.setStyle("textFormat",tf);
      lblhitemp.setStyle("textFormat",tf);
    }

  }
}


I'm pretty sure this error only occurs if you have function defined outside of your class:

For example:

package {    
    public class YourClass {
        public function YourClass() {
            //Constructor
        }

        public function correctFunction():void {
            //No error.
        }
    }
    public function errorFunction():void {
        //will throw 5006 error.
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜