6 Replies Latest reply on Nov 26, 2007 6:17 PM by chris.huston.t10

    HTTPService in ActionScript

    bpapau
      I'm writing a service call in Actionscript (httpservice) and it doesnt seem to work as expected. I've looked around and haven't found anything that really explains this seemingly simple task, but some that come close. I'd appreciate some help.

      I have a php call that will pass an XML data file.

      First my imports:

      import mx.events.*;
      import mx.rpc.events.*;

      NOTE: I cant seem to get mx.rpc.http.httpservice to import... weird error occurs... anyone know why?

      Next, my service call, which occurs in a for loop (that seems to matter to the posts I've been reading):

      var DBService:HTTPService = new HTTPService;
      DBService.url = " http://192.168.11.12/getdata.php?length=day";
      var DB_data:Object = new Object();
      DBService.send(DB_data);

      Then I try to use the data:

      var _newChart:LineChart = new LineChart;
      _newChart.percentHeight = 92;
      _newChart.percentWidth = 100;
      _newChart.showDataTips = true;
      _newChart.x = 0;
      _newChart.y = 0;
      _newChart.dataProvider = DB_data.lastResult.data.point;
      _newPanel.addChild(_newChart);

      I've tried using both the service call and the object as that data provider, but it errors every time. Anyone have some experience with this? Seems like thistype of thing would be a fairly common tool, but I haven't seen it used.

      Thanks.

        • 1. Re: HTTPService in ActionScript
          estacado1
          I'm not sure if this helps but try setting the resultFormat of the HTTPService. I usually have mine set to e4x.
          • 2. Re: HTTPService in ActionScript
            chris.huston.t10 Level 3
            This might help:
            import mx.charts.LineChart;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.http.HTTPService;
            import mx.rpc.events.ResultEvent;

            private function view_code():void {
            var DBService:HTTPService = new HTTPService();
            DBService.url = " http://192.168.11.12/getdata.php?length=day";
            DBService.method = "POST";
            DBService.resultFormat = 'e4x'; //if you have XML data
            DBService.addEventListener(ResultEvent.RESULT,dataReceived);
            DBService.addEventListener(FaultEvent.FAULT, httpFault);
            var DB_data:Object = new Object();
            DBService.send(DB_data);
            }

            private function dataReceived(evt:ResultEvent):void {
            var _newChart:LineChart = new LineChart();
            _newChart.dataProvider = evt.result.data.point;
            }

            private function httpFault(evt:FaultEvent):void {
            Alert.show(evt.fault.faultString);
            }

            Or you can wrap the above code in a generic function that you pass the url and result function to as parameters. That way you can easily have multiple HTTPService calls with various formats with only one main function. Here is what I use:

            private function contact_server(parameters:Object, php_url:String, result_fxn:Function=null, result_format:String="e4x"):void {
            var contact:HTTPService = new HTTPService();
            contact.resultFormat = result_format;
            contact.url = php_url;
            contact.method = "POST";
            if (result_fxn != null ) {
            contact.addEventListener(ResultEvent.RESULT, result_fxn);
            }
            contact.addEventListener(FaultEvent.FAULT, httpFault);
            contact.send(parameters);
            }

            Vygo
            • 3. Re: HTTPService in ActionScript
              bpapau Level 1
              Thank you for the reply, it's very helpful.

              I have it set up as per you're example, and I am getting another error, 'RangeError: Error #2006: The supplied index is out of bounds.' I noticed you've had some experience with it in the past.

              I have made the _newChart a public variable, and then a for loop generates a new child on every loop:

              // makes a public chart
              public var _newChart:LineChart = new LineChart;
              // populates the chart
              private function dataReceived(evt:ResultEvent):void {
              Alert.show("success");
              _newChart.dataProvider = evt.result.data.point;
              }

              // and this is in a for loop to generate a bunch of these charts that will exist inside a togglebuttonbar
              var DB_Connect:Object = new Object;
              var URL:String = new String;
              URL = " http://192.168.11.12/getdata.php?length=day"

              useHTTPservice(DB_Connect,URL,dataReceived);

              _newChart.percentHeight = 92;
              _newChart.percentWidth = 100;
              _newChart.showDataTips = true;
              _newChart.x = 0;
              _newChart.y = 0;
              _newPanel.addChild(_newChart);

              I'd appreiate any help, and I'll post the final solution so when other people try this they will have an immediate reference




              • 4. Re: HTTPService in ActionScript
                bpapau Level 1
                Ok, I think the source of the Range error is because I kept changing that same child (_newChart).

                So then how am I to add the dataProvider to _newChart given that the function only handles the evt:ResultEvent

                example below

                private function useHTTPservice(param:Object,URL:String,result_fxn:Function=null,result_format:String="e4x "):void
                {

                var contact:HTTPService = new HTTPService();
                contact.resultFormat = result_format;
                contact.url = URL;
                contact.method = "POST";
                if (result_fxn != null ) {
                contact.addEventListener("result", result_fxn);
                }
                contact.addEventListener("fault", httpFault);
                contact.send(param);
                }

                private function dataReceived(evt:ResultEvent):void {
                Alert.show("success");
                //_newChart.dataProvider = evt.result.data.point;
                }

                private function httpFault(evt:FaultEvent):void {
                Alert.show("fail");
                }
                • 5. Re: HTTPService in ActionScript
                  bpapau Level 1
                  Okay, so I've tried to simplfy my code (taking out for loops for a proof of concept) and the graphs still don not populate...

                  My code is below:
                  public var obj:Object = new Object;

                  private function useHTTPservice(param:Object,URL:String,result_fxn:Function=null,result_format:String="e4x "):void
                  {

                  var contact:HTTPService = new HTTPService();
                  contact.resultFormat = result_format;
                  contact.url = URL;
                  contact.method = "POST";
                  if (result_fxn != null ) {
                  contact.addEventListener("result", result_fxn);
                  }
                  contact.addEventListener("fault", httpFault);
                  contact.send(param);
                  }

                  private function dataReceived(evt:ResultEvent):void
                  {
                  obj = evt.result.data.point;
                  Alert.show("succss");
                  //_newChart.dataProvider = evt.result.data.point;
                  }

                  private function httpFault(evt:FaultEvent):void
                  {
                  Alert.show("fail");
                  }
                  private function configkiosk(event:ResultEvent):void
                  {
                  // sets up the base page
                  MainCanvas = new Canvas();
                  MainCanvas.id = "Main Canvas";
                  MainCanvas.width = 860;
                  MainCanvas.height = 800;
                  MainCanvas.setStyle("backgroundColor","white");
                  MainCanvas.setStyle("horizontalCenter","0");
                  MainCanvas.setStyle("verticalCenter","0");
                  this.addChild(MainCanvas);

                  var _newPanel:Panel = new Panel;
                  _newPanel.percentHeight = 100;
                  _newPanel.percentWidth = 100;
                  _newPanel.x = 0;
                  _newPanel.y = 0;
                  _newPanel.setStyle("borderStyle","none");
                  _newPanel.setStyle("horizontalAlign","center");
                  MainCanvas.addChild(_newPanel);

                  var _newChart:LineChart = new LineChart;
                  _newChart.percentHeight = 100;
                  _newChart.percentWidth = 100;
                  _newChart.showDataTips = true;
                  _newChart.x = 0;
                  _newChart.y = 0;
                  _newPanel.addChild(_newChart);

                  var DB_Connect:Object = new Object;
                  var URL:String = new String;
                  URL = " http://192.168.11.12/getdata.php?length=day"

                  useHTTPservice(DB_Connect,URL,dataReceived);
                  //horizontal axis

                  _newChart.dataProvider = obj;

                  var _newSeries:Series = new Series;

                  var line:LineSeries = new LineSeries;
                  _newChart.addChild(_newSeries);

                  var lineStroke:Stroke = new Stroke();
                  lineStroke.color = 0xFF9900;
                  lineStroke.alpha = 1;
                  lineStroke.weight = 2;
                  line.setStyle("linestroke",lineStroke);
                  line.yField = "CO2_BDROOM";
                  _newSeries.addChild(line);


                  }
                  • 6. Re: HTTPService in ActionScript
                    chris.huston.t10 Level 3
                    HTTPService calls are asynchronous, which means that you cannot make a call to the service and assign the result in the same function. This is why there are result functions. So, put the bottom part of your configkios function into your dataReceived function to populate the chart. Also, you are returning XML but converting it to an Object. It doesn't seem that this is necessary. If you are still having troubles, then make sure that your XML is being properly received. You can add a trace to your dataReceived function to check this as I have done below. Run the app in debug mode to see the trace.

                    private function dataReceived(evt:ResultEvent):void
                    {
                    obj = evt.result.data.point;
                    trace(obj.toXMLString());
                    _newChart.dataProvider = obj;
                    var _newSeries:Series = new Series;
                    var line:LineSeries = new LineSeries;
                    _newChart.addChild(_newSeries);

                    var lineStroke:Stroke = new Stroke();
                    lineStroke.color = 0xFF9900;
                    lineStroke.alpha = 1;
                    lineStroke.weight = 2;
                    line.setStyle("linestroke",lineStroke);
                    line.yField = "CO2_BDROOM";
                    _newSeries.addChild(line);
                    }

                    Vygo