Results 1 to 19 of 19

Thread: QT vs. Yahoo Finance - How to get a stock rate

  1. #1
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default QT vs. Yahoo Finance - How to get a stock rate

    Hi,
    for my little QT-project i want to write a function, which detect the current rate of stock.
    Something like "double FinanceAPI::GetStockRate(QString stock_symbol)". The committed stock symbol represents the stock.

    For these job i want to use the yahoo-finance-api. With a HTTP-request like...

    http://finance.yahoo.com/d/quotes.cs...sn&ignore=.cvs

    ...in my webbrowser, i get a right answer. It is a CSV-file with the rate of stock "847402". My problem is how get these information with a qt-function.

    This is what i try:

    Qt Code:
    1. #include "financeapi.h"
    2.  
    3. FinanceAPI::FinanceAPI(QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7. manager = new QNetworkAccessManager(this);
    8. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    9.  
    10. }
    11.  
    12.  
    13. FinanceAPI::~FinanceAPI(){
    14.  
    15. delete manager;
    16. }
    17.  
    18.  
    19.  
    20.  
    21. void FinanceAPI::replyFinished(QNetworkReply *reply)
    22. {
    23. if(reply->error()!=QNetworkReply::NoError){
    24. qDebug()<<"Fehlerhafte Anfrage\n";
    25. }
    26. else{
    27.  
    28.  
    29. QByteArray bytes = reply->readAll();
    30. QString answer = QString::fromUtf8(bytes);
    31. QFile file("test.csv");
    32. if(!file.open(QFile::WriteOnly))
    33. {
    34. qDebug()<<"test";
    35. }
    36.  
    37. //file.write(*answer);
    38. QTextStream out(&file);
    39. out<<answer;
    40. file.flush();
    41. file.close();
    42.  
    43. }
    44. }
    45.  
    46.  
    47.  
    48.  
    49.  
    50. double FinanceAPI::GetStockRate(QString stock_symbol){
    51.  
    52. QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//
    53. manager->get(QNetworkRequest(QUrl(tmpQuery)));
    54.  
    55. QNetworkRequest request;
    56. request.setUrl(QUrl(tmpQuery));
    57. manager->get(request);
    58.  
    59. return 0.0;
    60. }
    To copy to clipboard, switch view to plain text mode 


    My call of the function looks like:

    Qt Code:
    1. FinanceAPI *tmpFinance = new FinanceAPI();
    2. double rate =tmpFinance->GetStockRate("847402.DE"));
    To copy to clipboard, switch view to plain text mode 


    Somethings goes wrong respectively i don't understand the working of the...

    Qt Code:
    1. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 

    Because I think the answer from the request (-> the rate) come not in the "GetStockRate()", but rather "replyFinished()". But I needed at the end of the "GetStockRate()"-function to use it as return value.

    Thanks for helping

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    A web API request is asynchronous so you either deal with it asynchronously or you block until the asynchronous process has finished.

    If you are in a GUI application, the last is usually not an option, as the program would stop responding to any user interaction.

    Do you have a GUI program or is this a console application?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    yes, you are right. it is a "gui-program". I will use the function in two general case.

    case one: to fill a textbox, asynchron is no problem

    case two (most case): to use the value ( = the rate of the stock) for further calculation, asynchron will be a problem. I think in this case, i have to go synchron, maybe with little the help of threads to uncopple from the gui.

    the problem is the same, how can i get the return value in the invoking function? Can you maybe optimize my code? ;-)

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    the problem is the same, how can i get the return value in the invoking function?
    You cannot do that without blocking.
    This type of function does not work in a UI program.

    You have two options:

    1) asynchronous request/response, similar to how the QNetworkAccessManager requests work
    2) local cache of values that gets updated asynchronously and reacting to those changes.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Ok, then let us talk about option asynchon.How can i do that?

    I think I have to give you more details over my architecture. I have different .h/.cpp for different main functionallies. We can focus on two of them.

    first: gui.cpp
    secound: financeAPI.cpp

    Assuming that i want to calculate someting with this function in the gui

    Qt Code:
    1. double GUI::CalcSomething(double stockRate)
    To copy to clipboard, switch view to plain text mode 

    So, i have to do something with GUI::CalcSomething() and the FinanceAPI::GetStockRate()
    When i understand you right, then the following code is not good

    Qt Code:
    1. ...
    2. CalcSomething(GetStockRate("StockNameABC"));
    3. ...
    To copy to clipboard, switch view to plain text mode 

    But can I say use the value of GetStockRate() to CalcSomething()? I normally work with VisualStudio, in these case I would use functionpointer/delegates.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    I think that such a scenario would be a good :
    1. Start GetStockRate with QTimer.
    2. After receiving the data start CalcSomething with signal.

  7. #7
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    And how i realise that? I think my functions from the first post are not working correct. Maybe you can look over it.


    Qt Code:
    1. #include "financeapi.h"
    2.  
    3. FinanceAPI::FinanceAPI(QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7. manager = new QNetworkAccessManager(this);
    8. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    9.  
    10. }
    11.  
    12.  
    13. FinanceAPI::~FinanceAPI(){
    14.  
    15. delete manager;
    16. }
    17.  
    18.  
    19.  
    20.  
    21. void FinanceAPI::replyFinished(QNetworkReply *reply)
    22. {
    23. if(reply->error()!=QNetworkReply::NoError){
    24. qDebug()<<"Fehlerhafte Anfrage\n";
    25. }
    26. else{
    27.  
    28.  
    29. QByteArray bytes = reply->readAll();
    30. QString answer = QString::fromUtf8(bytes);
    31. QFile file("test.csv");
    32. if(!file.open(QFile::WriteOnly))
    33. {
    34. qDebug()<<"test";
    35. }
    36.  
    37. //file.write(*answer);
    38. QTextStream out(&file);
    39. out<<answer;
    40. file.flush();
    41. file.close();
    42.  
    43. }
    44. }
    45.  
    46.  
    47.  
    48.  
    49.  
    50. double FinanceAPI::GetStockRate(QString stock_symbol){
    51.  
    52. QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//
    53. manager->get(QNetworkRequest(QUrl(tmpQuery)));
    54.  
    55. QNetworkRequest request;
    56. request.setUrl(QUrl(tmpQuery));
    57. manager->get(request);
    58.  
    59. return 0.0;
    60. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    And how i realise that?
    One way you can wait for the asynchronous response is to use something similar to the code below. Google around for "QNetworkAccessManager synchronous" to learn the drawbacks of this approach.
    Qt Code:
    1. QNetworkReply* reply = manager->get(QNetworkRequest(QUrl(tmpQuery)));
    2. connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    3. loop.exec();
    4. QByteArray bytes = reply->readAll();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    Ok, then let us talk about option asynchon.How can i do that?
    Look at the API of QNetworkAccessManager, it uses such a call/response pattern.

    Quote Originally Posted by cit View Post
    Assuming that i want to calculate someting with this function in the gui

    Qt Code:
    1. double GUI::CalcSomething(double stockRate)
    To copy to clipboard, switch view to plain text mode 
    You would call that from the reponse handler that delivers the result of your asynchronous call.

    Quote Originally Posted by cit View Post
    But can I say use the value of GetStockRate() to CalcSomething()? I normally work with VisualStudio, in these case I would use functionpointer/delegates.
    I am not sure what the IDE has to do with what you can do in C++, but you can of course use VS to develop Qt applications.

    Cheers,
    _

  10. #10
    Join Date
    Jun 2012
    Posts
    173
    Thanks
    48
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    first in you code here you have send the request twice, was that intentially ??

    Qt Code:
    1. ]
    2. double FinanceAPI::GetStockRate(QString stock_symbol){
    3.  
    4. QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//
    5.  
    6. // first time
    7. manager->get(QNetworkRequest(QUrl(tmpQuery)));
    8.  
    9.  
    10. // 2nd time
    11. QNetworkRequest request;
    12. request.setUrl(QUrl(tmpQuery));
    13. manager->get(request);
    14.  
    15.  
    16.  
    17. return 0.0;
    18. }
    To copy to clipboard, switch view to plain text mode 


    since you must wait until the request finish and return you the reply, as well as you need to do some caluclation after getting your responce,
    I would just call the calculatoin function from replyFinished(QNetworkReply*) slot.

  11. #11
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Okay yes, this could be from my experiment.

    Can somebody look over the other function;


    Qt Code:
    1. FinanceAPI::FinanceAPI(QObject *parent) :
    2. QObject(parent)
    3. {
    4.  
    5. manager = new QNetworkAccessManager(this);
    6. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    7.  
    8. }
    9.  
    10. I don't know how to get the information out of the CSV-file. Some idea's?
    11.  
    12. FinanceAPI::~FinanceAPI(){
    13.  
    14. delete manager;
    15. }
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22. void FinanceAPI::replyFinished(QNetworkReply *reply)
    23. {
    24. if(reply->error()!=QNetworkReply::NoError){
    25. qDebug()<<"Fehlerhafte Anfrage\n";
    26. }
    27. else{
    28.  
    29.  
    30. QByteArray bytes = reply->readAll();
    31. QString answer = QString::fromUtf8(bytes);
    32. QFile file("test.csv");
    33. if(!file.open(QFile::WriteOnly))
    34. {
    35. qDebug()<<"test";
    36. }
    37.  
    38. //file.write(*answer);
    39. QTextStream out(&file);
    40. out<<answer;
    41. file.flush();
    42. file.close();
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 



    TO jthomps: In Line 4 (loop.exec() the programm will be wait till the answer is recived ?


    TO anda_skoa: Can I develop QT-application with VisualStudio?

  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    TO jthomps: In Line 4 (loop.exec() the programm will be wait till the answer is recived ?
    Yes, in the example I gave, when the QNetworkReply::finished signal is fired, it executes the QEventLoop::quit slot causing the event loop to end. That's the purpose of the connect statement.
    Last edited by jefftee; 14th January 2015 at 22:57.

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    I don't know how to get the information out of the CSV-file. Some idea's?
    If you don't need the data inside a file you can directly process it.
    QString::indexOf(), QString::mid(), QString::split() can be useful when dealing with CSV data.

    Quote Originally Posted by cit View Post
    TO jthomps: In Line 4 (loop.exec() the programm will be wait till the answer is recived ?
    Yes, but be aware that it is a crude hack that will have all kinds of consequences.
    Use it if you don't want a clean solution.

    Quote Originally Posted by cit View Post
    TO anda_skoa: Can I develop QT-application with VisualStudio?
    Yes.

    Cheers,
    _

  14. #14
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Thank you for your answers!!!! ;-)

    Today I test something.


    Qt Code:
    1. FinanceAPI::FinanceAPI(QObject *parent) :
    2. QObject(parent)
    3. {
    4.  
    5. manager = new QNetworkAccessManager(this);
    6. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyUpdateFinished(QNetworkReply*)));
    7.  
    8. }
    9.  
    10.  
    11. FinanceAPI::~FinanceAPI(){
    12.  
    13. delete manager;
    14. }
    15.  
    16.  
    17.  
    18.  
    19. void FinanceAPI::replyUpdateFinished(QNetworkReply *reply)
    20. {
    21. if(reply->error()!=QNetworkReply::NoError){
    22. qDebug()<<"Fehlerhafte Anfrage\n";
    23. }
    24. else{
    25.  
    26.  
    27. QByteArray bytes = reply->readAll();
    28. QString answer = QString::fromUtf8(bytes); //HERE I GET THE ERROR
    29. QFile file("test.csv");
    30. if(!file.open(QFile::WriteOnly))
    31. {
    32. qDebug()<<"test";
    33. }
    34. qDebug()<<"hat geklappt";
    35.  
    36. //file.write(*answer);
    37. QTextStream out(&file);
    38. out<<answer;
    39. file.flush();
    40. file.close();
    41.  
    42. }
    43. }
    44.  
    45.  
    46.  
    47.  
    48. void FinanceAPI::Update(QString stock_symbol){
    49.  
    50. QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s=847402.DE&f=l1sn&ignore=.cvs";
    51. manager->get(QNetworkRequest(QUrl(tmpQuery)));
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 





    But I get as reply only these statement (found it in the variable "answer"):

    <HTML>\n<HEAD>\n<TITLE>Error</TITLE>\n</HEAD>\n\n<BODY BGCOLOR="white" FGCOLOR="black">\n<!-- status code : 301 -->\n<!-- Error: GET -->\n<!-- host machine: yts274.global.media.ir2.yahoo.com -->\n<!-- timestamp: 1421343924.000 -->\n<!-- url: http://finance.yahoo.com/d/quotes.csv?s=847402.DE&f=l1sn&ignore=.cvs-->\n<H1>Error</H1>\n<HR>\n\n<FONT FACE="Helvetica,Arial"><B>\nDescription: Could not process this "GET" request.\n</B></FONT>\n<HR>\n</BODY>\n

    Can somebody help? Maybe i have to make some settings before i send the query.


    Added after 17 minutes:


    When i try something like this:

    http://http://stackoverflow.com/ques...yahoo-finances

    It works in my browser and i get a CSV-File. But in QT I get an error-reply like this:

    "<!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html><head><title>Yahoo! - 404 Not Found</title><style>
    /* nn4 hide */
    /*/*/
    body {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}
    html>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}
    p {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}
    #ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}
    h1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}
    form {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}
    #s1p {width:15em;margin-right:.1em;}
    form span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;}
    form .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}
    /* end nn4 hide */
    </style></head>
    <body><div id="doc">
    <div id="ygma"><a href="http://us.rd.yahoo.com/404/*http://www.yahoo.com"><img
    src=http://l.yimg.com/a/i/yahoo.gif
    width=147 height=31 border=0 alt="Yahoo!"></a><div><a
    href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo!</a>
    - <a href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Help</a></div></div>
    <div id="bd"><h1>Sorry, the page you requested was not found.</h1>
    <p>Please check the URL for proper spelling and capitalization. If
    you're having trouble locating a destination on Yahoo!, try visiting the
    <strong><a
    href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo! home
    page</a></strong> or look through a list of <strong><a
    href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/">Yahoo!'s
    online services</a></strong>. Also, you may find what you're looking for
    if you try searching below.</p>
    <form name="s1" action="http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search"><fieldset>
    <legend><label for="s1p">Search the Web</label></legend>
    <input type="text" size=30 name="p" id="s1p" title="enter search terms here">
    <input type="submit" value="Search">
    <span><a href="http://us.rd.yahoo.com/404/*http://search.yahoo.com/search/options?p=">advanced search</a> <span class=sep>|</span> <a href="http://us.rd.yahoo.com/404/*http://buzz.yahoo.com">most popular</a></span>
    </fieldset></form>
    <p class="more">Please try <strong><a
    href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Yahoo!
    Help Central</a></strong> if you need more assistance.</p>
    </div><div id="ft"><p>Copyright &copy; 2015 Yahoo! Inc.
    All rights reserved. <a
    href="http://us.rd.yahoo.com/404/*http://privacy.yahoo.com">Privacy
    Policy</a> - <a
    href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/">Terms
    of Service</a></p></div>
    </div></body></html>
    "


    Added after 21 minutes:


    The second error message is maybe my fault.


    Added after 4 minutes:


    Now i tried a totally different csv-download from an other webpage. And it's worked fine. Whats wrong with yahoo????
    Last edited by cit; 15th January 2015 at 18:43.

  15. #15
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    When using firefox web console and entering the URL:

    http://finance.yahoo.com/d/quotes.cs...sn&ignore=.cvs

    I get an HTTP/1.1 301 Redirect response, which is a permanent redirect. The Location header specifies the relocated URL is:

    http://download.finance.yahoo.com/d/...sn&ignore=.cvs

    Unless I am mistaken, you must handle redirects yourself. Look at QNetworkReply::attribute to check for the following attributes pertinent for a redirected URL:




    Hope that helps.
    Last edited by jefftee; 16th January 2015 at 03:23.

  16. #16
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    uffff...redirect...manually? That sounds not easy.Or?

    Or can I use the second link direct? Or is the second link a "dynamic" link and always another link?

  17. #17
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Quote Originally Posted by cit View Post
    uffff...redirect...manually? That sounds not easy.Or?

    Or can I use the second link direct? Or is the second link a "dynamic" link and always another link?
    The redirected URL is the permanent location for the redirect, since the HTTP return code is 301, meaning it has moved permanently. You should be able to use it instead of your original URL.

    As with other programming best practices, you should check return codes and not just assume everything works. The HTTP status code is the return code for the HTTP request that you made and you are currently ignoring it and assuming you will receive the data you expect, etc. If you were checking your HTTP status codes in your finished slot, you would have seen right away that the HTTP status of your request was HTTP 301.

    To your point about having to handle the redirect manually, I don't believe handling this is a difficult task. I already gave you the two attributes you need to use to detect an HTTP 301 status code and get the redirected URL target. All you would need to do it issue another get request for that redirected URL target.

  18. #18
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    I hope you are little bit pride of me ;-)

    Qt Code:
    1. void FinanceAPI::replyUpdateFinished(QNetworkReply *reply)
    2. {
    3.  
    4. if(reply->error())
    5. {
    6. qDebug() << "ERROR!";
    7. qDebug() << reply->errorString();
    8. }
    9. else
    10. {
    11. qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    12. qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
    13. qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
    14. qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    15. qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    16. qDebug() << reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
    17.  
    18.  
    19. //Redirect? -> Cancel this reply and send new request with new URL
    20. if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()==301)
    21. {
    22. QNetworkRequest request;
    23. request.setUrl(QUrl(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString()));
    24. manager->get(request);
    25. return;
    26. }
    27.  
    28.  
    29. QFile *file = new QFile("downloaded.txt");
    30. if(file->open(QFile::Append))
    31. {
    32. file->write(reply->readAll());
    33. file->flush();
    34. file->close();
    35. }
    36. delete file;
    37. }
    38.  
    39. reply->deleteLater();
    40. }
    To copy to clipboard, switch view to plain text mode 


    For the problem with the synchron/asynchron. I think i will use a asynchron update-function for full update on application start and store all information in a table of the database. And later, when i need some data, i pick the data synchron from the database. In my opinion, it make more sense und is easier for programming ;-)

    Thanks to all. For the help!

  19. #19
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QT vs. Yahoo Finance - How to get a stock rate

    Better, but could be improved...

    You are blindly writing out the data you receive without validating the data is in the correct format and something that you can consume. You have have other code that validates the data before you use it, but why write out potentially bogus data without validating it first?

    Good luck.

    Edit: I edited reply after realizing you are checking for an error, but data validation comment is still valid IMHO.
    Last edited by jefftee; 16th January 2015 at 20:44.

Similar Threads

  1. Yahoo! messenger like project
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2009, 18:13
  2. Stock Icons
    By wswartzendruber in forum Newbie
    Replies: 2
    Last Post: 14th February 2009, 09:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.