8 Replies Latest reply on Jun 26, 2009 11:42 AM by Jerry62712

    FLEX URL read

    Jerry62712 Level 1

      I guess I've been asking too much at once, so I try again more targeted.

       

      Who has an example, hopefully explained, of reading the URL request string?  Here is what I'm looking for.

       

      URL: file:///C:/Flex Builder 3/FSCalc/FSCalc/bin-debug/FSCalc.html?xml=<?xml version="1.0" encoding="UTF-8" ?>
      <FSParm><HouseholdSize>6</HouseholdSize></FSParm>

       

      String: xml=<?xml version="1.0" encoding="UTF-8" ?><FSParm><HouseholdSize>6</HouseholdSize></FSParm>

       

      Background - no need to read this:

      I"ve seen a few suggestions, but they all breakdown at some point losing me.  For example,

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

       

      This has no error in it that I see, but when I put in the debugger run, it never stops at the "handleXML" function.  When I started this I had no idea it would take over three weeks (and counting) to find out how to get xml information into the Flex system.  I had assumed that someone had actually done this and there would be an easily found example.

        • 1. Re: FLEX URL read
          Flex harUI Adobe Employee

          Where'd you get the example?  It is incomplete.  I needed to add the parameter controls and especially, the button that calls send:

           

          <mx:TextInput y="20" id="zipcode" />

               <mx:TextInput y="50" id="weight_lb" />

               <mx:Button y="80" click="xmlRPC.send()" />

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: FLEX URL read
            CoreyRLucier Adobe Employee

            Well, your sample had quite a few errors in it..here is updated code...

             

            1. You had no input controls called zipcode or weight_lb.  I just removed those bindings and hardcoded a value.

            2. You were never calling 'send()' on your HTTPService instance.

            3. The request URL you were using had a spurious space character.

            4. The parameters to Alert.show were invalid..

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="xmlRPC.send()">

             

                 <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);
                         }
                      ]]>
                 </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>22611</zipcode>
                        <pounds>25</pounds>
                    </mx:request>
                 </mx:HTTPService>
                
            </mx:Application>

            • 3. Re: FLEX URL read
              Jerry62712 Level 1

              Thanks for the reply.  It takes about 15 minutes for the reply screen to pop up so I'm going to have to guess which code example I used rather than go back and wait another 15 minutes to get back here.  I have used two sources mostly.  http://learn.adobe.com/wiki/display/Flex/2b.+Code+Files is the prime one and the other is http://developer.yahoo.com/flash/articles/e4x-beginner-to-advanced.html.

               

              I need this to happen automatically.  There will be no person to hit a button.  The application will take in the data (how is the question), process it and output an XML file back to the invoker (the next question once someone can tell me how to get the input stream).

               

              Is there a way to call javascript in Flex?  Javascript has an automaticly created variable that has the input parameters.  All I would need is one line: document.referer will do it as will: location.pathname.  I was hoping Flex had something like this.

               

              Of course, this input isn't XML formatted so I would like to have that applied to the stream if we can ever figure out how to get that stream.

              • 4. Re: FLEX URL read
                Jerry62712 Level 1

                Thanks for your reply.  I think I set a new record for this reply window opening - about 30 minutes.  Sigh.

                 

                I think I understand the stuff from the example in the script area.  A function is called (regular or fault) by the system as detailed in:

                 

                <mx:HTTPService  result="handleXML(event);" fault="handleFault(event);" id="xmlRPC" resultFormat="e4x"
                         url="http://examples.adobe.com/flex3app/flex3samples/exchangingdata/xml/xmlHttpServic e.jsp" useProxy="false">
                         <mx:request xmlns="">
                            <zipcode>22611</zipcode>
                            <pounds>25</pounds>
                        </mx:request>
                     </mx:HTTPService>

                 

                The data in the event.result.option has the URL I'm looking for (I hope).  When I enter event.result, option isn't an option that presents itself, but I take it on faith.

                 

                My problem is how to get the functions invoked.  I had assumed that HTTPService would be a listener of some type that would have got the data from the load of the page.  I don't know what the "url" property would be in my case.  I can't know the stream passed in in advance to hard code it in the application.  I could be getting from 1 to many items passed in that I will then be using to make some calculations.

                 

                Will the above code work to 1) automatically get the input parameters from the event object, 2) be available to script processing and 3) function like either of the javascript properties that do this (location.pathname or document.referrer)?

                • 5. Re: FLEX URL read
                  Flex harUI Adobe Employee

                  I use the email options so I don't have to go the web pages.  I'm still unclear what you're trying to do.

                   

                  HTTPService is like Ajax XMLHTTPRequest.  It is a way to make server requests after your app is up and running.  It has nothing to do with the HTML page that wraps the SWF that is your application.  If you want to automatically make a server request use the creationComplete or applicationComplete to call send().

                   

                  If you want to know the parameters on the HTML page that wraps the SWF, you can use BrowserManager for that, or modify the HTML wrapper to pass the parameters to the SWF.

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: FLEX URL read
                    Jerry62712 Level 1

                    [WOW - the reply window opened in just seconds instead of the normal 15-20 minutes I experience!]

                     

                    It looks like HTTPService isn't related to what I want at all.  That seemed to be what everyone was pointing to.

                     

                    You are correct that what I am after is the URL that invokes the webpage that has the SWF application in it.  I need to get the query part (if any) of the URL.  For example, if this is used:

                     

                    http://FSCalc/bin-debug/FSCalc.html?xml=<?xml version="1.0" encoding="UTF-8" ?><FSParm><HouseholdSize>6</HouseholdSize></FSParm>
                    
                    

                    then I would want to be able to look at this:

                     

                    xml=<?xml version="1.0" encoding="UTF-8" ?><FSParm><HouseholdSize>6</HouseholdSize></FSParm>
                    
                    

                    The ultimate goal is to be able to set property "HouseholdSize" to the value of 6 and then do all the calculations.  The stream could have no parns (a user enters just the webpage), 1 parm (a program calls the webpage as above) or many parms (that program might have many fields such as:

                     

                    xml=
                    <?xml version="1.0" encoding="UTF-8" ?>
                    <FSParm>
                    <GrossMonthlyEarnedIncome>2000</GrossMonthlyEarnedIncome>
                    <GrossMonthlyOtherIncome>694</GrossMonthlyOtherIncome>
                    <HouseholdSize>6</HouseholdSize>
                    <QualifyingMember>true</QualifyingMember>
                    <SimplifiedProcessingUnitInd>true</SimplifiedProcessingUnitInd>
                    <DependentCareCostsAmt>500</DependentCareCostsAmt>
                    <NumberOfChildrenUnder2></NumberOfChildrenUnder2>
                    <NumberOfChildrenOver2>1</NumberOfChildrenOver2>
                    <CourtOrderedSupportAmt>300</CourtOrderedSupportAmt>
                    <MedicalExpenseAmt>250</MedicalExpenseAmt>
                    <MonthlyRentOrMortgageAmt>250</MonthlyRentOrMortgageAmt>
                    <MonthlyInsuranceAndTaxAmt>0</MonthlyInsuranceAndTaxAmt>
                    <UtilityType>liheap</UtilityType>
                    <TotalAssets>2999</TotalAssets>
                    </FSParm>
                    

                     

                    I looked into BrowserManager (http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html#229997), but it looked like the "URL" in that case is delimited by a "#" and used for URL Fragments.  Just in case I missed something and am wrong, could you provide a c/site for the page?  This is what was on their page:

                     

                    If you set a value for the default fragment in the init() method, when the URL changes, the URL with the default fragment will be entered in the browser's history. This way, if the user clicks the Back button or in some other way access this page from the browser's history, this fragment will be used. Also, the BrowserManager returns default fragment if there is nothing after the pound sign ("#") in the URL.

                     

                    If you have any connections, please ask the PTB to include sample input and output for these pages.

                     

                    BTW, I found this section (http://livedocs.adobe.com/flex/3/html/help.html?content=deep_linking_3.html), but it seemed to point to the .SWF file URL, not the one a user would enter.

                    • 7. Re: FLEX URL read
                      Flex harUI Adobe Employee

                      BrowserManager.url should have the full url.

                       

                      Here's another way someone blogged about: http://blog.circlecube.com/2008/03/20/get-current-url-and-query-string-parameters-to-flash -tutorial/

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

                      Blog: http://blogs.adobe.com/aharui

                      • 8. Re: FLEX URL read
                        Jerry62712 Level 1

                        You, sir, are a gentleman and a scholar.  The external part worked and this part of it is now resolved.  Here is the class I wrote:

                         

                        package DHSclasses {
                            import flash.external.*;
                           
                            import mx.controls.Alert;

                         

                            public class FSURLParse {
                                 private var allURL:String;
                                  private var queryString:String;
                                private var parseXML:XMLList;

                         

                                // constructor (dummy)
                                public function FSURLParse() {};

                         

                                public function getAllURL():String {
                                    return allURL;
                                }

                         

                                public function setAllURL():void {
                                    allURL = ExternalInterface.call("window.location.href.toString");
                        Alert.show("**Testing** allURL=" + allURL,"All Url Test");
                                }

                         

                                public function getQueryString():String {
                                    return queryString;
                                }

                         

                                public function setQueryString():void {
                                    // for this application, the queryString will always begin in
                                    // the 5th position (url=xxxx) and will be a single variable
                                    // containing an XML string
                                    var parmKey:String;
                                    var parmValue:String;
                                    var parmSeparater:int;
                                   
                                    queryString = ExternalInterface.call("window.location.search.substring",1);

                         

                                    // separate the "key" ('url' in this case)
                                    // from the "value" (xml string in this case)
                                    parmSeparater = queryString.indexOf("=");
                                    parmKey = queryString.substring(0,parmSeparater);
                                    parmValue = queryString.substring(++parmSeparater);

                         

                                    // change the goofy "%20" to a space and other other codes
                                    // to their normal representation
                                    queryString = unescape(parmValue);
                        Alert.show("**Testing** unescaped query string=" + queryString,"decoded Query String");

                         

                                    // if we were getting multiple parameters, we would have to split
                                    // them into an array based on the separation character "&"
                                    //        if(queryString) {
                                    //            var allParams:Array = queryString.split('&amp;');
                                    //            var length:uint = params.length;
                                    //        }
                                    // we could then look at the array to see the name/value
                                    //        for (var i = 0, index = -1; i < length; i++) {
                                    //            var keyValuePair:String = allParams[i];
                                    //            if((index = keyValuePair.indexOf("=")) > 0) {
                                    //                var paramKey:String = keyValuePair.substring(0,index);
                                    //                var paramValue:String = keyValuePair.substring(index+1);
                                    //                params[paramKey] = paramValue;
                                    //            }
                                    //        }
                                }

                         

                                public function getParseXML():XMLList {
                                    return parseXML;
                                }

                         

                                public function setParseXML():void {
                                    var testing:String = getQueryString().toString();
                        Alert.show("**Testing** query as string" + testing,"XML as string");

                         

                                    parseXML = getQueryString() as XMLList;
                                    if (parseXML) {
                        Alert.show("**testing** parm length=" + parseXML.length(),"Test parm length");
                                        var item:XML;
                                        for each(item in parseXML) {
                                            Alert.show("item:" + item.toXMLString());
                                        }
                                    }
                                }
                            }
                        }

                         

                        Note that getParseXML doesn't work right and will be the next thread.