<!--新Google 468x15 横链接单元开始--> <script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 横链接单元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 横链接单元结束-->
<!-- Feedsky FEED发布代码开始 -->
使用RSS订阅本Blog <!-- FEED自动发现标记开始 --> <link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 -->
amf是Adobe独家开发的数据封装格式,Flash可以直接访问,由于它是基于二进制的数据传输,所以相对于xml SOAP、json、webService等基于字符串的数据格式,有数据体积小和效率高的特点。
amf目前有AMF0和AMF3两种格式,flash8以前版本只支持AMF0,flash9支持两种格式。AMF3比AMF0效率更高。
调试环境:
1、JDK 1.5
2、Resin 3.0
3、Flash CS3
4、flex-messaging-core.jar和flex-messaging-common.jar(这两个jar包文件可在Adobe的开源项目
BlazeDS中找到)
ActionScript客户端代码:
- /**
- *...
- *@authorKinglong
- *@version0.1
- */
- packageproject.test{
- importflash.display.*;
- importflash.events.*;
- importflash.net.*;
- importflash.utils.*;
- publicclassTestAMFextendsSprite{
- privatevar_loader:URLLoader;
- publicfunctionTestAMF(){
- trace("TestAMF");
- _loader=newURLLoader();
- _loader.dataFormat=URLLoaderDataFormat.BINARY;
- _loader.addEventListener(Event.COMPLETE,loaderHandler);
- _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,loaderHandler);
- _loader.addEventListener(IOErrorEvent.IO_ERROR,loaderHandler);
- varrequest:URLRequest=newURLRequest("http://localhost:8080/ajax/servlet/test");
- _loader.load(request);
- }
- privatefunctionloaderHandler(event:*):void{
- trace(event.type);
- switch(event.type){
- caseEvent.COMPLETE:
- varstart:Number=getTimer();
- varbyte:ByteArray=_loader.dataasByteArray;
- byte.uncompress();
- varobj:Object=byte.readObject();
- varend:Number=getTimer();
- trace("耗时:"+(end-start)+"毫秒");
- trace(obj.fullName);
- break;
- }
- }
- }
- }
Java服务端代码
- importjava.io.*;
- importjava.util.HashMap;
- importjava.util.Map;
- importjava.util.zip.*;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importflex.messaging.io.*;
- importflex.messaging.io.amf.*;
- publicclassTestServletextendsHttpServlet{
- privatestaticfinallongserialVersionUID=-1079073749217155506L;
- privateSerializationContextcontext=newSerializationContext();
- publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throws
- ServletException,IOException{
- response.setContentType("application/x-gzip-compressed");
- Amf3Outputout=newAmf3Output(context);
- DeflaterOutputStreamstream=newDeflaterOutputStream(newDataOutputStream(response.getOutputStream()));
- out.setOutputStream(stream);
- Mapmap=newHashMap();
- String[]arr="1,2,3,4,5".split(",");
- map.put("datas",arr);
- map.put("userName","kinglong");
- map.put("fullName","金龙工作室");
- map.put("domain","klstudio.com");
- out.writeObject(map);
- stream.finish();
- }
- publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throws
- ServletException,IOException{
- doPost(request,response);
- }
- }
因为AS3中的ByteArray支持zlib数据压缩和解压,所以我在服务端代码里增加数据压缩。AMF3很适合Flash和后台服务端的数据交换,我写了这个范例,希望能给大家一参考。
摘自【http://www.klstudio.com/post/168.html】
<!--Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60横幅广告结束--><!--新Google 468*60横幅广告开始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 创建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60横幅广告结束-->
<!-- Google Reader shared发布代码开始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p%28%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D%29%3Bnew%20GRC"></script><!-- Google Reader shared发布代码结束 -->