如果文件不存在,则AS3 IOErrorEvent.IO_ERROR事件无法捕获

时间:2015-09-24 22:39:25

标签: actionscript-3

这没有任何意义。我正在尝试加载一个简单的文本文件。如果文件名只是“jeffaberle.txt”,那么它工作正常,因为该文件存在于指定的位置。但是,如果名称更改为“jeffaberle2.txt”,则代码会生成错误,因为服务器上不存在此文件。

如果文件丢失,我希望触发IOErrorEvent,但它不会触发。显然我缺少一些东西。有什么建议吗?

import flash.events.*;
import flash.net.URLRequest;
import flash.net.URLLoader;

var urlRequest:URLRequest = new URLRequest("http://www.postureviewer.com/trials/jeffaberle2.txt" + "?" + Math.random());  // + Math.random()
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

urlLoader.load(urlRequest);

function completeHandler(e:Event):void {
    if (urlLoader.data.trialversion == "true") {
        trace("true");
    } else {
        trace("false");
    }
}
function ioErrorHandler(e:IOErrorEvent):void {
    trace("ioErrorHandlerJeff: " + e.toString());
}

2 个答案:

答案 0 :(得分:0)

您未加载"文件",您正在使用网址访问数据资源。最可能的解决方案是处理HTTPStatus事件

...
urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );

urlLoader.load(urlRequest);

function httpStatusHandler( e:HTTPStatusEvent ) : void
{
    switch ( e.status )
    {
        case 404:
            trace( "Resource missing!" );
            break;
        ...
    }
}

如果流已损坏或连接出现故障,您将依赖IOErrorEvent。

答案 1 :(得分:0)

你和#34; http://www.postureviewer.com/trials/jeffaberle2.txt"之后发生了冲突。返回'数据',但它不是您通过URLLoaderDataFormat.VARIABLES请求的名称/值对形式。

为了触发" IOErrorEvent"删除URLLoaderDataFormat.VARIABLES行,以便将http错误(在本例中为404)抛出到 ioErrorHandler 。然后在 completeHandler 中,您可以尝试将收到的数据转换为名称/值对。在try / catch中执行此操作将允许您从php脚本中捕获错误的数据:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonViewGroup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">

<Button
    android:id="@+id/startButton"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:textSize="20sp"
    android:background="@drawable/basic_button"
    android:text="Start" />

    </LinearLayout>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">

<Button
    android:id="@+id/sendButton"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/basic_button"
    android:text="Prepare Attack!" />

</LinearLayout>