Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Not getting reply for every Request sent.



  • 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;
    }
    

  • Lifetime Qt Champion

    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.


Log in to reply