PDA

View Full Version : QNetworkReply::readAll() empty or not depending on calling position



AmirH
23rd May 2015, 17:06
Hi,

I will show u 2 situations, and the difference between those two is only a switch concerning 2 lignes

code 1 :


Window::Window()
{
AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";


for(int i=0;i<AppVars::urlRSSlist.size();i++){
qDebug() << "enter for loop for i = "+QString::number(i);
netManList<< new QNetworkAccessManager(this);
listRequest<<QNetworkRequest(QUrl(AppVars::urlRSSlist[i]));
listReply<<netManList[i]->get(listRequest[i]);
}
QObject::connect (listReply[0], SIGNAL(readyRead()), this, SLOT(downloadedRSSalaune())) ;

}

void Window::downloadedRSSalaune(){

qDebug() << listReply[0]->errorString();

if(listReply[0]->error() != QNetworkReply::NoError)
return;
//HERE
QByteArray data =QByteArray(listReply[0]->readAll());
qDebug() <<listReply[0]->readAll();

int statusCode = listReply[0]->attribute(QNetworkRequest::HttpStatusCodeAttribute ).toInt();
qDebug() << QVariant(statusCode).toString();
}

code 2 :


Window::Window()
{
AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";


for(int i=0;i<AppVars::urlRSSlist.size();i++){
qDebug() << "enter for loop for i = "+QString::number(i);
netManList<< new QNetworkAccessManager(this);
listRequest<<QNetworkRequest(QUrl(AppVars::urlRSSlist[i]));
listReply<<netManList[i]->get(listRequest[i]);
}
QObject::connect (listReply[0], SIGNAL(readyRead()), this, SLOT(downloadedRSSalaune())) ;

}

void Window::downloadedRSSalaune(){

qDebug() << listReply[0]->errorString();

if(listReply[0]->error() != QNetworkReply::NoError)
return;
//AND HERE
qDebug() <<listReply[0]->readAll();
QByteArray data =QByteArray(listReply[0]->readAll());


int statusCode = listReply[0]->attribute(QNetworkRequest::HttpStatusCodeAttribute ).toInt();
qDebug() << QVariant(statusCode).toString();
}

as you can see, only qDebug() and QByteArray calling order differ between those code, and I've got 2 different results :

result 1 :

"enter for loop for i = 0"
"Unknown error"
""
"200"

result 2 :

"enter for loop for i = 0"
"Unknown error"
"<?xml [................................. lot of lines...............]"
"200"

What's going on here ?

N.B. :
QList<QNetworkAccessManager*> netManList;
QList<QNetworkRequest> listRequest;
QList<QNetworkReply*> listReply;

anda_skoa
23rd May 2015, 17:57
In the first case you call readAll() and store the result in a QByteArray. Then you call readAll() again which obviously will return nothing (an empty byte array).
In the second case you call readAll() and write it to qDebug(). Then you call readAll() again to store nothing in the QByteArray variable data.

Not sure why you are creating multiple network access managers or why you are storing the network requests or why you are only connecting to one of the network replys.

Cheers,
_

AmirH
24th May 2015, 05:56
In fact, I don't really manage what I do with downloading files but first I wanted to make some test.

I put code in Window::Window() but in fact I want to put the code in a slot of Window (wich derivate from QWidget), then from a custom QThread, I will emit 9 signals, spread on 9 minutes. Each emitted signal will make Window to download a rss file (here "http://rss.lemonde.fr/c/205/f/3050/index.rss" is the first file) and the content of each rss file will be added to a unique local xml file with QDomDocument functions.

If someone could help me with the download part, it would be great.

Still I don't understand why the QNetworkReply doesnot store anymore any data, why a readAll() makes it empty ? Is it a concept?

anda_skoa
24th May 2015, 11:08
Still I don't understand why the QNetworkReply doesnot store anymore any data, why a readAll() makes it empty ? Is it a concept?

If you have a table full of cake and you eat all cake, how much cake will be left?

Cheers,
_

AmirH
24th May 2015, 14:12
ok, this is the meaning of readAll(), I did not understand at first, I though it copied the content and not tranfer it.

And for my project, Do I need only one QNetworkAccessManager for the 9 requests ?

It will be a loop, every 9 minutes, 9 differents rss file, and we loop, does it matter ? and what about connecting ? do I need other things then readyRead() ? (it is in silent mode, no printing advancement ect...)

anda_skoa
24th May 2015, 16:12
And for my project, Do I need only one QNetworkAccessManager for the 9 requests ?

I don't see why you would need more than one QNAM.



It will be a loop, every 9 minutes, 9 differents rss file, and we loop, does it matter ?

I doubt it.



and what about connecting ? do I need other things then readyRead() ?
You might want to know when the request is done, i.e. by connecting to the finished() signal.

Cheers,
_

AmirH
24th May 2015, 18:55
thanks for your help
another thing, should I code something special for the slot connected to finish? is there something we must do ?

wysota
24th May 2015, 20:40
ok, this is the meaning of readAll(), I did not understand at first, I though it copied the content and not tranfer it.

This would prevent the socket from receiving more data than fits into one third (original data, the copy, the variable you assign the result of readAll() to) of the machine's virtual memory.

anda_skoa
25th May 2015, 07:14
another thing, should I code something special for the slot connected to finish? is there something we must do ?

Well, at some point you should delete the QNetworkReply instance.
That can be, and often is, done in the slot connected to the finished signal, using deleteLater() on the reply object.

Cheers,
_

AmirH
25th May 2015, 15:25
ok.

We can close the subject.

Resolved!