Flex中数据交换(httpservice)

在这部分,你将学习到怎么去创建一个程序,他们将取回基于给用户提供的信息的XML和纯文本运输数据。你将用FlexBuilder,预编译Flex组件,和FlexHTTP服务去创建这个程序。

学习要点:
学习怎么发送参数到服务器端程序,作为纯文本或者XML取回结果

相关该例子的Flex信息
下面的列表概述了相关这个例子的Flex技术要点
1. Flex程序是SWF文件
2. Flex是以编程为主要方式去创建基于Flash的RIA
3. Flex程序用Flash Player9展现
4. 像所有的RIA一样,FlexSWF文件是在客户端处理的,而不是在服务器端处理的。
5. Flex连接像ColdFusion,PHP,ASP。NET和Java这样的服务器端程序。
6. 你可以通过HTTP取回纯文本或者XML数据
7. 你可以通过web服务取回SOAP信息
8. 你可以用LiveCycle数据服务和Java远程对象(POJOs, JavaBeans, EJBs and ColdFusion Components) 交互

代码文件:
这部分为运输费用Flex程序提供了完整的代码。
Application overview


种类 1: HTTPService 返回纯文本
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute"
    backgroundColor="#FFFFFF"
    backgroundAlpha="0"
    backgroundImage="" >
	<mx:Script>
		<![CDATA[
		import mx.rpc.events.ResultEvent;
		import mx.rpc.events.FaultEvent;
		import mx.controls.Alert;

	        public function handlePlain(event:ResultEvent):void
	        {
	            shippingOptions.htmlText = event.result.toString();
	        }

	        public function handleFault(event:FaultEvent):void
	        {
	           Alert.show(event.fault.faultString, "Error");
	        }
		]]>
	</mx:Script>

	<mx:HTTPService result="handlePlain(event);" fault="handleFault(event);" id="plainRPC" resultFormat="text"
	    url="http://examples.adobe.com/flex3/exchangingdata/text/plainHttpService.php"
	    useProxy="false">
	    <mx:request xmlns="">
	        <zipcode>{zipcode.text}</zipcode>
	        <pounds>{weight_lb.text}</pounds>
	    </mx:request>
	</mx:HTTPService>

	<mx:Label x="56" y="32" text="Zip Code" width="55" height="18" textAlign="right" fontWeight="bold"/>
	<mx:Label x="56" y="58" text="Weight" width="55" height="18" textAlign="right" fontWeight="bold"/>
	<mx:TextInput x="130" y="32" id="zipcode" width="160" height="22"/>
	<mx:TextInput x="130" y="58" id="weight_lb" width="160" height="22"/>
	<mx:Button x="130" y="95" label="Get Shipping Options" click="plainRPC.send();" width="160" height="22"/>

	<mx:Text x="56" y="150" id="shippingOptions" width="310" height="133" fontWeight="bold"/>

</mx:Application>


种类 2: HTTPService 返回XML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

	<mx:Script>
		<![CDATA[
			import mx.rpc.events.ResultEvent;
			import mx.rpc.events.FaultEvent;
			import mx.controls.Alert;

                           [Bindable]
                           private var shippingInfo:XMLList;

	        public function handleXML(event:ResultEvent):void
            {
                shippingInfo = event.result.option as XMLList;
            }

	        public function handleFault(event:FaultEvent):void
	        {
	           Alert.show(event.fault.faultString, "Error");
	        }
		]]>
	</mx:Script>

    <mx:HTTPService result="handleXML(event);" fault="handleFault(event);" id="xmlRPC" resultFormat="e4x"
    	url="http://examples.adobe.com/flex3app/flex3samples/exchangingdata/xml/xmlHttpService.jsp" useProxy="false">
	    <mx:request xmlns="">
	        <zipcode>{zipcode.text}</zipcode>
	        <pounds>{weight_lb.text}</pounds>
	    </mx:request>
	</mx:HTTPService>

	<mx:Label x="56" y="32" text="Zip Code" width="55" height="18" textAlign="right" fontWeight="bold"/>
	<mx:Label x="56" y="58" text="Weight" width="55" height="18" textAlign="right" fontWeight="bold"/>
	<mx:TextInput x="130" y="32" id="zipcode" width="160" height="22"/>
	<mx:TextInput x="130" y="58" id="weight_lb" width="160" height="22"/>
	<mx:Button x="130" y="95" label="Get Shipping Options" click="xmlRPC.send();" width="160" height="22"/>
	<mx:DataGrid
		dataProvider="{shippingInfo}"
		x="80" y="141" width="262" height="92" id="shippingOptionsList" editable="false" enabled="true">
	    <mx:columns>
	        <mx:DataGridColumn headerText="Service" dataField="service" />
	        <mx:DataGridColumn headerText="Price" dataField="price" />
	    </mx:columns>
	</mx:DataGrid>

</mx:Application>


JSP and JAVA files
Plain text example
PlainHttpService.jsp
<%@page import="quickstart.ShippingCalculator,
                quickstart.ShippingOption,
                java.util.List" %>
<%
    ShippingCalculator calc = new ShippingCalculator();
    List    options;
    int     zipcode;
    double  pounds;

    zipcode = Integer.parseInt(request.getParameter("zipcode"));
    pounds = Double.parseDouble(request.getParameter("pounds"));

    options = calc.getShippingOptions(zipcode, pounds);

    for (int i = 0; i < options.size(); i++) {
        ShippingOption option = (ShippingOption) options.get(i);
%><%= option.getService() %>: <%= option.getPrice() %> USD
<%
    }
%>


XML example
xmlHttpService.jsp
<%@page import="quickstart.ShippingCalculator,
                quickstart.ShippingOption,
                java.util.List" %>

<?xml version="1.0" encoding="utf-8"?>
<options>
<%
    ShippingCalculator calc = new ShippingCalculator();
    List    options;
    int     zipcode;
    double  pounds;

    zipcode = Integer.parseInt(request.getParameter("zipcode"));
    pounds = Double.parseDouble(request.getParameter("pounds"));

    options = calc.getShippingOptions(zipcode, pounds);

    for (int i = 0; i < options.size(); i++) {
        ShippingOption option = (ShippingOption) options.get(i);
%>
    <option>
        <service><%= option.getService() %></service>
        <price><%= option.getPrice() %></price>
    </option>
<%
    }
%>
</options>


ShippingCalculator.java

package quickstart;

import java.util.ArrayList;
import java.util.List;
import java.lang.Math;

public class ShippingCalculator {

    /*  Returns a list of made-up ShippingOptions.
    */
    public List getShippingOptions(int zipcode, double pounds) {

        List        options = new ArrayList();
        double      baseCost;

        baseCost = Math.round(zipcode / 10000) + (pounds * 5);

        options.add(new ShippingOption("Next Day", baseCost * 4));
        options.add(new ShippingOption("Two Day Air", baseCost * 2));
        options.add(new ShippingOption("Saver Ground", baseCost));

        return options;
    }
}


ShippingOption.java
package quickstart;

public class ShippingOption {

    private String  service;
    private double  price;

    public ShippingOption() {
    }

    public ShippingOption(String aService, double aPrice) {
        this.service = aService;
        this.price = aPrice;
    }

    public void setService(String value) {
        this.service = value;
    }

    public void setPrice(double value) {
        this.price = value;
    }

    public String getService() { return this.service; }

    public double getPrice() { return this.price; }
}




你可能感兴趣的:(java,xml,Flex,AIR,ColdFusion)