Flex's httpservice Data Interaction code collection

Source: Internet
Author: User
Tags xml example

TodayCodeAfter finishing the tutorial, you don't need to write it. I believe that if you are involved in this field, you don't have to teach them by hand. The code you have compiled mainly includes PHP and ASP. net, Java. nice enough. recently, examples of various language versions of the fms2.0 application are being compiled, which may pop up later.

First, let's look at the following categories: httpservice is mainly divided into common webpage format calling and XML method calling.

Flex (common form ):

<? 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 format):   
<? 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: datagriddataprovider = "{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>
Background code:
ColdFusion Files (my favorite files are CF and PHP) Plainhttpservice. cfm

<Cfprocessingdirective pageencoding = "UTF-8" suppresswhitespace = "yes">
<Cfset myargs.zipcode=url.zip code>
<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.zip code> <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, using arguments.zip Code #, # 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 file XML example

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 ;}?>
Page format example

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

Examples of JSP and Java file page formats

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 ;}}
ASP. NET file page format file: 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 = new shippingcalculator (); shippingoption = new shippingoption (); arraylist Al = shippingcalculator. getshippingoptions (zipcode, weight); 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 Format File
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 = new shippingcalculator (); shippingoption = new shippingoption (); arraylist Al = shippingcalculator. getshippingoptions (zipcode, weight); stringbuilder = new stringbuilder ("<Options>"); foreach (Object OBJ in Al) {shippingoption = (shippingopti On) 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 ;}}}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.