【问题标题】:How to handle security error and time out error of UrlLoader.load() in actionscript3?如何处理actionscript3中UrlLoader.load()的安全错误和超时错误?
【发布时间】:2011-10-06 04:56:32
【问题描述】:

我在我的 Flash 应用程序中使用 UrlLoader.load()
当发生未捕获的异常时,我正在处理 UncaughtErrorEvent.UNCAUGHT_ERROR 以停止应用程序。

UrlLoader.load() 在您正常连接互联网时工作。
但是,如果您的浏览器加载应用程序后与互联网的连接丢失, SecurityError 在调用 UrlLoader.load() 时发生。

我无法使用 try catch 捕获 SecurityError 并且发生了 UNCAUGHT_ERROR 并且它停止了我的应用程序。

我不想在UrlLoader.load() 失败时停止应用程序,因为我只是使用UrlLoader.load() 记录一些不重要的信息。

而且我认为如果加载时间过长也会出现超时错误。
而且我也不想因为超时错误而停止我的应用程序。

我该如何解决这些问题?
还有更多其他类型的错误会发生并停止我的应用程序吗?

【问题讨论】:

  • 未捕获的异常是什么意思,因为它总是会发现任何类型的异常或错误,然后它将停止您的应用程序。如果你想防止安全错误,那么你应该尝试 IOErrorEvent.IO_ERROR。你能说清楚未捕获的异常吗?
  • @SwatiSingh 感谢您的评论。未捕获的异常只是停止进程而不是应用程序。应用程序只是停止响应用户输入,用户无法理解应用程序是否停止。因此,我通过清除所有对象来停止应用程序,并在发生 UncaughtErrorEvent.UNCAUGHT_ERROR 时在屏幕上显示错误消息。如果我不通过处理 UncaughtErrorEvent.UNCAUGHT_ERROR 来停止应用程序,则 UrlLoader.load() 的 SecurityError 将被忽略,并且应用程序正常继续,因为 UrlLoader.load() 是异步函数并且不会影响应用程序的主进程。

标签: actionscript-3 exception-handling urlloader uncaught-exception


【解决方案1】:

发生某种类型的安全违规时会引发 SecurityError 异常。

安全错误示例:

An unauthorized property access or method call is made across a security sandbox boundary.
An attempt was made to access a URL not permitted by the security sandbox.
A socket connection was attempted to an unauthorized port number, e.g. a port above 65535.
An attempt was made to access the user’s camera or microphone, and the request to access the device was denied by the user.

假设我们必须从任何外部 URL 加载 swf:

    // URL of the external movie content
    var myRequest:URLRequest=new URLRequest("glow2.swf");

    // Create a new Loader to load the swf files
    var myLoader:Loader=new Loader();

    // 1st level IO_ERROR input and output error checking
    // Listen error events for the loading process
    myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);

    function ioError(event:ErrorEvent):void 
    {
      // Display error message to user in case of loading error.
         output_txt.text = "Sorry that there is an IO error during the loading of  an  
                            external movie. The error is:" + "\n" + event;
    }

    function checkComplete(evt:MouseEvent) 
    {
      // 2nd level SecurityError error checking
      // Use the try-catch block
      try
      {
         // Load the external movie into the Loader
         myLoader.load(myRequest);
      }
      catch (error:SecurityError) 
      {
         // catch the error here if any
         // Display error message to user in case of loading error.
            output_txt.text = "Sorry that there is a Security error during the 
                               loading of an external movie. The error is:" + "\n" +
                               error;
      }
    }

 movie1_btn.addEventListener(MouseEvent.CLICK, checkComplete);

 // Listen when the loading of movie (glow.swf) is completed
 myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMovie1);

 function loadMovie1(myEvent:Event):void 
 {
     // Display the Loader on the MainTimeline when the loading is completed
     addChild(myLoader);
     // Set display location of the Loader
     myLoader.x = 200;
     myLoader.y = 80;
 }

希望这对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多