Multiple requests with QNetworkAccessManager
-
I have a class called
NYTimes
that class has a method calledparse(QVector<QString> addresses, const QString &word)
, that method will receive a list of addresses. NYTimes is a class that looks for a specific word or tag on the content of a page, for example: Does that article from NYTimes contain a tag or word calledsports
on it? If yes, then I want to return the address that has the word.As you can see on the title I want to create multiple requests cause it's a list of addresses and I don't want to wait each one to download, parse and then return.
The question is: How to get the list of addresses, download, parse (check for the word), concurrently and then return another
QVector<QString>
with ONLY the addresses that went right? -
@Defohin said in Multiple requests with QNetworkAccessManager:
s a method called parse(QVector<QString> addresses, const QString &word), that method will receive a list of addresses. NYTimes is a class that looks for a specific word or tag on the content of a p
connect a slot to the
QNetworkAccessManagers::finished(QNetworkReply*)
signal. Check for errors and parse the reply. If it contains the word add thereply->url()
to a list. -
As I have a QVector I have to use foreach, do I have to connect all the QNetworkAccessManager::get to the same finished slot?
-
@Defohin
you should something like this:QNetworkAccessManager* nam = new QNetworkAccessManager( this ); connect( nam, &QNetworkAccessManager::finished, this, &NYTimes::onReplyFinished ) foreach( QString address, addresses ) { nam->get( address ); } .... void NYTimes::onReplyFinished( QNetworkReply* reply ) { switch( reply->error() ) { case QNetworkReply::NoError: // parse reply->readAll(); break; case QNetworkReply::XXX: //error handling break; } }
Do you use a recent compiler (C++11)?
-
Yes, I'm using VS2015