1 Reply Latest reply on Mar 8, 2007 4:01 PM by ntsiii

    Hello HTTPService Tutorial

      Here is some code to show you how to use mxml HTTPService and Actionscript's HTTPService side by side.
      Goes over some basics that other beginners might appreciate.

      <?xml version="1.0" encoding="utf-8"?>
      Author: Travis Somerville
      My first Actionscript application. Wanted to share a few details with other people.
      Subjects: HTTPService caching, making URL requests, databinding parsed results, URL tutorial

      This app shows the following:
      * how to get URL requests and parse a complex response into a useable value.
      * the concept of avoiding caching is shown.
      * the concept of how to import the HTTPService object when using both mxml and Actionscript references.
      * how to attach an event to an mxml HTTPService object, and an Actionscript HTTPService object
      * how to databind the HTTPService result to the parsed value

      <mx:Application layout="vertical"
      xmlns:mx=" http://www.adobe.com/2006/mxml"

      import mx.rpc.events.ResultEvent;
      import mx.messaging.messages.HTTPRequestMessage;
      //import mx.rpc.http.HTTPService;
      import mx.rpc.http.mxml.HTTPService;
      import mx.controls.Alert;

      [Bindable] public var time:String;
      [Bindable] public var timeServ2:HTTPService = new HTTPService();

      private function initializeHandler(event:Event):void {
      timeServ2.resultFormat = "text";
      // This URL updates our request with current time. Visit it in your browser to see what we're parsing.
      timeServ2.url = " http://tycho.usno.navy.mil/cgi-bin/timer.pl?"
      timeServ2.addEventListener(ResultEvent.RESULT, timeResultHandler);

      // This handler shows how to parse copmlex responses.
      // Useful for when the URL you want doesn't return just what you need.
      // Most likely to encounter a need when you're not controlling the other end...
      private function timeResultHandler(event:Event):void {
      var timeResult:String = String(timeServ2.lastResult);
      var timeZones:Array = timeResult.split("<BR>");
      // The 3 means the 4th element in the array. This should give Central Time.
      time = timeZones[3];

      private function timeResultHandler3(event:Event):void {
      var timeResult:String = String(timeServ3.lastResult);
      var timeZones:Array = timeResult.split("<BR>");
      // The 4 means the 5th element in the array. This should give Mountain Time.
      time = timeZones[4];


      <!-- Note how the URL has a ? on the end. For some reason, this causes the request NOT to cache
      A random number attached on the end might do the same thing. Took me hours to figure this out! -->
      <mx:HTTPService url=" http://tycho.usno.navy.mil/cgi-bin/timer.pl?"

      <mx:Panel width="644" height="429" layout="absolute" id="mainPanel" title="slide test" horizontalAlign="center">
      <!-- Just another quick example of binding data to something -->
      <mx:Label text="{slider.value}" x="201" y="10"/>

      <!-- the text="{time}" is where the databind magic happens -->
      <mx:TextArea id="textArea" text="{time}" x="10" y="54" width="272" height="130"/>

      <mx:HSlider id="slider" minimum="0" maximum="100" snapInterval="2" enabled="true" x="10" y="10"/>
      <mx:Button x="303" y="8" label="Get Time2" click="timeServ2.send()"/>
      <mx:Button x="437" y="8" label="Get Time3" click="timeServ3.send()"/>

        • 1. Re: Hello HTTPService Tutorial
          ntsiii Level 3
          That is a good example. One recommendation: Do not use lastResult in the handler, like this:
          var timeResult:String = String(timeServ2.lastResult);
          lastResult is intended for binding expressions.

          Instead, declare the handler's event argument as a ResultEvent (that is what it really is, remember to import mx.rpc.events.ResultEvent), then use the event.result:
          private function timeResultHandler(event:ResultEvent):void {
          var timeResult:String = String(event.result);