//Invoke the createGatewayConnection to initialize the Flash Remoting //functionality gatewayConnnection = NetServices.createGatewayConnection();
//Bind the Java Object: com.tallan.data.providers.FlashDemo //to a service called tallanDataService. tallanDataService = gatewayConnnection.getService ("com.tallan.data.providers.FlashDemo", this);
//Invoke a method called getReportData on the service named //tallanDataSerevice
function getReportData_Result(result) { //Print the results from the app server to the //debug window trace(result); } 所有的方法调用信息,包括参数和返回值,都在客户端和服务器端以Action Message Format(AMF)的格式传递。AMF是二进制信息格式,通过HTTP以一种类似于SOAP(Simple Object access Protocol)的方式传送。在网络中,AMF信息比SOAP信息要小得多,因而答应函数以更快的速度被调用。Flash Remoting透明地处理Java对象和ActionScript对象之间的所有映射。
public class FlashDemo { public FlashDemo() { } public document.nbspgetReportData() { document.nbspdoc =null; try { System.out.println("calling getReportData"); document.uilder builder = document.uilderFactory.newInstance().newdocument.uilder(); doc= builder.newdocument.);
//Create a record as the root element Element element = doc.createElement("graph"); Element child1=doc.createElement("point"); child1.setAttribute("xvalue", "February"); child1.setAttribute("yvalue", "20"); element.insertBefore(child1, null); doc.appendChild(element); 这个简单的类将返回一个文档对象给Flash客户端解析; 下面的例子完成解析:
//Parse the XML document.nbspreturned by the server, //keeping the results in an array called nodes function parseXML() { //Create a new array called nodes nodes = new Array();
//Initialize the nodes array to //contain all of the children of the reportXML document.BR> nodes = reportXML.nodes;
// childCounter is used as a counter for the child Nodes childCounter = 0;
//childNodes will contain the children of each node childNodes = new Array();
//XAxisvalue will contain the x-axis value to plot xAxisvalue = new Array();
//YAxisvalue will contain the y-axis value of the data sets yAxisvalue = new Array();
//Iterate through the first level children of the XML Doc for (j=0; j<=nodes.length; j++) { //Check if the node Name is report i.e., the data set //belongs to graph if (nodes[j].nodeName == "graph") { //Get the background color of the graph bgcolor = "#000000";//snodes[j].attributes.bgcolor; //Get the caption of the graph caption = nodes[j].attributes.caption; //Get the x-axis name xaxisname = nodes[j].attributes.xaxisname; //Get the y-axis name yaxisname = nodes[j].attributes.yaxisname; //Get the min value of the y-axis yaxisminvalue = nodes[j].attributes.yaxisminvalue; //Get the max value of the y-axis yaxismaxvalue = nodes[j].attributes.yaxismaxvalue; //Now, we get the childNodes childNodes = nodes[j].nodes;
//Iterate through all of the child nodes, pulling out //their x,y values and storing them in arrays
for (k=0; k<=childNodes.length; k++) { //If the node name is Set i.e., a graph data set, then we retrieve //and collect the values if (childNodes[k].nodeName == "point") { //Increment counter childCounter= childCounter+1; //Get the x-axis name xAxisvalue[childCounter] = childNodes[k].attributes.xvalue; //Get the value yAxisvalue[childCounter] = childNodes[k].attributes.yvalue;