network request more than one
How can I read many sites ?
Now I'm using this:
Code:
{
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())
if (bData.isEmpty() == false)
return bData;
}
....
for (int i = 0; i < lAvatars.count(); i++)
{
....
but sometimes it "hangup" on reading, then i've got problem :)
Of course I can convert it to something like this:
Code:
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())
// show bData ...
}
but it can only read one avatar/site ...
So how can I convert it to reading many sites in FOR loop ?
Re: network request more than one
Quote:
Originally Posted by
mero
but it can only read one avatar/site ...
Why is that?
Re: network request more than one
Quote:
Originally Posted by
wysota
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
Re: network request more than one
The original url is stored in the QNetworkReply object.
Re: network request more than one
Quote:
Originally Posted by
wysota
how can i read this url ? what function ?
also if I have one QNetworkReply in .h:
Code:
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 ?
Re: network request more than one
Quote:
Originally Posted by
mero
how can i read this url ? what function ?
See QNetworkReply::request() and QNetworkRequest::url().
Quote:
also if I have one
QNetworkReply in .h:
Code:
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.
Re: network request more than one
Quote:
Originally Posted by
wysota
.h
Code:
private:
QNetworkAccessManager *avatarAccessManager;
QNetworkReply *pAvatarReply;
.cpp
Code:
... (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();
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 ?
Re: network request more than one
Because you're using a variable that you are constantly overwriting. What exactly did you expect here?
Re: network request more than one
Quote:
Originally Posted by
wysota
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)
Re: network request more than one
I have already told you.
Quote:
Originally Posted by wysota
QNetworkAccessManager will give you a pointer to the reply object with the finished() signal.
Re: network request more than one
thank you it is working :)
but in the finished signal do i need to deleteLayer of reply ?
Re: network request more than one
It's up to you what to do with it.