Results 1 to 10 of 10

Thread: QNetworkReply::readAll() empty or not depending on calling position

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default QNetworkReply::readAll() empty or not depending on calling position

    Hi,

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

    code 1 :

    Qt Code:
    1. Window::Window()
    2. {
    3. AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
    4.  
    5.  
    6. for(int i=0;i<AppVars::urlRSSlist.size();i++){
    7. qDebug() << "enter for loop for i = "+QString::number(i);
    8. netManList<< new QNetworkAccessManager(this);
    9. listRequest<<QNetworkRequest(QUrl(AppVars::urlRSSlist[i]));
    10. listReply<<netManList[i]->get(listRequest[i]);
    11. }
    12. QObject::connect (listReply[0], SIGNAL(readyRead()), this, SLOT(downloadedRSSalaune())) ;
    13.  
    14. }
    15.  
    16. void Window::downloadedRSSalaune(){
    17.  
    18. qDebug() << listReply[0]->errorString();
    19.  
    20. if(listReply[0]->error() != QNetworkReply::NoError)
    21. return;
    22. //HERE
    23. QByteArray data =QByteArray(listReply[0]->readAll());
    24. qDebug() <<listReply[0]->readAll();
    25.  
    26. int statusCode = listReply[0]->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    27. qDebug() << QVariant(statusCode).toString();
    28. }
    To copy to clipboard, switch view to plain text mode 

    code 2 :

    Qt Code:
    1. Window::Window()
    2. {
    3. AppVars::urlRSSlist << "http://rss.lemonde.fr/c/205/f/3050/index.rss";
    4.  
    5.  
    6. for(int i=0;i<AppVars::urlRSSlist.size();i++){
    7. qDebug() << "enter for loop for i = "+QString::number(i);
    8. netManList<< new QNetworkAccessManager(this);
    9. listRequest<<QNetworkRequest(QUrl(AppVars::urlRSSlist[i]));
    10. listReply<<netManList[i]->get(listRequest[i]);
    11. }
    12. QObject::connect (listReply[0], SIGNAL(readyRead()), this, SLOT(downloadedRSSalaune())) ;
    13.  
    14. }
    15.  
    16. void Window::downloadedRSSalaune(){
    17.  
    18. qDebug() << listReply[0]->errorString();
    19.  
    20. if(listReply[0]->error() != QNetworkReply::NoError)
    21. return;
    22. //AND HERE
    23. qDebug() <<listReply[0]->readAll();
    24. QByteArray data =QByteArray(listReply[0]->readAll());
    25.  
    26.  
    27. int statusCode = listReply[0]->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    28. qDebug() << QVariant(statusCode).toString();
    29. }
    To copy to clipboard, switch view to plain text mode 

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

    result 1 :
    Qt Code:
    1. "enter for loop for i = 0"
    2. "Unknown error"
    3. ""
    4. "200"
    To copy to clipboard, switch view to plain text mode 

    result 2 :
    Qt Code:
    1. "enter for loop for i = 0"
    2. "Unknown error"
    3. "<?xml [................................. lot of lines...............]"
    4. "200"
    To copy to clipboard, switch view to plain text mode 

    What's going on here ?

    N.B. :
    Qt Code:
    1. QList<QNetworkAccessManager*> netManList;
    2. QList<QNetworkRequest> listRequest;
    3. QList<QNetworkReply*> listReply;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    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,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    Quote Originally Posted by AmirH View Post
    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,
    _

  5. #5
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    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...)

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    Quote Originally Posted by AmirH View Post
    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.

    Quote Originally Posted by AmirH View Post
    It will be a loop, every 9 minutes, 9 differents rss file, and we loop, does it matter ?
    I doubt it.

    Quote Originally Posted by AmirH View Post
    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,
    _

  7. #7
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    thanks for your help
    another thing, should I code something special for the slot connected to finish? is there something we must do ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    Quote Originally Posted by AmirH View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    Quote Originally Posted by AmirH View Post
    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,
    _

  10. #10
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QNetworkReply::readAll() empty or not depending on calling position

    ok.

    We can close the subject.

    Resolved!

Similar Threads

  1. QHttp readAll() return empty QbyteArray
    By chamoda in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2013, 06:59
  2. QFile readAll() problem..
    By zgulser in forum Newbie
    Replies: 4
    Last Post: 23rd June 2012, 00:11
  3. QTcpSocket, problems with readall()
    By Leo_san in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2011, 23:31
  4. How to obtain a QI,age from QTcpSocket->readAll
    By gorka_sm in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2011, 08:50
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.