Results 1 to 12 of 12

Thread: network request more than one

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default network request more than one

    How can I read many sites ?

    Now I'm using this:

    Qt Code:
    1. QByteArray DlgMyAvatar::get_avatar(QString strUrl)
    2. {
    3. QEventLoop eventLoop;
    4. QNetworkAccessManager *accessManager = new QNetworkAccessManager;
    5. QNetworkReply *pReply = accessManager->get(QNetworkRequest(QUrl(strUrl)));
    6. QObject::connect(pReply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    7. eventLoop.exec();
    8.  
    9. accessManager->deleteLater();
    10. pReply->deleteLater();
    11.  
    12. if (pReply->error())
    13. return QByteArray();
    14.  
    15. QByteArray bData = pReply->readAll();
    16.  
    17. if (bData.isEmpty() == false)
    18. return bData;
    19.  
    20. return QByteArray();
    21. }
    22.  
    23. ....
    24. for (int i = 0; i < lAvatars.count(); i++)
    25. {
    26. QByteArray bAvatar = get_avatar(lAvatars.at(i));
    27. ....
    To copy to clipboard, switch view to plain text mode 

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

    Of course I can convert it to something like this:

    Qt Code:
    1. void DlgMyAvatar::get_avatar(QString strUrl)
    2. {
    3. pReply = accessManager->get(QNetworkRequest(QUrl(strUrl)));
    4. QObject::connect(pReply, SIGNAL(finished()), this, SLOT(read_avatar()));
    5. }
    6.  
    7. void DlgMyAvatar::read_avatar()
    8. {
    9. accessManager->deleteLater();
    10. pReply->deleteLater();
    11.  
    12. if (pReply->error())
    13. return QByteArray();
    14.  
    15. QByteArray bData = pReply->readAll();
    16.  
    17. // show bData ...
    18. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    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: network request more than one

    Quote Originally Posted by mero View Post
    but it can only read one avatar/site ...
    Why is that?
    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.


  3. #3
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network request more than one

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

  4. #4
    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: network request more than one

    The original url is stored in the QNetworkReply object.
    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.


  5. #5
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network request more than one

    Quote Originally Posted by wysota View Post
    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:
    Qt Code:
    1. private:
    2. QNetworkAccessManager *accessManager;
    3. QNetworkReply *pReply;
    To copy to clipboard, switch view to plain text mode 

    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 ?

  6. #6
    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: network request more than one

    Quote Originally Posted by mero View Post
    how can i read this url ? what function ?
    See QNetworkReply::request() and QNetworkRequest::url().


    also if I have one QNetworkReply in .h:
    Qt Code:
    1. private:
    2. QNetworkAccessManager *accessManager;
    3. QNetworkReply *pReply;
    To copy to clipboard, switch view to plain text mode 

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


  7. #7
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network request more than one

    Quote Originally Posted by wysota View Post
    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
    Qt Code:
    1. private:
    2. QNetworkAccessManager *avatarAccessManager;
    3. QNetworkReply *pAvatarReply;
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. ... (constructor)...
    2. {
    3. avatarAccessManager = new QNetworkAccessManager;
    4. }
    5.  
    6. ...(destructor) ...
    7. {
    8. avatarAccessManager->deleteLater();
    9. }
    10.  
    11. void DlgMyAvatar::get_avatar(QString strUrl)
    12. {
    13. pAvatarReply = avatarAccessManager->get(QNetworkRequest(QUrl(strUrl)));
    14. QObject::connect(pAvatarReply, SIGNAL(finished()), this, SLOT(avatar_finished()));
    15. }
    16.  
    17. void DlgMyAvatar::avatar_finished()
    18. {
    19. // prevent crash (?)
    20. //pAvatarReply->deleteLater();
    21.  
    22. if (pAvatarReply->error())
    23. return;
    24.  
    25. QString strUrl = pAvatarReply->url().toString();
    26. QByteArray bData = pAvatarReply->readAll();
    27.  
    28. if ((strUrl.isEmpty() == false) && (bData.isEmpty() == false))
    29. {
    30. qDebug() << "got avatar data for url:" << strUrl;
    31. //return bData;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

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

  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: network request more than one

    Because you're using a variable that you are constantly overwriting. What exactly did you expect here?
    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
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network request more than one

    Quote Originally Posted by wysota View Post
    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)

  10. #10
    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: 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.
    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.


  11. The following user says thank you to wysota for this useful post:

    mero (11th March 2011)

  12. #11
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: network request more than one

    thank you it is working
    but in the finished signal do i need to deleteLayer of reply ?

  13. #12
    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: network request more than one

    It's up to you what to do with it.
    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.


Similar Threads

  1. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  2. Replies: 0
    Last Post: 25th April 2010, 15:50
  3. http request
    By yagabey in forum Qt Programming
    Replies: 3
    Last Post: 28th December 2008, 19:19
  4. QSessionManager example request
    By oguzy in forum Qt Programming
    Replies: 0
    Last Post: 30th September 2008, 21:53
  5. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 12:26

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.