MOC89
MOC89

Reputation: 73

How to synchronize QNetworkAccessManager?

My deleteIdList variable is coming from replyFinished(QNetworkReply*) function. But deleteIdList variable returning empty. However, it have to return anything..I want to synchronize QNetworkAccessManager..

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));

    foreach (QString delIdList, deleteIdList) {

    serviceUrl = "http://localhost:8080/limit_id="+delIdList+"&false";
    requestUrl = QString("%1%2:%3%4").arg(scheme).arg(qstr).arg(QString::number(svcPort)).arg(serviceUrl);

    QUrl url(requestUrl);
    QNetworkRequest request;
    request.setUrl(url);
    manager->get(request);

  }

How to synchronize QNetworkAccessManager ?

Upvotes: 0

Views: 1578

Answers (3)

fwoelffel
fwoelffel

Reputation: 411

I might be late, but I think it could help others facing this issue.

QNetworkAccessManager *networkMgr = new QNetworkAccessManager(this);
QNetworkReply *reply = networkMgr->get( QNetworkRequest( QUrl( "http://www.google.com" ) ) );

QEventLoop loop;
QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit()));

// Execute the event loop here, now we will wait here until readyRead() signal is emitted
// which in turn will trigger event loop quit.
loop.exec();

// Lets print the HTTP GET response.
qDebug( reply->readAll());

Source : developer.nokia.com

Upvotes: 1

Konrad Lindenbach
Konrad Lindenbach

Reputation: 4961

It's a little difficult to understand the question -- but I have some advice none-the-less which is: never use the finished signal of QNetworkAccessManager. Instead connect to the finished slot of QNetworkReply.

QNetworkAccessManager *manager = new QNetworkAccessManager(this);

foreach (QString delIdList, deleteIdList) {

    serviceUrl = "http://localhost:8080/limit_id="+delIdList+"&false";
    requestUrl = QString("%1%2:%3%4").arg(scheme).arg(qstr).arg(QString::number(svcPort)).arg(serviceUrl);

    QUrl url(requestUrl);
    QNetworkRequest request;
    request.setUrl(url);
    QNetworkReply *reply = manager->get(request);
    connect(reply,SIGNAL(finished()),this,SLOT(replyFinished()));
}

Then in the replyFinished slot you can call sender() to receive a pointer to the reply. In this way, you can "synchronize" and figure out which reply belongs to which request.

Upvotes: 0

Greenflow
Greenflow

Reputation: 3989

Erm... what are you doing? You create a QNetWorkManager, connect the finished signal to a replyFinished slot and expect to get a result, which comes from replyFinished before you even sent the request? Sorry, but from what I see in your code, I'd say it is beyond repair.

The foreach (QString delIdList, deleteIdList) must be in the replyFinished slot, when deleteIdList is somehow a result of your request. And

    QUrl url(requestUrl);
    QNetworkRequest request;
    request.setUrl(url);
    manager->get(request);

must be outside the loop below your connect. But of course, your code is so wrong, that my 'hints' are just rough guess work about what you might intend to do.

Upvotes: 0

Related Questions