Skip navigation
voldy2003
Currently Being Moderated

multiple HTTP Requests in for loop response handling

Apr 14, 2013 4:41 AM

Tags: #flex #actionscript #flex-4.6 #datagrid #httpservice

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.

 
Replies
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Apr 15, 2013 9:34 AM   in reply to voldy2003

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points