将Urlloader放入类中

时间:2013-06-29 22:07:35

标签: actionscript-3

我在google上搜索了不同的页面,我发现了问题,但没有找到解决方案。

我有一个我制作的类,该类名为WebServiceController:

public class WebServiceController 
{
    private var url:String;
    private var post:Object=new Object();
    private var loader:URLLoader = new URLLoader();
    private var postVariable:String="";
    private var getVariable:String="";
    private var Geladen:Boolean=false;

    public function WebServiceController() 
    {   

    }

    public function postUrlData(u:String,p:Object):String
    {
        url=u;
        post=p;

        var urlReq:URLRequest = new URLRequest (url);


        var i:int=0;
        for(var key:String in post) 
        {
           //trace(key +" = " + post[key]);
           if(i==0)
           {
              // urlVars.key = post[key];
               postVariable=postVariable+""+key+"="+post[key];
           }
           else
           {
               //urlVars.key = post[key];
               postVariable=postVariable+"&"+key+"="+post[key];
           }
           i++;
        }
        //trace(postVariable);
        var urlVars:URLVariables = new URLVariables(postVariable);

        //trace(urlVars);

        // Add the variables to the URLRequest
        urlReq.data = urlVars;
        urlReq.method = URLRequestMethod.POST;


        // Add the URLRequest data to a new Loader
        //loader.load(urlReq);

        // Set a listener function to run when completed
        loader.addEventListener(Event.COMPLETE, onLoaderComplete);

        // Set the loader format to variables and post to the PHP
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.load(urlReq); 

        function onLoaderComplete(event:Event):void
        {
            return loader.data;
        }

    }

现在从我的movieclip我调用下一个函数,但它显示未定义:

var wb:WebServiceController = new WebServiceController();
trace(wb.postUrlData(url,post));

我不知道如何解决这个问题。我尝试了不同的东西,但它一直在说未定义。

1 个答案:

答案 0 :(得分:2)

URLLoader.load调用是异步执行的,因此当您尝试在实例化WebServiceController类的类中对其进行跟踪时,尚未返回数据。

要访问父类中的数据,最好的办法是在数据加载后从WebServiceController类调度一个事件并在父类中捕获它。

<强> WebServiceController:

  public function postUrlData(u:String, p:Object):void
  {
      url=u;
      post=p;

      var urlReq:URLRequest = new URLRequest (url);


      var i:int=0;
      for(var key:String in post) 
      {
         //trace(key +" = " + post[key]);
         if(i==0)
         {
            // urlVars.key = post[key];
             postVariable=postVariable+""+key+"="+post[key];
         }
         else
         {
             //urlVars.key = post[key];
             postVariable=postVariable+"&"+key+"="+post[key];
         }
         i++;
      }
      //trace(postVariable);
      var urlVars:URLVariables = new URLVariables(postVariable);

      //trace(urlVars);

      // Add the variables to the URLRequest
      urlReq.data = urlVars;
      urlReq.method = URLRequestMethod.POST;


      // Add the URLRequest data to a new Loader
      //loader.load(urlReq);

      // Set a listener function to run when completed
      loader.addEventListener(Event.COMPLETE, onLoaderComplete);

      // Set the loader format to variables and post to the PHP
      loader.dataFormat = URLLoaderDataFormat.VARIABLES;
      loader.load(urlReq); 
  }

  private function onLoaderComplete(event:Event):void
  { 
      // We can pass the event on like this
      dispatchEvent(event); 
  }

家长班:

public function initWebServiceController():void 
{
  var wb:WebServiceController = new WebServiceController();
  wb.addEventListener(Event.COMPLETE, onWebServiceControllerDataLoaded);
  wb.postUrlData(url, post);
}

private function onWebServiceControllerDataLoaded(event:Event):void
{
  // The event target is the URLLoader instance. We can 
  // access the loaded data via its data property
  trace(URLLoader(event.target).data);  
}