1

我的应用程序中有 1 个QNetworkAccessManager,我同时发出 2 个请求。当我从经理那里得到回复时,回复不是按照我打电话给他们的顺序,这是有道理的。我该如何解决这个问题?我应该让另一位经理来清除任何请求队列问题吗?

QNetworkRequest request1(ONE_GET);
request1.setRawHeader("Content-Type", "application/vnd.api+json");
request1.setRawHeader("Accept", "application/vnd.api+json");  
m_nam.get(request1);

connect(&m_nam, &QNetworkAccessManager::finished,this , &HelperClass::onReply1Recieved);

QNetworkRequest request2(TWO_GET);
request2.setRawHeader("Content-Type", "application/vnd.api+json");
request2.setRawHeader("Accept", "application/vnd.api+json");

m_nam.get(request2);

connect(&m_nam, &QNetworkAccessManager::finished,this , &HelperClass::onReply2Recieved);
4

1 回答 1

2

在您的情况下,问题是两个插槽都连接到相同的信号,因此两者都会收到通知,即使您尝试断开不能保证其正常工作的信号,解决方案是连接每个的信号QNetworkReply

QNetworkRequest request1(ONE_GET);
request1.setRawHeader("Content-Type", "application/vnd.api+json");
request1.setRawHeader("Accept", "application/vnd.api+json");  
QNetworkReply *reply1 = m_nam.get(request1);
connect(reply1, &QNetworkReply::finished, this, &HelperClass::onReply1Recieved);


QNetworkRequest request2(TWO_GET);
request2.setRawHeader("Content-Type", "application/vnd.api+json");
request2.setRawHeader("Accept", "application/vnd.api+json");
QNetworkReply *reply2 = m_nam.get(request2);
connect(reply2, &QNetworkReply::finished, this, &HelperClass::onReply2Recieved);

void HelperClass::onReply1Recieved(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    qDebug() << reply->readAll();
}

void HelperClass::onReply2Recieved(){
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    qDebug() << reply->readAll();
}
于 2018-11-06T00:48:07.133 回答