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. [SOLVED] QNetworkReply - Retrying request gracefully

[SOLVED] QNetworkReply - Retrying request gracefully

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.6k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    I'm using quite a lot of QNetworkReply to retrieve important data for the execution of my software.

    In case the Request fail, I would like to try it again until it succeed
    The QNetworkReply documentation says that finished should trigger even if an error occurred, quote on the error signal "This signal is emitted when the reply detects an error in processing. The finished() signal will probably follow, indicating that the connection is over"

    can someone confirm this? I don't like the "probably follow" I want to be sure that my data is retrieved. I would like to redo the request again if I detect it failed (no data was retrieved from the request). I want to make sure that finished() is called no matter what so I can retry it again in my slot.

    Code example :

        // Retrieve achievement list from DB
        replyGetListAchievement = AchievementDAO::getLstAchievement();
        connect(replyGetListAchievement, SIGNAL(finished()), this, SLOT(slotGetAchievementListFinished()) );
    

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void ManagerAchievement::slotGetAchievementListFinished() {

        QByteArray arrayData =  replyGetListAchievement->readAll();
        QString replyMsg(arrayData);
    
        lstAchievement =  Util::parseJsonAchievementList(replyMsg);
    
        //request had a problem (should have data) - Redo request
        if (lstAchievement.size() < 1) {
            replyGetListAchievement = AchievementDAO::getLstAchievement();
            connect(replyGetListAchievement, SIGNAL(finished()), this, SLOT(slotGetAchievementListFinished()) );
        }
        else {
            replyGetListAchievementForUser = AchievementDAO::getLstAchievementForUser(account->id);
            connect(replyGetListAchievementForUser, SIGNAL(finished()), this, SLOT(slotGetAchievementListForUserFinished()) );
            replyGetListAchievement->deleteLater();
        }
    
    }
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      You should check if there was an error or not with QNetworkReply->error():

      if (replyGetListAchievement->error() == QNetworkReply::NoError) {
              //success, process data
      } else {
              qDebug() << "Failure"  << replyGetListAchievement->errorString();
             // emit signal about error and possibly retry
      }
      
      M 1 Reply Last reply
      1
      • M mchinand

        You should check if there was an error or not with QNetworkReply->error():

        if (replyGetListAchievement->error() == QNetworkReply::NoError) {
                //success, process data
        } else {
                qDebug() << "Failure"  << replyGetListAchievement->errorString();
               // emit signal about error and possibly retry
        }
        
        M Offline
        M Offline
        maximus
        wrote on last edited by maximus
        #3

        @mchinand

        Thanks a lot, that is exactly what I wanted to do (avoiding to code another slot to catch the Error signal) didn't know you could check it like this after finished() was emitted.

        void ManagerAchievement::slotGetAchievementListFinished() {
        
        
            //success, process data
            if (replyGetListAchievement->error() == QNetworkReply::NoError) {
                qDebug() << "no error process data achievement list!";
                QByteArray arrayData =  replyGetListAchievement->readAll();
                QString replyMsg(arrayData);
                lstAchievement =  Util::parseJsonAchievementList(replyMsg);
                qDebug() << "slotGetAchievementListFinished" << lstAchievement.size();
        
                replyGetListAchievement->deleteLater();
                replyGetListAchievementForUser = AchievementDAO::getLstAchievementForUser(account->id);
                connect(replyGetListAchievementForUser, SIGNAL(finished()), this, SLOT(slotGetAchievementListForUserFinished()) );
            }
            // error, retry request
            else {
                qDebug() << "Problem getting achievement list! retry again..." << replyGetListAchievement->errorString();
                replyGetListAchievement = AchievementDAO::getLstAchievement();
                connect(replyGetListAchievement, SIGNAL(finished()), this, SLOT(slotGetAchievementListFinished()) );
            }
        
        }
        

        Free Indoor Cycling Software - https://maximumtrainer.com

        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