AIR:加载应用程序文件夹外部的本地 xml 文件
我正在构建一个 AIR 应用程序,它将用于编辑另一个应用程序的幻灯片(也是由我构建的,但不是在 AIR 中)。幻灯片和其他数据在多个 xml 文件中定义,这些文件位于其他应用程序的程序文件夹中。当尝试将这些文件加载到我的 AIR 应用程序中时,我收到以下错误:
fault(mx.messaging.messages::ErrorMessage)#0
body = ""
clientId = "DirectHTTPChannel0"
correlationId = "D67CFDDA-2A2E-9D8B-21DE-7D9FB4B72167"
destination = ""
extendedData = (null)
faultCode = "Server.Error.Request"
faultDetail = "Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: app:/Users/path/to/folder/languages.xml" errorID=2032]. URL: /Users/path/to/folder/languages.xml"
faultString = "HTTP request error"
headers = (Object)#1
DSStatusCode = 0
messageId = "F54EACC8-22F3-3718-9106-7D9FB4E0649C"
rootCause = (flash.events::IOErrorEvent)#2
bubbles = false
cancelable = false
currentTarget = (flash.net::URLLoader)#3
bytesLoaded = 0
bytesTotal = 0
data = ""
dataFormat = "text"
errorID = 2032
eventPhase = 2
target = (flash.net::URLLoader)#3
text = "Error #2032: Stream Error. URL: app:/Users/path/to/folder/languages.xml"
type = "ioError"
timestamp = 0
timeToLive = 0
这就是我的编码方式:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
this.languages.addEventListener(FaultEvent.FAULT, this.faultHandler);
this.languages.addEventListener(ResultEvent.RESULT, resultHandler);
this.languages.send();
}
private function faultHandler(event:FaultEvent):void
{
trace("fault" + event.message);
}
private function resultHandler(event:ResultEvent):void
{
trace("success" + event.result);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<s:HTTPService id="languages" url="/Users/path/to/folder/languages.xml"/>
</fx:Declarations>
</s:WindowedApplication>
如何从这些文件中读取内容?除了本地文件之外,应用程序不需要与任何其他内容进行通信。
感谢J_A_X的回答,我将我的creationCompleteHandler更改为:
var file:File = new File("/Users/path/to/folder/languages.xml");
var fs:FileStream = new FileStream();
fs.open(file, FileMode.READ);
var text:String = fs.readUTFBytes(file.size);
trace(text);
这就像一个魅力。
I'm building an AIR application which will be used to edit slideshows for another app (also built by me, but not in AIR). The slideshows and other data are defined in several xml files which are in the other application's program folder. When trying to load these into my AIR app I get the following error:
fault(mx.messaging.messages::ErrorMessage)#0
body = ""
clientId = "DirectHTTPChannel0"
correlationId = "D67CFDDA-2A2E-9D8B-21DE-7D9FB4B72167"
destination = ""
extendedData = (null)
faultCode = "Server.Error.Request"
faultDetail = "Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: app:/Users/path/to/folder/languages.xml" errorID=2032]. URL: /Users/path/to/folder/languages.xml"
faultString = "HTTP request error"
headers = (Object)#1
DSStatusCode = 0
messageId = "F54EACC8-22F3-3718-9106-7D9FB4E0649C"
rootCause = (flash.events::IOErrorEvent)#2
bubbles = false
cancelable = false
currentTarget = (flash.net::URLLoader)#3
bytesLoaded = 0
bytesTotal = 0
data = ""
dataFormat = "text"
errorID = 2032
eventPhase = 2
target = (flash.net::URLLoader)#3
text = "Error #2032: Stream Error. URL: app:/Users/path/to/folder/languages.xml"
type = "ioError"
timestamp = 0
timeToLive = 0
This is how I've coded it:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
this.languages.addEventListener(FaultEvent.FAULT, this.faultHandler);
this.languages.addEventListener(ResultEvent.RESULT, resultHandler);
this.languages.send();
}
private function faultHandler(event:FaultEvent):void
{
trace("fault" + event.message);
}
private function resultHandler(event:ResultEvent):void
{
trace("success" + event.result);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<s:HTTPService id="languages" url="/Users/path/to/folder/languages.xml"/>
</fx:Declarations>
</s:WindowedApplication>
How can I read from these files? The app will have no need to communicate with anything but the local files.
Thanks to J_A_X's answer, I changed my creationCompleteHandler to:
var file:File = new File("/Users/path/to/folder/languages.xml");
var fs:FileStream = new FileStream();
fs.open(file, FileMode.READ);
var text:String = fs.readUTFBytes(file.size);
trace(text);
which worked like a charm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTPService 用于 HTTP 通信。获取本地文件不是 HTTP。您想要查看文件类来打开/读取您的 xml。
An HTTPService is used for HTTP communication. Getting a local file isn't HTTP. You want to look at the File class to open/read your xml.