flex之HTTPService 对象

注:原创作品,分享为交流学习,转载请注明出处。

     HTTPService对象的使用 不用任何的额外配置,直接访问j2ee的Servlet类。从servlet返回来的数据类型有如下几种:
    object、array、xml、flashvars、text和e4x,默认的设置为object。

下面介绍六种返回类型的实例
第一种:返回类型是e4x(实例是简单计算器)  
   ECMAScript for XML(E4X)是一扩展了ECMAScript(包含ActionScript、DMDScript、JavaScript、JScript等)的编程语言,它增加对 XML 的内在支持。它的目标是在访问XML文档时,提供一种更直观、语法更简洁的的 DOM 接口。它也是处理XML时的一种新的方式。在 E4X 版本之前,通常总是在 Objcet(对象)层次上访问XML,而 E4X 则是把 XML 当做原始数据(primitive)来处理(比如字符、数字和布尔值)。这也意味着更快的访问速度、更好的支持、和使 XML 可以成为程序的一个组块(数据结构)。
e4x实例:
var sales = <sales vendor="John">
     <item type="peas" price="4" quantity="6"/>
     <item type="carrot" price="3" quantity="10"/>
     <item type="chips" price="5" quantity="3"/>
   </sales>;
 
 alert( sales.item.(@type == "carrot").@quantity );
 alert( sales.@vendor );
 for each( var price in sales..@price ) {
   alert( price );
 }


flex页面:
<?xml version="1.0" encoding="utf-8"?>  
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
			   xmlns:s="library://ns.adobe.com/flex/spark"   
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
	
	<!--返回数据的格式resultFormat有几种类型,object、array、xml、flashvars、text和e4x,默认的设置为object。 -->
	<fx:Declarations>  
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->  
		<mx:HTTPService id="sevlet" resultFormat="e4x" result="resultHandler(event)"
						url="http://localhost:8080/myReport/FlexCalculate"
						/>  
	</fx:Declarations>  
	<fx:Script>  
		<![CDATA[  
			import mx.collections.ArrayCollection;  
			import mx.controls.Alert;  
			import mx.rpc.events.ResultEvent;  
			var com:ArrayCollection = new ArrayCollection(["+","-","*","/"]);  
			
			private function calculate():void{  
				this.submit.enabled=false;  
				var param:URLVariables=new URLVariables();  
				if(this.number1.text == "" || this.number2.text == ""){  
					Alert.show("请输入数字");  
					return;  
				}  
				param.number1=this.number1.text;  
				param.number2=this.number2.text;  
				param.opertion=this.myComboBox.text;  
				param.timestamp=(new Date()).toString();  
				this.sevlet.send(param);//  
			}  
			private function resultHandler(event:ResultEvent):void{  
				result.text = event.result.calculateResult  
				this.submit.enabled=true;  
			}  
			private function clean():void{  
				this.number1.text = "";  
				this.number2.text = "";  
				this.myComboBox.text = "+";  
				this.result.text = "";  
			}  
		]]>  
	</fx:Script>  
	<s:Panel width="500" height="300" title="计算器" backgroundColor="#8C7C73" verticalCenter="0" horizontalCenter="0">  
		<mx:VBox>  
			<mx:HBox>  
				<s:TextInput width="150" id="number1"/>    
				<mx:ComboBox id="myComboBox" x="10" y="10" dataProvider="{com}" width="50"/>  
				<s:TextInput width="150" id="number2"/>  
				<s:Button label="=" width="30" id="submit" click="calculate();"/>  
				<s:TextInput width="50" id="result"/>  
			</mx:HBox>  
			<mx:HBox>  
				<s:Button label="计算" width="230" click="calculate();"/>  
				<s:Button label="清除" width="230" click="clean();"/>  
			</mx:HBox>  
		</mx:VBox>  
	</s:Panel>  
</s:Application>  


服务端Servlet:
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FlexCalculate extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("number1:" + request.getParameter("number1"));
		System.out.println("number2:" + request.getParameter("number2"));
		System.out.println("opertion:" + request.getParameter("opertion"));
		int result = 0;
		if (request.getParameter("opertion").equals("+"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					+ Integer.valueOf(request.getParameter("number2"));
		} else if (request.getParameter("opertion").equals("-"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					- Integer.valueOf(request.getParameter("number2"));
		} else if (request.getParameter("opertion").equals("*"))
		{
			result = Integer.valueOf(request.getParameter("number1"))
					* Integer.valueOf(request.getParameter("number2"));
		} else
		{
			result = Integer.valueOf(request.getParameter("number1"))
					/ Integer.valueOf(request.getParameter("number2"));
		}
		System.out.println("result1:" + result);

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=utf-8");
		String para = request.getParameter("username");
		PrintWriter out = response.getWriter();
		out.println("<zpa>");
		out.println("<calculateResult>" + result + "</calculateResult>");
		out.println("</zpa>");
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}
}



第二种:返回类型是list<String>
flex端:
<!--返回数据的格式resultFormat有几种类型,object、array、xml、flashvars、text和e4x,默认的设置为object。 -->
	<fx:Declarations>  
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->  
		<mx:HTTPService id="sevlet" resultFormat="array" result="resultHandler(event)"
						url="http://localhost:8080/myReport/stringListServlet"
						/>  
	</fx:Declarations>  

	this.sevlet.send(param);//  

	private function resultHandler(event:ResultEvent):void{
				trace(event.result);
//				result.text = event.result.calculateResult  
				this.submit.enabled=true;  
	}  


java服务端:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StringListServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		List<String> resultList = new ArrayList<String>();
		resultList.add("chenchaoyang");
		resultList.add("supan");
		resultList.add("pangzi");
		resultList.add("27");
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(resultList);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}
}


你可能感兴趣的:(Flex)