2009年4月6日 星期一

[Flex] 用Flex讀取繁體中文編碼的資料

出處:http://blog.tmu.edu.tw/cyhuang_4/004594.html

Flex裡就有可以處理的方法,
就是用 flash.utils.ByteArray class 內的方法:
readMultiByte(length:uint, charSet:String):String

以下是我做的兩個練習:

練習一:讀入繁體中文編碼的外部TXT檔

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadText()">

<mx:Script>
<![CDATA[
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;

public function loadText():void {
var request:URLRequest = new URLRequest("http://localhost/big5toutf8/big5.txt");
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);
}

private function completeHandler(event:Event):void {
var loader2:URLLoader = URLLoader(event.target);
var ba:ByteArray = ByteArray(URLLoader(event.target).data);
var utf8Str:String = ba.readMultiByte(ba.length, "big5");
big5text.text = URLLoader(event.target).data.toString();
big5textUtf8.text = utf8Str;
}
]]>
</mx:Script>


<mx:VBox horizontalCenter="0" top="120" horizontalAlign="center" id="vbox1">
<mx:HBox id="hbox1">
<mx:Panel width="250" height="200" layout="absolute" title="直接從外部TXT檔案讀取(big5)" id="panel1">
<mx:Text x="10" y="10" width="210" height="140" id="big5text"/>
</mx:Panel>
<mx:Panel width="250" height="200" layout="absolute" title="將字串在Flex 2里面處理之後(utf8)" id="panel2">
<mx:Text x="10" y="10" width="210" height="140" id="big5textUtf8"/>
</mx:Panel>
</mx:HBox>
<mx:Button label="下一個練習"/>
</mx:VBox>
</mx:Application>


練習二:讀入繁體中文編碼的外部PHP檔
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="loadText()">


<mx:Script>
<![CDATA[
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;

public function loadPHP():void {
var request:URLRequest = new URLRequest();
var loader:URLLoader = new URLLoader();
var variables:URLVariables = new URLVariables("username=admin&password=admin");
request.url = "http://localhost/~mac/big5toutf8/test.php";
request.method = URLRequestMethod.POST;
request.data = variables;
loader.dataFormat = URLLoaderDataFormat.BINARY;

loader.addEventListener(Event.COMPLETE, completeHandlerPHP);
loader.load(request);
}

private function completeHandlerPHP(event:Event):void {
var loader2:URLLoader = URLLoader(event.target);
var ba:ByteArray = ByteArray(URLLoader(event.target).data);
var utf8Str:String = ba.readMultiByte(ba.length, "big5");
var xmlObj:XML = new XML(utf8Str);
xmlStr1.text = URLLoader(event.target).data.toString();
xmlStr2.text = utf8Str;
dg1.dataProvider = xmlObj.message;
}
]]>
</mx:Script>

<mx:VBox horizontalCenter="0" top="120" horizontalAlign="center" id="vbox1">
<mx:Panel width="500" height="150" layout="absolute" title="直接從外部PHP檔案讀取進來(big5)">
<mx:Text x="10" y="10" width="460" height="90" id="xmlStr1"/>
</mx:Panel>
<mx:Panel width="500" height="150" layout="absolute" title="將字串在Flex 2里面處理之後(utf8)">
<mx:Text x="10" y="10" width="460" height="90" id="xmlStr2"/>
</mx:Panel>
<mx:Panel width="500" height="200" layout="absolute" title="將字串轉成XML物件,並指定給DataGrid當作dataProvider" id="panel3">
<mx:DataGrid x="10" y="10" width="460" height="140" id="dg1" variableRowHeight="true" wordWrap="true">
<mx:columns>
<mx:DataGridColumn headerText="Message" dataField="text"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
<mx:HBox width="100%" horizontalAlign="center">
<mx:Button label="讀取PHP檔" click="loadPHP()"/>
<mx:Button label="上一個練習"/>
</mx:HBox>
</mx:VBox>
</mx:Application>

【下列文章您可能也有興趣】

沒有留言: