2 Replies Latest reply on Apr 15, 2013 10:16 AM by voldy2003

    multiple HTTP Requests in for loop response handling

    voldy2003

      i am working on stock market portfolio. a user saves his stock name , buy price ,etc .i've added this details in database.but while viewing his portfolio he should even get the current price of d stocks he has added. while retrieving i fetch all data from database set them in a bean and for each stock i make a HTTP Request. in result handler i event set the current price into the bean. Problem is my entire application is displayed with all the CurrentPrice fields with null.

       

      private function selectContacts():void

      {

          trace("select before");

          stmt.sqlConnection = this.isDbConnected(conn);

          stmt.text = "SELECT * FROM stocks_t";

          stmt.addEventListener(SQLErrorEvent.ERROR, sqlError);

          stmt.addEventListener(SQLEvent.RESULT, sqlResult);

          stmt.execute();

          trace("select after");

      }

       

      private function sqlResult(res:SQLEvent):void{

         

          httpServ = new HTTPService();

          data1 = stmt.getResult().data;

          stockList = new ArrayCollection();   

          for(var d:int=0;d<=data1.length-1;d++)

          {

              myPortfolioBean = new MyProtfolioBean();

              httpServ.url = 'http://quote.yahoo.com/d/quotes.csv?s='+data1[d].stockName+'&f=snl1a';

              httpServ.method = 'GET';

              httpServ.addEventListener(ResultEvent.RESULT,yDataResult);

              httpServ.addEventListener(FaultEvent.FAULT,yDataFault);

              httpServ.showBusyCursor= true;

              httpServ.send();

             

              myPortfolioBean.stockNameB = data1[d].stockName;

              myPortfolioBean.buyB = data1[d].buy;

              myPortfolioBean.quantityB = data1[d].quantity;

              myPortfolioBean.totalQuantityAmountB = data1[d].totalQuantityAmount;

              myPortfolioBean.profitB = data1[d].profit;

             

              stockList.addItem(myPortfolioBean);

              }

      }

      public function yDataResult(evt:ResultEvent):void

      {

          var s:String = evt.result as String;

          var a:Array = s.split(",");

          //To add to a form or individual fields

          lastTraded =  a[2].toString();

          lastTradedFloat = parseFloat(lastTraded);

          myPortfolioBean.currentB = lastTradedFloat;

          stockList.addItem(myPortfolioBean);

          //stockList.addItem({current:lastTradedFloat});

      }

       

      the yDataResult() is called after the entire datagrid is displayed on screen as a result of which the CurrentPrice in datagrid column in null. Eg: user has added google ,yahoo,apple( CurrentPrice  is null for all three values) after some time three more rows (as a result of call to yDataResult) with CurrentPrice xx.xx displayed. I want merged results.

        • 1. Re: multiple HTTP Requests in for loop response handling
          rtalton Level 4

          I hope I understood your example, but unsure about it...

           

          I don't see "CurrentPrice" anywhere within your code, but...

          It looks like you are using addItem to add new items to your data provider "stockList" within BOTH "sqlResult" and "yDataResult". If you were expecting to merge results from "yDataResult" into the results from "sqlResult", it won't work this way automatically.

           

          If "yDataResult" is pulling in data that belongs to the records you got in "sqlResult", then within "yDataResult" you will have to search for the correct record you created in "sqlResult", and merge the data together there.

          1 person found this helpful
          • 2. Re: multiple HTTP Requests in for loop response handling
            voldy2003 Level 1

            hello ,

            "CurrentPrice" is "lastTradedFloat" sorry for the ambiguity.

            and u got my problem but there is an issue ..

             

            all the results are set using "sqlResult" except the "lastTradedFloat" which is set in "ydataResult".

            but i confused with HTTP ResultEvent Execution.

             

            first "sqlResult" method is called arrayCollection is populated. then result is displayed on screen.( lastTraded value is null then)

            then "ydataResult" executes again manipulating data in arraycollection.

             

            what i want is data in "sqlResult" and "ydataResult" to be set sequentially then callin my display page . such that display is correct.