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. Cant get QNetworkReply error continuosly...

Cant get QNetworkReply error continuosly...

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 407 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.
  • B Offline
    B Offline
    bladekel
    wrote on last edited by bladekel
    #1

    Hello to all,

    I need to get xml information from a web page and I can.

    My problem is, I couldnt catch the error signals continuosly. I only catch it once.

    Here are my codes;

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QDebug>
    #include <QTimer>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        QNetworkReply *reply;
        QNetworkRequest request;
        QNetworkAccessManager *manager;
        QTimer *timer;
    
    private slots:
        void replyFinished(QNetworkReply*);
        void updateDB();
        void slotError(QNetworkReply::NetworkError);
        void slotSslErrors(QList<QSslError>);
    };
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        manager = new QNetworkAccessManager(this);
    
        request.setUrl(QUrl("https://www.w3schools.com/xml/note.xml"));
    
        // Get response
        updateDB();
    
        // Signals for xml
        connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
        connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
    
        // Update timer
        timer = new QTimer(this);
        timer->start(2000);
        connect(timer,SIGNAL(timeout()),this,SLOT(updateDB()));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::replyFinished(QNetworkReply *)
    {
        qDebug() << reply->readAll();
        reply->deleteLater();
    }
    
    void MainWindow::updateDB()
    {
        reply = manager->get(request);
    }
    
    void MainWindow::slotError(QNetworkReply::NetworkError)
    {
        qDebug() << "slotError";
        reply->deleteLater();
    }
    
    void MainWindow::slotSslErrors(QList<QSslError>)
    {
        qDebug() << "slotSslErrors";
        reply->deleteLater();
    }
    
    
    

    Note: - I'm using the timer for getting the information continuously.
    - I'm using QT 5.13.1 and I have added QT += network on .pro file

    raven-worxR 1 Reply Last reply
    0
    • B bladekel

      Hello to all,

      I need to get xml information from a web page and I can.

      My problem is, I couldnt catch the error signals continuosly. I only catch it once.

      Here are my codes;

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QNetworkAccessManager>
      #include <QNetworkReply>
      #include <QDebug>
      #include <QTimer>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          QNetworkReply *reply;
          QNetworkRequest request;
          QNetworkAccessManager *manager;
          QTimer *timer;
      
      private slots:
          void replyFinished(QNetworkReply*);
          void updateDB();
          void slotError(QNetworkReply::NetworkError);
          void slotSslErrors(QList<QSslError>);
      };
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          manager = new QNetworkAccessManager(this);
      
          request.setUrl(QUrl("https://www.w3schools.com/xml/note.xml"));
      
          // Get response
          updateDB();
      
          // Signals for xml
          connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
          connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(slotError(QNetworkReply::NetworkError)));
          connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
      
          // Update timer
          timer = new QTimer(this);
          timer->start(2000);
          connect(timer,SIGNAL(timeout()),this,SLOT(updateDB()));
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::replyFinished(QNetworkReply *)
      {
          qDebug() << reply->readAll();
          reply->deleteLater();
      }
      
      void MainWindow::updateDB()
      {
          reply = manager->get(request);
      }
      
      void MainWindow::slotError(QNetworkReply::NetworkError)
      {
          qDebug() << "slotError";
          reply->deleteLater();
      }
      
      void MainWindow::slotSslErrors(QList<QSslError>)
      {
          qDebug() << "slotSslErrors";
          reply->deleteLater();
      }
      
      
      

      Note: - I'm using the timer for getting the information continuously.
      - I'm using QT 5.13.1 and I have added QT += network on .pro file

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @bladekel
      i think the issue is that you simply overwrite the same reply pointer each time the timer fires, but the signal-connections are still available.
      So if your requests needs longer than 2s (which is very likely when u expect a timeout error) it rather deletes the last created reply, instead the reply object actually created the error.

      You shouldn't start the next request not before the last one has triggered it's finished signal at least.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      B 1 Reply Last reply
      3
      • raven-worxR raven-worx

        @bladekel
        i think the issue is that you simply overwrite the same reply pointer each time the timer fires, but the signal-connections are still available.
        So if your requests needs longer than 2s (which is very likely when u expect a timeout error) it rather deletes the last created reply, instead the reply object actually created the error.

        You shouldn't start the next request not before the last one has triggered it's finished signal at least.

        B Offline
        B Offline
        bladekel
        wrote on last edited by
        #3

        @raven-worx Actually my timer loop will be 24hours. So you suggest me to create signal everytime I call request but wont it be problem after a while because there will be lots of signals ???

        raven-worxR 1 Reply Last reply
        0
        • B bladekel

          @raven-worx Actually my timer loop will be 24hours. So you suggest me to create signal everytime I call request but wont it be problem after a while because there will be lots of signals ???

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @bladekel

          timer->start(2000);

          2000ms = 2s

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

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