Log In  
  Getting Started  
----------------------------------------

Resources

 

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
previous page next page

User rating?

Show / Hide hints for:

Application overview

Category 1: HTTPService with plain text return

Use the following links to view the page served by each of the technologies:

<?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>

Category 2: HTTPService with XML return

Use the following links to view the page served by each of the technologies:

<?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>

ColdFusion files

PlainHttpService.cfm
<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>
XmlHttpService.cfm
<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>
Shipping.cfc
<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>

PHP files

XML example

xmlHttpService.php

<?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);
?>

shipping.php

<?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;
    }

?>
Plain text example

plainHttpService.php

<?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);
?>

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.java

<%@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; }
}

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.

PlainHttpService.aspx
<%@ 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>
xmlHttpService.aspx
<%@ 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);
%>
ShippingCalculator.cs
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;
        }
    }
}
ShippingOption.cs
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; }
    }
}
previous page next page
Added by Mark Nichoson , last edited by Randy Nielsen on May 28, 2009  (view change)
Labels: 
(None)

Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators