Results 1 to 8 of 8

Thread: QNetworkAccessManager and multiples requests

  1. #1
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QNetworkAccessManager and multiples requests

    Hi all,

    I want to connect to a web service so i used QNetworkAccessManager, and it worked and i got the informations i wanted.
    The problem is i want to reconnect to the web service every time to get the currents informations, i was thinking to use a thread to do this, but it doesn't work , the slot connecting to the finish of the QNetworkAccessManager is not invoked until the end of the thread.

    This is the code i used to connect to the web service one time :
    Qt Code:
    1. m_url.setUrl("http://time.jsontest.com/");
    2. m_request.setUrl(m_url);
    3. connect(&m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*)));
    4. m_networkManager.get(m_request);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and multiples requests

    So what exactly is the problem? Why can't you call m_networkManager.get(m_request) again?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and multiples requests

    I did it, i put m_networkManager.get(m_request); into a method run() of QThread , but the problem is the slot onResult(QNetworkReply*) is not invoked.

    Qt Code:
    1. void Manager::run()
    2. {
    3. while(true)
    4. {
    5. m_networkManager.get(m_request);
    6. sleep(10);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and multiples requests

    Well, obviously the above code won't work because you have an infinite loop here. But why use a thread at all?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and multiples requests

    as the title says , i want to reconnect again to the web service , how i can do it without thread ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkAccessManager and multiples requests

    Qt Code:
    1. #include <QNetworkAccessManager>
    2. #include <QCoreApplication>
    3. #include <QTimer>
    4. #include <QtDebug>
    5.  
    6. class ServiceConnector : public QObject {
    7. Q_OBJECT
    8. public:
    9. ServiceConnector(const QUrl &url, QObject *parent = 0) : QObject(parent), m_url(url) {
    10. connect(&m_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(processResult(QNetworkReply*)));
    11. connect(&m_timer, SIGNAL(timeout()), this, SLOT(nextQuery()));
    12. }
    13. public slots:
    14. void start(int ms = 1000) {
    15. m_timer.start(ms);
    16. }
    17. private slots:
    18. void nextQuery() {
    19. m_manager.get(QNetworkRequest(m_url));
    20. }
    21. void processResult(QNetworkReply *reply) {
    22. qDebug() << reply->readAll();
    23. reply->deleteLater();
    24. }
    25. private:
    26. QNetworkAccessManager m_manager;
    27. QUrl m_url;
    28. QTimer m_timer;
    29. };
    30.  
    31. #include "main.moc"
    32.  
    33. int main(int argc, char **argv) {
    34. QCoreApplication app(argc, argv);
    35. ServiceConnector connector("your webservice url");
    36. connector.start(1000);
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    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: QNetworkAccessManager and multiples requests

    You could at least have provided a link to the other thread so Wysota didn't have to re-ask all the same questions again.

    You are starting to look like a troll.

    Cheers,
    _

  8. #8
    Join Date
    Dec 2013
    Location
    Jerada, Morroco
    Posts
    106
    Thanks
    11
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and multiples requests

    I'm sorry i didn't mean it, when posting this Thread i saw the other , so i asked because it treat the same topic, i'm sorry again and thanx you two for your help

Similar Threads

  1. Multiple requests with QNetworkAccessManager
    By ljuhrich in forum Qt Programming
    Replies: 7
    Last Post: 7th June 2013, 09:34
  2. Multiple QNetworkAccessManager requests.
    By Diath in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2011, 23:04
  3. Replies: 4
    Last Post: 19th January 2011, 14:49
  4. Multiples QSqlQueryModel and QTableView
    By Netheril in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2010, 19:38
  5. QHttp delaying requests
    By etru1927 in forum Qt Programming
    Replies: 8
    Last Post: 29th April 2008, 22:52

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.