Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkAccessManager finished signal error
QtWS25 Last Chance

QNetworkAccessManager finished signal error

Scheduled Pinned Locked Moved General and Desktop
qnetworkaccessmsignals emit
16 Posts 5 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 4 Offline
    4 Offline
    4j1th
    wrote on last edited by 4j1th
    #1

    I want to download a token from server, I download files from the server before but this time the only change is the code is in a new header and source file.

    updater.h :

    #ifndef UPDATER_H
    #define UPDATER_H
    
    #include <QObject>
    #include <QNetworkReply>
    #include <QSettings>
    
    class Updater : public QObject
    {
        Q_OBJECT // Enable slots and signals
    
    public:
        Updater();
        ~Updater();
    
    private slots:
        void getTokenFinished(QNetworkReply* replay);
    
    private:
        void getToken();
    
        void statusCheck();
    
        QString token;
    
        QSettings settings;
    };
    
    #endif // UPDATER_H
    
    

    updater.cpp :

    #include "updater.h"
    
    #include <QDebug>
    #include <QNetworkAccessManager>
    
    Updater::Updater()
    {
        this->getToken();
    }
    
    Updater::~Updater()
    {
    
    }
    
    void Updater::getToken()
    {
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        QNetworkRequest request;
    
        request.setUrl(QUrl("http://localhost/cdms/pro/index.php/update/token?version=3.14&"
                            "lisenceRegis=1&onlineRegis=1&parishId=1"));
    
        request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
        QNetworkReply *replay = manager->get(request);
        qDebug() << request.url();
    
        connect(manager, SIGNAL(finished(QNetworkReply*)),
                this, SLOT(getTokenFinished(QNetworkReply*)));
    }
    
    void Updater::getTokenFinished(QNetworkReply* replay)
    {
        qDebug() << "finished";
        // Checking replay
        if (replay->isReadable()) {
            // Checking errors
            if(replay->error() == QNetworkReply::NoError) {
                QByteArray strreplay = replay->readAll();
    
                qDebug() << strreplay;
                if(strreplay != "0") {
                    token = strreplay;
                    qDebug() << token;
                }
            }
        }
    }
    
    void Updater::statusCheck()
    {
    }
    
    

    php :

    $token = "dfsdfs";
    echo $token;
    

    No error in build or running the code. finished signal isn't emitted or 'void Updater::getTokenFinished(QNetworkReply* replay)' didn't run.

    Pardon my English
    Thank you.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ccleung
      wrote on last edited by
      #2

      You should connect the readyRead() and error() signals of QNetworkReply *replay.

      Please see the documentation: http://doc.qt.io/qt-5/qnetworkaccessmanager.html

      4 1 Reply Last reply
      0
      • C ccleung

        You should connect the readyRead() and error() signals of QNetworkReply *replay.

        Please see the documentation: http://doc.qt.io/qt-5/qnetworkaccessmanager.html

        4 Offline
        4 Offline
        4j1th
        wrote on last edited by 4j1th
        #3

        @ccleung but I don't get any signals

        Pardon my English
        Thank you.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          HI,

          is your request recevied from the running web server?

          What is your environment? (Qt version, OS, ...)

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          4 1 Reply Last reply
          0
          • M mcosta

            HI,

            is your request recevied from the running web server?

            What is your environment? (Qt version, OS, ...)

            4 Offline
            4 Offline
            4j1th
            wrote on last edited by
            #5

            @mcosta I test the url on a browser and it worked, it's a LAMP server

            environment : Qt 5.4.1, Ubuntu 12.04

            the same code is run on main window class fine but I can't run the same network request on custom resource/header. I want to write the data updating section code on a different resource/header file.

            Pardon my English
            Thank you.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              Hi,

              when you run your Qt code, can you see the request on the server side? (apache access_log file for instance)?

              what about remove the custom header? something changes?

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              4 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Any chance of having something like:

                void MyCoolClass::getUpate()
                {
                    Updater updater;
                    updater.getToken();
                }
                

                ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                4 1 Reply Last reply
                0
                • M mcosta

                  Hi,

                  when you run your Qt code, can you see the request on the server side? (apache access_log file for instance)?

                  what about remove the custom header? something changes?

                  4 Offline
                  4 Offline
                  4j1th
                  wrote on last edited by 4j1th
                  #8

                  @mcosta Removed the header part but nothing on access log. I think the QNetworkAccessManager no even sending any request.

                  Pardon my English
                  Thank you.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Any chance of having something like:

                    void MyCoolClass::getUpate()
                    {
                        Updater updater;
                        updater.getToken();
                    }
                    

                    ?

                    4 Offline
                    4 Offline
                    4j1th
                    wrote on last edited by
                    #9

                    @SGaist No luck.

                    Pardon my English
                    Thank you.

                    1 Reply Last reply
                    0
                    • 4 Offline
                      4 Offline
                      4j1th
                      wrote on last edited by 4j1th
                      #10

                      @SGaist @mcosta I think I got a clue, In

                      void MyCoolClass::getUpate()
                      {
                           Updater updater;
                           updater.getToken();
                          
                           this->runUpdate();
                      }
                      

                      if I run both then only 'this->runUpdate();' call will work if commented out 'this->runUpdate();' then 'updater.getToken();' works.

                      void MyCoolClass::runUpdate()
                      {
                          
                          QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                          request.setUrl(QUrl("http://localhost/cdms/pro/index.php/update/linux?version=4.14&"
                                              "lisenceRegis=1&onlineRegis=1"));
                      
                          QNetworkReply *reply = manager->get(request);
                      
                          connect(manager, SIGNAL(finished(QNetworkReply*)),
                                  this, SLOT(replyFinished(QNetworkReply*)));
                      }
                      
                      
                      void CDMS::replyFinished(QNetworkReply * replay)
                      {
                      
                          // Checking replay
                          if (replay->isReadable()) {
                      
                              // Checking errors
                              if(replay->error() == QNetworkReply::NoError) {
                                  QByteArray strreplay = replay->readAll();
                      
                                  // update ststus check
                                  if(strreplay != "0") {
                                     } else {
                                      ui->statusBar->showMessage("No new updates found", 10000);
                      
                                      // run update on intervel in 30Minute
                                      QTimer::singleShot(1800000, this, SLOT(updateOnlineTimer()));
                      
                                     
                                      // Run update on data and settings
                                      //Updater updateCDMSdata;
                                      //updateCDMSdata.getToken();
                      
                                  }
                      
                              } else {
                                  ui->statusBar->showMessage(replay->errorString(), 10000);
                      
                                  // run update on intervel in 1Minute
                                  QTimer::singleShot(60000, this, SLOT(updateOnlineTimer()));
                              }
                          }
                      }
                      

                      I want to call the 'Updater' when the 'runUpdate' download finished.

                      Pardon my English
                      Thank you.

                      1 Reply Last reply
                      0
                      • jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        You create a local instance of Updater in this method:

                        void MyCoolClass::getUpate()
                        {
                             Updater updater;
                             updater.getToken();
                            
                             this->runUpdate();
                        }
                        

                        As soon as this method finishes updater goes out of scope and is destroyed. That's why nothing is called. You must keep the instance until it finishes its work.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • 4 Offline
                          4 Offline
                          4j1th
                          wrote on last edited by
                          #12

                          Replace with this works fine

                          void MyCoolClass::getUpate()
                          {

                           Updater *updateCDMSdata = new Updater;
                          
                           this->runUpdate();
                          

                          }

                          Pardon my English
                          Thank you.

                          1 Reply Last reply
                          0
                          • jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            But now you have a memory leak: you do not delete updateCDMSdata

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              To add to @jsulm, what does runUpdate do ? Call a function on updateCDMSdata ? If so you are lucky id doesn't crash.

                              If you will be using Updater several times then make it a member of your MyCoolClass object and don't forget to handle its deletion.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              4 1 Reply Last reply
                              1
                              • SGaistS SGaist

                                To add to @jsulm, what does runUpdate do ? Call a function on updateCDMSdata ? If so you are lucky id doesn't crash.

                                If you will be using Updater several times then make it a member of your MyCoolClass object and don't forget to handle its deletion.

                                4 Offline
                                4 Offline
                                4j1th
                                wrote on last edited by 4j1th
                                #15

                                @SGaist , runUpdate used to update the software and updateCDMSdata used to update data. I want to run data update when there is no software update, first check software update then from the 'finished(QNetworkReply*)' signal data update started.

                                I need to call updater several times, so I think use Updater as a member and delete on MyCoolClass::~MyCoolClass() is a better idea ?

                                Pardon my English
                                Thank you.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Since it's a QObject you can use the parent/child paradigm to let Qt handle the deletion for you.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved