Log In  
  Getting Started  
compared with
Current by Randy Nielsen
on May 28, 2009 13:44.

(show comment)
 
Key
These lines were removed. This word was removed.
These lines were added. This word was added.

View page history


There are 1 changes. View first change.

 {section}{column:width=80%}
  
 h2. Code files
  
 This section provides the completed code for the Shipping Costs Flex application. To see the application in action, see the Part II Introduction or the Code Anatomy. For step-by-step instructions to build this application, see the Tutorial.
  
 The Shipping Costs Flex application code is available below in two categories, with three versions each, that only differ by one line of code:
 * Category 1: HTTPService with plain text return
 * Category 2: HTTPService with XML return{column}{column}
 {adbe-prev:http://learn.adobe.com/wiki/display/Flex/2a.+Learning+Points}
 {adbe-next:http://learn.adobe.com/wiki/display/Flex/2c.+Code+Anatomy}
 {rate:title=User rating\|theme=dynamic\|key=focal\|display=default} {adbe-pod}
  
 h6. Learn more
  
 * {adbe-popup:http://www.adobe.com/devnet/flex/quickstart/accessing_xml_data/}Accessing XML data {adbe-popup}
 * {adbe-popup:http://livedocs.adobe.com/flex/3/html/data_intro_2}Flex:Accessing Data {adbe-popup}
 * {adbe-popup:http://en.wikipedia.org/wiki/Plain_text}Plain text definition {adbe-popup}{adbe-pod}
 {adbe-showhide}{column}{section}
  
 h2. Application overview
  
 !dataRetrieval.png!
  
 h2. Category 1: HTTPService with plain text return
  
 Use the following links to view the page served by each of the technologies:
 * ColdFusion: [http://www.coldfusionportal.org/flex3cf/PlainHTTPService.cfm?zipcode=12345&pounds=34]
 * Java: [http://examples.adobe.com/flex3app/flex3samples/exchangingdata/text/PlainHttpService.jsp?zipcode=12345&pounds=34]
 * ASP.NET: [http://aspexamples.adobe.com/flex3/exchangingdata/PlainHttpService.aspx?zipcode=12345&pounds=34?zipcode=12345&pounds=34]
 * PHP: [http://examples.adobe.com/flex3/exchangingdata/text/plainHttpService.php?zipcode=12345&pounds=34]
  
 {code}
 <?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>
 {code}
  
 h2. Category 2: HTTPService with XML return
  
 Use the following links to view the page served by each of the technologies:
 * ColdFusion: [http://www.coldfusionportal.org/flex3cf/xmlHttpService.cfm?zipcode=12345&pounds=34]
 * Java: [http://examples.adobe.com/flex3app/flex3samples/exchangingdata/xml/xmlHttpService.jsp?zipcode=12345&pounds=34]
  * ASP.NET: [http://aspexamples.adobe.com/flex3/exchangingdata/xmlHttpService.aspx?zipcode=12345&pounds=34]
  * ASP.NET: [http://aspexamples.adobe.com/flex3/exchangingdata/xmlHttpService.aspx?zipcode=02360&pounds=54]
 * PHP: [http://examples.adobe.com/flex3/exchangingdata/xml/xmlHttpService.php?zipcode=12345&pounds=34]
  
 {code}
 <?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>
 {code}{adbe-cf}
  
 h2. ColdFusion files
  
  
 h6. PlainHttpService.cfm
  
 {code}
 <cfsetting enablecfoutputonly="true" />
  
 <cfinvoke component="Shipping"
  method="getShippingOptions" argumentcollection="#url#"
  returnvariable="myResult" />
  
 <cfloop index="i" from="1" to="#ArrayLen(myResult)#">
  <cfoutput>#myResult[i].service#: #dollarFormat(myResult[i].price)#<br /></cfoutput>
 </cfloop>
 {code}
  
 h6. XmlHttpService.cfm
  
 {code}
 <cfsetting enablecfoutputonly="true" />
  
 <cfsilent>
  <cfinvoke component="Shipping"
  method="getShippingOptions" argumentcollection="#url#"
  returnvariable="myResult" />
  <cfoutput>
  <cfxml variable="userXML">
  <options>
  <cfloop index="i" from="1" to="#ArrayLen(myResult)#">
  <option>
  <service>#myResult[i].service#</service>
  <price>#myResult[i].price#</price>
  </option>
  </cfloop>
  </options>
  </cfxml>
  </cfoutput>
 </cfsilent>
 <cfcontent reset ="yes" type="text/xml; charset=UTF-8">
 <cfoutput>#userXML#</cfoutput>
 {code}
  
 h6. Shipping.cfc
  
 {code}
 <cfcomponent>
  <cffunction name="getShippingOptions" access="remote" returntype="array">
  <cfargument name="zipcode" type="any" required="yes" >
  <cfargument name="pounds" type="any" required="yes">
  
  <cfset var options=ArrayNew(1)>
  <cfset var baseCost=(zipcode / 10000) + (pounds * 5)>
  
  <cfset options[1] = structNew() />
  <cfset options[1].service="Next Day">
  <cfset options[1].price=baseCost * 4>
  <cfset options[2] = structNew() />
  <cfset options[2].service="Two Day Air">
  <cfset options[2].price=baseCost * 2>
  <cfset options[3] = structNew() />
  <cfset options[3].service="Saver Ground">
  <cfset options[3].price=baseCost>
  
  <cfreturn options>
  </cffunction>
  
  <cffunction name="getShippingOptions_CFQuery" access="remote" returntype="query">
  <cfargument name="zipcode" type="any" required="yes" >
  <cfargument name="pounds" type="any" required="yes">
  
  <cfset var options=ArrayNew(1)>
  <cfset var baseCost=(zipcode / 10000) + (pounds * 5)>
  
  
  <cfscript>
  qOptions = queryNew("service, price");
  newRow = QueryAddRow(qOptions, 3);
  temp = QuerySetCell(qOptions, "service", "Next Day", 1);
  temp = QuerySetCell(qOptions, "price", baseCost * 4, 1);
  temp = QuerySetCell(qOptions, "service", "Two Day Air", 2);
  temp = QuerySetCell(qOptions, "price", baseCost * 2, 2);
  temp = QuerySetCell(qOptions, "service", "Saver Ground", 3);
  temp = QuerySetCell(qOptions, "price", baseCost, 3);
  </cfscript>
  <cfreturn qOptions>
  </cffunction>
 </cfcomponent>
 {code}{adbe-cf}{adbe-php}
  
 h2. PHP files
  
  
 h6. XML example
  
 xmlHttpService.php
 {code}
 <?php
  
  // shipping.php contains our get_shipping_options($zipcode, $pounds) function.
  // It returns an array mapping service name to price in US dollars.
  require('shipping.php');
  
  $options = get_shipping_options($_REQUEST["zipcode"], $_REQUEST["pounds"]);
  
  $results[] = "<options>";
  foreach ($options as $service => $price) {
  $results[] = "<option><service>$service</service><price>$price</price></option>";
  }
  $results[] = "</options>";
  print implode("\n", $results);
 ?>
 {code}
 shipping.php
 {code}
 <?php
  
  // Returns an array of made-up shipping options.
  function get_shipping_options($zipcode, $pounds) {
  $baseCost = round($zipcode / 10000) + ($pounds * 5);
  $options = array( "Next Day" => $baseCost * 4,
  "Two Day Air" => $baseCost * 2,
  "Saver Ground" => $baseCost);
  return $options;
  }
  
 ?>
 {code}
  
 h6. Plain text example
  
 plainHttpService.php
 {code}
 <?php
  // shipping.php contains our get_shipping_options($zipcode, $pounds) function.
  // It returns an array mapping service name to price in US dollars.
  require ('shipping.php');
  
  $options = get_shipping_options($_REQUEST[zipcode], $_REQUEST[pounds]);
  foreach ($options as $service => $price) {
  $result[] = "$service: $price USD";
  }
  print implode("\n", $result);
 ?>
 {code}{adbe-php}{adbe-java}
  
 h2. JSP and JAVA files
  
  
 h6. Plain text example
  
 PlainHttpService.jsp
 {code}
 <%@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
 <%
  }
 %>
 {code}
  
 h6. XML example
  
 xmlHttpService.java
 {code}
 <%@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>
 {code}
 ShippingCalculator.java
 {code}
 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;
  }
 }
 {code}
 ShippingOption.java
 {code}
 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; }
 }
 {code}{adbe-java}{adbe-asp}
  
 h2. ASP.NET files
  
 To use the ASP.NET files, you must create a virtual directory in the IIS Manager named
 "quickstart". You must then create a subdirectory named "bin". Compile the two C# files
 into quickstart.dll, and copy that DLL into the bin directory.
  
 h6. PlainHttpService.aspx
  
 {code}
 <%@ Import Namespace="quickstart" %>
  
 <script language="C#" runat="server">
  
  public void Page_Load(Object sender, EventArgs E)
  {
  int zipcode;
  double weight;
  
  if(Request.RequestType == "POST")
  {
  zipcode = Int32.Parse(Request.Form["zipcode"].ToString());
  weight = Double.Parse(Request.Form["pounds"].ToString());
  }
  else
  {
  zipcode = Int32.Parse(Request.QueryString["zipcode"].ToString());
  weight = Double.Parse(Request.QueryString["pounds"].ToString());
  }
  
  ShippingCalculator shippingcalculator = new ShippingCalculator();
  ShippingOption shippingOption = new ShippingOption();
  
  ArrayList al = shippingcalculator.getShippingOptions(zipcode, weight);
  StringBuilder stringBuilder;;
  
  foreach(Object obj in al)
  {
  stringBuilder = new StringBuilder();
  shippingOption = (ShippingOption)obj;
  stringBuilder.Append(shippingOption.getService());
  stringBuilder.Append(": ");
  stringBuilder.Append(shippingOption.getPrice());
  stringBuilder.Append(" USD" + "\n");
  Response.Write(stringBuilder.ToString());
  }
  }
 </script>
 {code}
  
 h6. xmlHttpService.aspx
  
 {code}
 <%@ Import Namespace="quickstart" %>
  
 <script language="C#" runat="server" ContentType="text/xml" >
  
  public string str="";
  
  public void Page_Load(Object sender, EventArgs E)
  {
  int zipcode;
  double weight;
  
  if(Request.RequestType == "POST")
  {
  zipcode = Int32.Parse(Request.Form["zipcode"].ToString());
  weight = Double.Parse(Request.Form["pounds"].ToString());
  }
  else
  {
  zipcode = Int32.Parse(Request.QueryString["zipcode"].ToString());
  weight = Double.Parse(Request.QueryString["pounds"].ToString());
  }
  
  ShippingCalculator shippingcalculator = new ShippingCalculator();
  ShippingOption shippingOption = new ShippingOption();
  
  ArrayList al = shippingcalculator.getShippingOptions(zipcode, weight);
  StringBuilder stringBuilder = new StringBuilder("<options>");
  
  foreach(Object obj in al)
  {
  shippingOption = (ShippingOption)obj;
  stringBuilder.Append("<option><service>");
  stringBuilder.Append(shippingOption.getService());
  stringBuilder.Append("</service><price>");
  stringBuilder.Append(shippingOption.getPrice());
  stringBuilder.Append("</price></option>" );
  }
  stringBuilder.Append("</options>");
  str = stringBuilder.ToString();
  
  }
  
 </script>
 <?xml version="1.0" encoding="utf-8"?>
 <%
 Response.ContentEncoding = Encoding.UTF8;
 Response.Write(str);
 %>
 {code}
  
 h6. ShippingCalculator.cs
  
 {code}
 using System;
 using System.Collections;
  
 namespace quickstart
 {
  
  public class ShippingCalculator
  {
  //Returns a list of made-up ShippingOptions.
  public ArrayList getShippingOptions(int zipcode, double pounds)
  {
  ArrayList options = new ArrayList();
  double baseCost;
  
  baseCost = Math.Round((double)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;
  }
  }
 }
 {code}
  
 h6. ShippingOption.cs
  
 {code}
 using System;
  
 namespace 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; }
  }
 }
 {code}
 {adbe-asp}
  
 {adbe-prev:http://learn.adobe.com/wiki/display/Flex/2a.+Learning+Points}
 {adbe-next:http://learn.adobe.com/wiki/display/Flex/2c.+Code+Anatomy}
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators