Results 1 to 4 of 4

Thread: Issue in downloading with QNetworkAccessManager

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Issue in downloading with QNetworkAccessManager

    Hi, i'm making an currency converter app.
    I go online, download a html and save it into a txt file, which i parse afterwards for the exchange rates.

    .h
    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QNetworkAccessManager>
    6. #include <QNetworkRequest>
    7. #include <QNetworkReply>
    8. #include <QUrl>
    9. #include <QFile>
    10. #include <QDebug>
    11. #include <QMessageBox>
    12. #include <string>
    13. #include <Windows.h>
    14. #include <qstring.h>
    15.  
    16.  
    17. class downloader : public QObject
    18. {
    19. Q_OBJECT
    20. public:
    21. explicit downloader(QObject *parent = 0);
    22.  
    23. void Do_download();
    24.  
    25. public slots:
    26. void replyFinished (QNetworkReply *reply);
    27.  
    28. private:
    29. QNetworkAccessManager *manager;
    30.  
    31. };
    To copy to clipboard, switch view to plain text mode 

    .cpp

    Qt Code:
    1. #include "downloader.h"
    2. #include "strings.h"
    3. #include <string>
    4. #include <Windows.h>
    5.  
    6. downloader::downloader(QObject *parent) :
    7. QObject(parent)
    8. {
    9.  
    10. }
    11.  
    12.  
    13. void downloader::Do_download()
    14. {
    15. manager = new QNetworkAccessManager(this);
    16.  
    17. connect(manager, SIGNAL(finished(QNetworkReply*)),
    18. this,SLOT(replyFinished(QNetworkReply*)));
    19.  
    20. manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
    21. }
    22.  
    23. void downloader::replyFinished(QNetworkReply *reply)
    24. {
    25. if(reply->error())
    26. {
    27. QMessageBox msgBox;
    28. msgBox.setText(strings::msgBoxDownReplyError);
    29. msgBox.exec();
    30. }
    31. else
    32. {
    33.  
    34. QFile *file = new QFile("C:/Qt/test/downloaded.txt");
    35. QByteArray data = reply->readAll();
    36.  
    37. if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
    38. {
    39. file->write(data);
    40. file->flush();
    41. file->close();
    42. }
    43. delete file;
    44.  
    45. }
    46. reply->deleteLater();
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

    parser.h

    Qt Code:
    1. #ifndef PARSER_H
    2. #define PARSER_H
    3.  
    4.  
    5. #include <QMessageBox>
    6. #include <QStringList>
    7. #include <string>
    8. #include <sstream>
    9. #include <stdlib.h>
    10. #include <fstream>
    11. #include <iostream>
    12. #include <algorithm>
    13. #include <string>
    14. #include <Windows.h>
    15.  
    16.  
    17. class parser
    18.  
    19. {
    20.  
    21. public:
    22. explicit parser();
    23.  
    24. QStringList currency_list;
    25. std::list<std::string> curr_list;
    26.  
    27. void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
    28. int read_line(std::map<std::string,double> &my_map);
    29.  
    30. void init_list();
    31.  
    32. std::string getExePathParser();
    33.  
    34. private:
    35.  
    36. };
    37.  
    38. #endif // PARSER_H
    To copy to clipboard, switch view to plain text mode 


    parser.cpp

    Qt Code:
    1. #include "parser.h"
    2. #include "calculator.h"
    3. #include "strings.h"
    4. #include <string>
    5.  
    6. parser::parser()
    7. {
    8. init_list();
    9.  
    10. }
    11. void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
    12. {
    13. double curr;
    14. std::string temp;
    15.  
    16. temp = line.substr(52,8);
    17.  
    18. std::replace(temp.begin(),temp.end(),',','.');
    19.  
    20. curr = std::stod(temp,NULL);
    21.  
    22. my_map.insert(std::make_pair(curr_string,curr));
    23.  
    24. }
    25.  
    26.  
    27. int parser::read_line(std::map<std::string,double> &my_map)
    28. {
    29.  
    30. std::list<std::string>::iterator iter;
    31. std::string linija;
    32. std::ifstream file_("C:/Qt/test/downloaded.txt");
    33.  
    34. if(file_.fail())
    35. {
    36. QMessageBox msgBox;
    37. msgBox.setText(strings::msgBoxOpeningError);
    38. msgBox.exec();
    39. }
    40.  
    41. else if(file_.is_open())
    42. {
    43. while(std::getline(file_,linija))
    44. {
    45. for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
    46. {
    47. if(linija.find(*iter) != std::string::npos)
    48. process_line(linija, my_map, *iter);
    49. }
    50. }
    51. file_.close();
    52. }
    53.  
    54. return 0;
    55. }
    56.  
    57.  
    58.  
    59. void parser::init_list()
    60. {
    61. currency_list << "HRK" << "CAD" << "CZK" << "DKK" << "HUF"
    62. << "JPY" << "NOK" << "SEK" << "CHF" << "GBP"
    63. << "USD" << "EUR" << "PLN" << "AUD";
    64. curr_list.push_back("HRK");
    65. curr_list.push_back("CAD");
    66. curr_list.push_back("CZK");
    67. curr_list.push_back("DKK");
    68. curr_list.push_back("HUF");
    69. curr_list.push_back("JPY");
    70. curr_list.push_back("NOK");
    71. curr_list.push_back("SEK");
    72. curr_list.push_back("CHF");
    73. curr_list.push_back("GBP");
    74. curr_list.push_back("USD");
    75. curr_list.push_back("EUR");
    76. curr_list.push_back("PLN");
    77. curr_list.push_back("AUD");
    78.  
    79.  
    80. }
    To copy to clipboard, switch view to plain text mode 

    and my main

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. std::map<std::string,double> currency_map;
    6.  
    7.  
    8. downloader d;
    9. d.Do_download();
    10.  
    11.  
    12. parser p;
    13. p.read_line(currency_map);
    14.  
    15.  
    16. MainWindow w(currency_map);
    17. w.show();
    18.  
    19.  
    20. return a.exec();
    21. };
    To copy to clipboard, switch view to plain text mode 

    It was working good, but then i changed the path for the .txt file i create.
    And the first time i run the app, i get a msgBoxError from my parser class
    Qt Code:
    1. if(file_.fail())
    2. {
    3. QMessageBox msgBox;
    4. msgBox.setText(strings::msgBoxOpeningError);
    5. msgBox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    this part of code pops up.
    then i shut it down. run the app again, and it's all working well.
    so everytime i change the file path, the first running of the app wont work.
    can someone help me out, and spot what's wrong here?

    (i have to use basic c++ in some parts of the code, don't ask)

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

    Default Re: Issue in downloading with QNetworkAccessManager

    You call Do_download() and immediately try to parse it.
    Since downloading something is not instantanious and you write the file after the download finished, there is no file to parse when you attempt to.

    I can recommend adding some qDebug() logging into the application when things like that happen, it would have told you that this is the sequence of how things currently happen.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: Issue in downloading with QNetworkAccessManager

    I can't seem to get this to work.

    .h

    Qt Code:
    1. #ifndef DOWNLOADER_H
    2. #define DOWNLOADER_H
    3.  
    4. #include <QObject>
    5. #include <QNetworkAccessManager>
    6. #include <QNetworkRequest>
    7. #include <QNetworkReply>
    8. #include <QUrl>
    9. #include <QFile>
    10. #include <QDebug>
    11. #include <QMessageBox>
    12. #include <string>
    13. #include <Windows.h>
    14. #include <qstring.h>
    15.  
    16. class downloader : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. explicit downloader(QObject *parent = 0);
    21.  
    22. void Do_download();
    23.  
    24. std::string getExePath();
    25.  
    26. void Do_parse(bool download, QNetworkReply *reply);
    27.  
    28. public slots:
    29. void replyFinished (QNetworkReply *reply);
    30.  
    31. private:
    32. QNetworkAccessManager *manager;
    33. bool downloadSuccess;
    34.  
    35. };
    36.  
    37. #endif // DOWNLOADER_H
    To copy to clipboard, switch view to plain text mode 

    i added a bool variable, to check if download passed, and a Do_parse function to call afterwards.

    .cpp

    Qt Code:
    1. #include "downloader.h"
    2. #include "strings.h"
    3. #include <string>
    4. #include <Windows.h>
    5.  
    6. downloader::downloader(QObject *parent) :
    7. QObject(parent)
    8. {
    9. downloadSuccess = false;
    10. }
    11.  
    12.  
    13. void downloader::Do_download()
    14. {
    15. manager = new QNetworkAccessManager(this);
    16.  
    17. connect(manager, SIGNAL(finished(QNetworkReply*)),
    18. this,SLOT(replyFinished(QNetworkReply*)));
    19.  
    20. manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
    21.  
    22. Do_parse(downloadSuccess, reply); // this doesn't work
    23. }
    24.  
    25. void downloader::replyFinished(QNetworkReply *reply)
    26. {
    27.  
    28. if(reply->error())
    29. {
    30. QMessageBox msgBox;
    31. msgBox.setText(strings::msgBoxDownReplyError);
    32. msgBox.exec();
    33. downloadSuccess = false;
    34. }
    35. else
    36. {
    37.  
    38. downloadSuccess = true;
    39.  
    40. }
    41. }
    42.  
    43. void downloader::Do_parse(bool download, QNetworkReply *reply)
    44. {
    45. if(download==true)
    46. {
    47. QFile *file = new QFile(strings::filePathQt);
    48. QByteArray data = reply->readAll();
    49.  
    50. if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
    51. {
    52. file->write(data);
    53. file->flush();
    54. file->close();
    55. }
    56. delete file;
    57.  
    58. reply->deleteLater();
    59. }
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 

    I thought of something like this. but i guess i'm missing something about the reply thing.
    Am i on the right track here?
    Is it possible to send the reply into another function like i tried in my code?

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

    Default Re: Issue in downloading with QNetworkAccessManager

    You schedule the download (QNAM::get()) and then call Do_parse, before the QNAM even had the chance to do anything.

    replyFinished() is obviously a place where code gets executed after the download finished, so there is where you need to call Do_parse.
    Not before the download happens.

    Cheers,
    _

Similar Threads

  1. QNetworkAccessManager proxy issue
    By dholliday in forum Qt Programming
    Replies: 12
    Last Post: 13th November 2017, 05:29
  2. Replies: 3
    Last Post: 27th June 2015, 04:00
  3. QWebPluginFactory::create issue with QNetworkAccessManager
    By brcontainer in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2013, 19:32
  4. Downloading a page with QNetworkAccessManager
    By TempleClause in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2012, 20:58
  5. Downloading multiple files using QNetworkAccessManager
    By aurorius in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2009, 10:51

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.