[AS3]AMF3+JAVA的调用范例
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 中找到)
Flash客户端代码:
- /**
- * ...
- * @author Kinglong
- * @version 0.1
- */
- package project.test {
- import flash.display.*;
- import flash.events.*;
- import flash.net.*;
- import flash.utils.*;
- public class TestAMF extends Sprite {
- private var _loader:URLLoader;
- public function TestAMF() {
- trace( "TestAMF" );
- _loader = new URLLoader();
- _loader.dataFormat = URLLoaderDataFormat.BINARY;
- _loader.addEventListener(Event.COMPLETE, loaderHandler);
- _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler);
- _loader.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler);
- var request:URLRequest = new URLRequest( "http://localhost:8080/ajax/servlet/test" );
- _loader.load(request);
- }
- private function loaderHandler(event:*): void {
- trace(event.type);
- switch (event.type) {
- case Event.COMPLETE:
- var start:Number=getTimer();
- var byte :ByteArray = _loader.data as ByteArray;
- byte .uncompress();
- var obj:Object = byte .readObject();
- var end:Number=getTimer();
- trace( "耗时:" + (end - start) + "毫秒" );
- trace(obj.fullName);
- break ;
- }
- }
- }
- }
/** * ... * @author Kinglong * @version 0.1 */ package project.test { import flash.display.*; import flash.events.*; import flash.net.*; import flash.utils.*; public class TestAMF extends Sprite { private var _loader:URLLoader; public function TestAMF() { trace("TestAMF"); _loader = new URLLoader(); _loader.dataFormat = URLLoaderDataFormat.BINARY; _loader.addEventListener(Event.COMPLETE, loaderHandler); _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler); _loader.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler); var request:URLRequest = new URLRequest("http://localhost:8080/ajax/servlet/test"); _loader.load(request); } private function loaderHandler(event:*):void { trace(event.type); switch(event.type) { case Event.COMPLETE: var start:Number=getTimer(); var byte:ByteArray = _loader.data as ByteArray; byte.uncompress(); var obj:Object = byte.readObject(); var end:Number=getTimer(); trace("耗时:" + (end - start) + "毫秒"); trace(obj.fullName); break; } } } }
Java服务端代码
- import java.io.*;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.zip.*;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import flex.messaging.io.*;
- import flex.messaging.io.amf.*;
- public class TestServlet extends HttpServlet {
- private static final long serialVersionUID = -1079073749217155506L;
- private SerializationContext context = new SerializationContext();
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws
- ServletException, IOException {
- response.setContentType( "application/x-gzip-compressed" );
- Amf3Output out = new Amf3Output(context);
- DeflaterOutputStream stream = new DeflaterOutputStream( new DataOutputStream(response.getOutputStream()));
- out.setOutputStream(stream);
- Map map = new HashMap();
- 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();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response) throws
- ServletException, IOException {
- doPost(request, response);
- }
- }
import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.zip.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import flex.messaging.io.*; import flex.messaging.io.amf.*; public class TestServlet extends HttpServlet { private static final long serialVersionUID = -1079073749217155506L; private SerializationContext context = new SerializationContext(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/x-gzip-compressed"); Amf3Output out = new Amf3Output(context); DeflaterOutputStream stream = new DeflaterOutputStream(new DataOutputStream(response.getOutputStream())); out.setOutputStream(stream); Map map = new HashMap(); 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(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
因为AS3中的ByteArray支持zlib数据压缩和解压,所以我在服务端代码里增加数据压缩。
AMF3很适合Flash和后台服务端的数据交换,我写了这个范例,希望能给大家一参考。