PDA

View Full Version : network request more than one



mero
11th March 2011, 16:28
How can I read many sites ?

Now I'm using this:



QByteArray DlgMyAvatar::get_avatar(QString strUrl)
{
QEventLoop eventLoop;
QNetworkAccessManager *accessManager = new QNetworkAccessManager;
QNetworkReply *pReply = accessManager->get(QNetworkRequest(QUrl(strUrl)));
QObject::connect(pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
eventLoop.exec();

accessManager->deleteLater();
pReply->deleteLater();

if (pReply->error())
return QByteArray();

QByteArray bData = pReply->readAll();

if (bData.isEmpty() == false)
return bData;

return QByteArray();
}

....
for (int i = 0; i < lAvatars.count(); i++)
{
QByteArray bAvatar = get_avatar(lAvatars.at(i));
....


but sometimes it "hangup" on reading, then i've got problem :)

Of course I can convert it to something like this:



void DlgMyAvatar::get_avatar(QString strUrl)
{
pReply = accessManager->get(QNetworkRequest(QUrl(strUrl)));
QObject::connect(pReply, SIGNAL(finished()), this, SLOT(read_avatar()));
}

void DlgMyAvatar::read_avatar()
{
accessManager->deleteLater();
pReply->deleteLater();

if (pReply->error())
return QByteArray();

QByteArray bData = pReply->readAll();

// show bData ...
}


but it can only read one avatar/site ...
So how can I convert it to reading many sites in FOR loop ?

wysota
11th March 2011, 16:42
but it can only read one avatar/site ...
Why is that?

mero
11th March 2011, 16:51
Why is that?

because when I'll use get_avatar (without eventloop) in FOR, then read_avatar do not know/remember what was original url

wysota
11th March 2011, 17:40
The original url is stored in the QNetworkReply object.

mero
11th March 2011, 18:47
The original url is stored in the QNetworkReply object.

how can i read this url ? what function ?


also if I have one QNetworkReply in .h:


private:
QNetworkAccessManager *accessManager;
QNetworkReply *pReply;


than if I will use get_avatar x times, than pReply in read_avatar will have original url of each request ? or not because it is only one variable i header ?

wysota
11th March 2011, 19:14
how can i read this url ? what function ?
See QNetworkReply::request() and QNetworkRequest::url().



also if I have one QNetworkReply in .h:


private:
QNetworkAccessManager *accessManager;
QNetworkReply *pReply;


than if I will use get_avatar x times, than pReply in read_avatar will have original url of each request ? or not because it is only one variable i header ?
You don't need this variable there. QNetworkAccessManager will give you a pointer to the reply object with the finished() signal.

mero
11th March 2011, 19:54
See QNetworkReply::request() and QNetworkRequest::url().

You don't need this variable there. QNetworkAccessManager will give you a pointer to the reply object with the finished() signal.

.h


private:
QNetworkAccessManager *avatarAccessManager;
QNetworkReply *pAvatarReply;


.cpp


... (constructor)...
{
avatarAccessManager = new QNetworkAccessManager;
}

...(destructor) ...
{
avatarAccessManager->deleteLater();
}

void DlgMyAvatar::get_avatar(QString strUrl)
{
pAvatarReply = avatarAccessManager->get(QNetworkRequest(QUrl(strUrl)));
QObject::connect(pAvatarReply, SIGNAL(finished()), this, SLOT(avatar_finished()));
}

void DlgMyAvatar::avatar_finished()
{
// prevent crash (?)
//pAvatarReply->deleteLater();

if (pAvatarReply->error())
return;

QString strUrl = pAvatarReply->url().toString();
QByteArray bData = pAvatarReply->readAll();

if ((strUrl.isEmpty() == false) && (bData.isEmpty() == false))
{
qDebug() << "got avatar data for url:" << strUrl;
//return bData;
}
}


but it is not correct because i've got only ONE first result. why?
also when i should deleteLayer for pAvatarReply ?

wysota
11th March 2011, 19:56
Because you're using a variable that you are constantly overwriting. What exactly did you expect here?

mero
11th March 2011, 19:58
Because you're using a variable that you are constantly overwriting. What exactly did you expect here?

so how I can do it without overwriting ? any simple example? (or fixed my code)

wysota
11th March 2011, 20:26
I have already told you.

QNetworkAccessManager will give you a pointer to the reply object with the finished() signal.

mero
11th March 2011, 20:44
thank you it is working :)
but in the finished signal do i need to deleteLayer of reply ?

wysota
11th March 2011, 20:48
It's up to you what to do with it.