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. Not getting reply for every Request sent.
Qt 6.11 is out! See what's new in the release blog

Not getting reply for every Request sent.

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

    Hello I want to send data asynchronously to server and receive acknowledgement for every request. Whenever I am sending single request I am getting acknowledgement but if I am sending more than 1 then I am receiving response only for the last one.Thanks in advance.

    m_pendingData.push_back(jsonObj);
      if(IsConnected())
      {
         if(m_pendingData.size() > 0)
         {
             QListIterator<QJsonObject> itr(m_pendingData);
             while(itr.hasNext())
             {
                QJsonObject localJsonObj = itr.next();
                m_ackData = localJsonObj.value("Result").toObject().value("AckData");
                output = QJsonDocument(localJsonObj).toJson(QJsonDocument::Compact);
                qDebug() << "output json data:" << localJsonObj;
                   TestConnection(output);
             }
         }
    void TestConnection(QByteArray output)
    {
    
       m_manager = new QNetworkAccessManager(this);
    
       bool status = true;
       qDebug() << "Connection status:" << status;
       QNetworkRequest request(URL);
    
       request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    
       QNetworkReply *reply = m_manager->post(request,output);
       status &= static_cast<bool>(connect(m_manager, &QNetworkAccessManager::finished, [&]()
       {
          if(reply->error() == QNetworkReply::NoError)
          {
             QString answer = reply->readAll();
    
             if(m_ackData.toString() == answer)
             {
                qDebug() << "m_pendingData.size()" << m_pendingData.size();
                if(m_pendingData.size()>0)
                {
                    m_pendingData.pop_front();
                }
             }
          }
          else
          {
             qDebug()<< "ERROR:" << reply->errorString();
          }
       }));
    
       status &= static_cast<bool>(connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(Finished(QNetworkReply*))));
       qDebug() << "Connection Status:" <<status;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You create a new QNetworkAccessManager everytime you call TestConnection() which is wrong. Also your local reply variable goes out of scope due to the wrong usage of the lambda capture (&) - please read about lambda captures.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2

      • Login

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