• Flex's HTTPService 数据交互代码大集合


    今天把常用的后台代码整理一下,教程就不用写了,我相信要是涉及到这方面的人,应该不用手把手教的,整理的代码主要有PHP,ASP.NET,JAVA.够爽吧.最近也正在整理FMS2.0应用的各种语言版本的例子,以后可能会爆出来.

    先分一下类:HTTPService主要分为普通网页格式调用和,XML方式调用.

    Flex(普通形式):

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="absolute"
        backgroundColor="#FFFFFF"
        backgroundAlpha="0">
    	<mx:Script>
    		<![CDATA[
    		import mx.rpc.events.ResultEvent;
    		import mx.rpc.events.FaultEvent;
    		import mx.controls.Alert;
    
    	        public function handlePlain(event:ResultEvent):void
    	        {
    	            shippingOptions.text = 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://yourserver.com/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>
    Flex(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;
    
    	        public function handleXML(event:ResultEvent):void
                {
                    shippingInfo = event.result.option as XML;
                }
    
    	        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"
        	method="POST"  url="http://yourserver.com/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文件(我最喜欢CF和PHP)

    PlainHttpService.cfm

    <cfprocessingdirective pageencoding = "utf-8" suppressWhiteSpace = "Yes">
    <cfset myArgs.zipcode=URL.zipcode>
    <cfset myArgs.pounds=URL.pounds>
    <cfinvoke component="Shipping" method="getShippingOptions"
    argumentcollection="#myArgs#"
       returnvariable="myResult">
    <cfoutput>
       <cfloop index="i" from="1" to="#ArrayLen(myResult)#">
       #myResult[i][1]#: $#myResult[i][2]#<br />
       </cfloop>
    </cfoutput>
    </cfprocessingdirective>

    XmlHttpService.cfm
    <cfprocessingdirective pageencoding = "utf-8" suppressWhiteSpace = "yes">
    <cfset myArgs.zipcode=URL.zipcode>
    <cfset myArgs.pounds=URL.pounds>
    <cfinvoke component="Shipping" method="getShippingOptions"
    	rgumentcollection="#myArgs#"
       	returnvariable="myResult">
    <cfxml variable="userXML">
    <options>
        <cfloop array="#myresult#" index="theOption" >
            <cfoutput>
                <option>
                    <service>#theOption[1]#</service>
                    <price>#theOption[2]#</price>
                </option>
            </cfoutput>
        </cfloop>
     </options>
    </cfxml>
    <cfoutput>#userXML#</cfoutput>
    </cfprocessingdirective>
    Shipping.cfc
    <cfcomponent>
    	<cffunction name="getShippingOptions"
    		access="public" returntype="array">
    		<cfargument name="zipcode" type="any" required="yes" >
    		<cfargument name="pounds" type="any" required="yes">
            <cflog text="In Shipping.getShippingOptions,
            	#Arguments.zipcode#, #Arguments.pounds#">
            <cfset options=ArrayNew(2)>
    		<cfset baseCost=(zipcode / 10000) + (pounds * 5)>
            <cfset options[1][1]="Next Day">
            <cfset options[1][2]=baseCost * 4>
            <cfset options[2][1]="Two Day Air">
            <cfset options[2][2]=baseCost * 2>
            <cfset options[3][1]="Saver Ground">
            <cfset options[3][2]=baseCost>
    		<cflog text="options: #ArrayToList(options[3])#">
    		<cfreturn options>
    	</cffunction>
    </cfcomponent>
     

    PHP 文件

    XML 例子

    xmlHttpService.php

    <?php
        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
        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;
        }
    
    ?>
    页面格式例子

    plainHttpService.php

    <?php
        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 和 JAVA 文件

    页面格式例子

    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例子

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

    ASP.NET 文件

    页面格式文件:
    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>
    XML格式文件
    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; }
        }
    }

  • 相关阅读:
    从aptitude 换回 apt-get .
    ubuntu 通过安装TOMCAT7
    第一次ubuntu使用的故障排除 ...the public key is not available: NO_PUBKEY...
    DES加密算法应用:分组加密模式
    使用“牛顿迭代法”求解方程
    html当中如何引用js文件
    javascript当中火狐的firebug如何单步调试程序?
    给出一个javascript的Helloworld例子
    java中讲讲PrintWriter的用法,举例?
    卷积和神经网络有什么关系?
  • 原文地址:https://www.cnblogs.com/cy163/p/1513966.html
Copyright © 2020-2023  润新知