对刚上手Flex的新手来说,或多或少都会碰到这样一个问题:Flex默认编码是utf-8,而往往我们要读取的数据库内容是gb2312编码的。这里提供了一个我封装的类,对URLLoader的封装,省去了事件的使用,编码的切换,更是简单。仅供参考

 

package logic
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	public class WebClient
	{
		protected var loader:URLLoader;
		private var request:URLRequest;
		private var _method:String;
		private var _svrUrl:String;
		private var callBack:Function;
		private var errCallBack:Function;
		private var _contentType:String;
		private var _charset:String;
		private var _resultFormat:String;
		
		public function WebClient()
		{
			this._charset="utf-8"; //默认编码utf-8
			this.loader=new URLLoader();
			this.loader.dataFormat=URLLoaderDataFormat.BINARY;
			this._method="POST";	//默认请求方式POST
			this._contentType="application/x-www-form-urlencoded";
			// 提供两种结果类型,默认文本。如果为binary,则不存在编码转换之分
			this._resultFormat="text";	//"binary"
			
			this.loader.addEventListener(Event.COMPLETE,dataLoaded);
			this.loader.addEventListener(IOErrorEvent.IO_ERROR,loadError);
		}
		
		protected function dataLoaded(e:Event):void
		{
			if(this.callBack!=null){
				if(this._resultFormat=="text")
				{
					var buf:ByteArray=this.loader.data as ByteArray;
					this.callBack(buf.readMultiByte(buf.length,this._charset));
				}
				else
				{
					this.callBack(this.loader.data);
				}
				this.callBack=null;
			}
		}
		
		protected function loadError(e:IOErrorEvent):void
		{
			if(this.errCallBack==null) return;
			this.errCallBack(e);
			this.errCallBack=null;
		}
		
		public function get resultFormat():String
		{
			return this._resultFormat;
		}
		public function set resultFormat(value:String):void
		{
			this._resultFormat=value;
		}
		
		public function get charset():String
		{
			return this._charset;
		}
		public function set charset(value:String):void
		{
			this._charset=value;
		}
		
		public function get method():String
		{
			return this._method;
		}
		
		public function set method(m:String):void
		{
			this._method=m;
		}
		
		public function get svrUrl():String
		{
			return this._svrUrl;
		}
		
		public function set svrUrl(url:String):void
		{
			this._svrUrl=url;
		}
		
		public function setHeader(header:String, value:String):void
		{
		}
		
		public function get contentType():String{
			return this._contentType;
		}
		public function set contentType(ct:String):void{
			this._contentType=ct;
		}
		
		public function call(callback:Function, arg:Object=null,errCallback:Function=null):void
		{
			this.callBack=callback;
			this.errCallBack=errCallback;
			
			this.request=new URLRequest(this._svrUrl);
			this.request.method=this._method;
			this.request.contentType=this._contentType;
			this.request.data=arg;
			
			this.loader.load(this.request);
		}
	}
}

 

 

使用示例:

 

1 <?xml version="1.0" encoding="utf-8"?>
2  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
3 xmlns:s="library://ns.adobe.com/flex/spark"
4 xmlns:mx="library://ns.adobe.com/flex/mx"
5 width="100%" height="100%">
6 <s:layout>
7 <s:BasicLayout/>
8 </s:layout>
9
10 <fx:Script>
11 <![CDATA[
12 import logic.WebClient;
13
14 import mx.controls.Alert;
15
16 private function load():void{
17 var file:Object=this.ddlFile.selectedItem;
18 var encode:Object=this.ddlEncode.selectedItem;
19
20 if(file==null || encode==null){
21 Alert.show("请选择文件和编码");
22 return
23 }
24 var client:WebClient=new WebClient();
25 client.svrUrl=file.url;
26 client.charset=encode.value;
27 client.method="GET";
28 client.call(dataLoaded);
29 }
30
31 private function dataLoaded(rv:*):void{
32 this.tbResult.text=rv;
33 }
34 ]]>
35 </fx:Script>
36
37 <fx:Declarations>
38 <!-- 将非可视元素(例如服务、值对象)放在此处 -->
39 </fx:Declarations>
40 <s:Label x="10" y="14" text="文件"/>
41 <s:DropDownList id="ddlFile"
42 y="10" left="42" right="243"
43 labelField="text">
44 <s:dataProvider>
45 <s:ArrayList>
46 <fx:Object text="gb2312文件"
47 url="https://files.cnblogs.com/shice/gb2312.js" />
48 <fx:Object text="utf-8文件"
49 url="https://files.cnblogs.com/shice/utf-8.js" />
50 </s:ArrayList>
51 </s:dataProvider>
52 </s:DropDownList>
53 <s:Label y="15" text="编码" right="211"/>
54 <s:DropDownList id="ddlEncode"
55 y="10" right="88" width="115"
56 labelField="text">
57 <s:dataProvider>
58 <s:ArrayList>
59 <fx:Object text="gb2312编码"
60 value="gb2312" />
61 <fx:Object text="utf-8编码"
62 value="utf-8" />
63 </s:ArrayList>
64 </s:dataProvider>
65 </s:DropDownList>
66 <s:Button y="10" label="加载" right="10" click="load()"/>
67 <s:Label x="10" y="41" text="返回结果"/>
68 <s:TextArea id="tbResult" left="10" top="61" bottom="10" right="10"/>
69  </s:Application>

示例Flex:

posted on 2011-01-13 09:04  蓝色_冰点  阅读(1902)  评论(0)    收藏  举报