中贏網,有我中國贏!
IT頻道
當前位置: 中贏網 » IT頻道 » 編程開發

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
Flex中數據交換(httpservice)種類 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; }
}
聲明:中贏網登載此文出於傳遞更多信息之目的,並不意味着贊同其觀點或證實其描述,文章內容僅供參考。