Results 1 to 5 of 5

Thread: problem in using QHttp

  1. #1
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default problem in using QHttp

    I've written a class that will handle file downloads. I think my logic is ok but the program isn't working as expected. The program downloads the first file successfully but hangs with the second download. I've gone through my written code over and over again but can't find the logical error.

    I've maintained one QHttp and one QFile object in the class and scheduled one GET request at a time. The idea has been taken from the book - C++ GUi programming with Qt.

    Please help me in finding out the logical error.

    myhttp.h

    Qt Code:
    1. #ifndef MYHTTP_H
    2. #define MYHTTP_H
    3.  
    4. #include <QObject>
    5.  
    6. class QHttp;
    7. class QFile;
    8.  
    9. class MyHttp : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyHttp(QObject* parent = 0);
    15. void get(const QString& strFullFilePath);
    16.  
    17. signals:
    18. void done();
    19.  
    20. private slots:
    21. void myHttpRequestDone(bool error);
    22.  
    23. private:
    24. void processNextRequest();
    25.  
    26. QHttp* http;
    27. QFile* file;
    28. QStringList* pendingRequestList;
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 


    myhttp.cpp :

    Qt Code:
    1. #include <QtNetwork>
    2.  
    3. #include "myhttp.h"
    4.  
    5. MyHttp::MyHttp(QObject* parent /* = 0 */) : QObject(parent)
    6. {
    7. http = new QHttp;
    8. file = 0;
    9. pendingRequestList = new QStringList;
    10.  
    11. connect(http, SIGNAL(done(bool)), this, SLOT(myHttpRequestDone(bool)));
    12. }
    13.  
    14. void MyHttp::get(const QString& strFullFilePath)
    15. {
    16. // Add this request to list
    17. pendingRequestList->append(strFullFilePath);
    18.  
    19. if (pendingRequestList->size() == 1)
    20. {
    21. processNextRequest();
    22. }
    23. }
    24.  
    25. void MyHttp::processNextRequest()
    26. {
    27. if (pendingRequestList->isEmpty() == true)
    28. {
    29. emit done();
    30. }
    31. else
    32. {
    33. // Get the first request in the list
    34. QString strFullFilePath = pendingRequestList->first();
    35.  
    36. // Extract the file name
    37. QUrl url(strFullFilePath);
    38. QFileInfo fileInfo(url.path());
    39. QString fileName = fileInfo.fileName();
    40.  
    41. // Create an instance of a file with the extracted name and open it
    42. file = new QFile(fileName);
    43. file->open(QIODevice::WriteOnly);
    44.  
    45. // Send the request
    46. http->setHost(url.host(), url.port(80));
    47. http->get(url.path(), file);
    48. }
    49. }
    50.  
    51. void MyHttp::myHttpRequestDone(bool error)
    52. {
    53. file->close();
    54.  
    55. if (error == true)
    56. {
    57. file->remove();
    58. }
    59.  
    60. delete file;
    61. file = 0;
    62.  
    63. pendingRequestList->removeFirst();
    64. processNextRequest();
    65. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp :

    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include "myhttp.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. MyHttp* myhttp = new MyHttp();
    10. QObject::connect(myhttp, SIGNAL(done()), &app, SLOT(quit()));
    11.  
    12. myhttp->get("http://thedailystar.net/images/tdsmainlogo.jpg");
    13. myhttp->get("http://thedailystar.net/photo/2008/09/06/2008-09-06__front02.jpg");
    14. myhttp->get("http://ahmadferdous.googlepages.com/Ferdous.JPG");
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by Ferdous; 9th September 2008 at 10:52. Reason: further elaboration

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

    Default Re: problem in using QHttp

    QHttp::get() is an asynchronous operation, thus all of your myHttp::get calls are executed before the first download even starts. Thus the condition "pendingRequestList->size() == 1" is only satisfied right after you add the first request to the queue. You should queue requests and once a request is finished, check the queue and start another request.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem in using QHttp

    Thanks for your reply.

    Quote Originally Posted by wysota View Post
    You should queue requests and once a request is finished, check the queue and start another request.
    That's what I've done here. I've queued all the download requests in pendingRequestList and scheduled only one download at a time. When the first download is finished and the request queue is empty, done(bool) signal is emitted and slot myHttpRequestDone(bool) is called where processNextRequest() is called and it's processNextRequest() where QHttp:get() is called for the next download. So it's not a problem that the condition pendingRequest.size() == 1 is executed only for the first download request.

    Here I've emulated QHttp in blocking approach.

    Please enlighten me further.

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

    Default Re: problem in using QHttp

    Quote Originally Posted by Ferdous View Post
    That's what I've done here.
    Not really.

    I've queued all the download requests in pendingRequestList and scheduled only one download at a time.
    And that's exactly what happens - you get one download.

    When the first download is finished and the request queue is empty, done(bool) signal is emitted and slot myHttpRequestDone(bool) is called where processNextRequest() is called and it's processNextRequest() where QHttp:get() is called for the next download. So it's not a problem that the condition pendingRequest.size() == 1 is executed only for the first download request.
    Yeah, but after the first download you have two elements in the queue, so the condition size()==1 is not satisfied. Don't you think it should be "!pendingRequest.isEmpty()"?

  5. #5
    Join Date
    Sep 2008
    Location
    Bangladesh
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem in using QHttp

    Quote Originally Posted by wysota View Post
    And that's exactly what happens - you get one download.
    When one download completes, processNextRequest() is called. So shouldn't the download for next request in pendingRequestList continue successfully?

    Yeah, but after the first download you have two elements in the queue, so the condition size()==1 is not satisfied. Don't you think it should be "!pendingRequest.isEmpty()"?
    pendingRequestList.size() == 1 condition is meant for only the first download request so that only the first download request gets chance to call processNextRequest as soon as it arrives, for other requests, they are put in the pendingRequestList. When the first download completes, QHttp::done(bool) signal is emitted and slot myHttpRequestDone(bool) gets called. At the end of myHttpRequestDone(bool) function, processNextRequest() is called and now the first request in the current list gets chance to start download by get().

    This is my logic to solve the problem - downloading multiple files. But I've failed so far.

    Hope I've been able to make you understand my viewpoint.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  2. QHttp download file problem.
    By fengtian.we in forum Qt Programming
    Replies: 12
    Last Post: 12th June 2007, 09:39
  3. Problem : use QHttp get a file to QFile
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 24th May 2007, 10:58
  4. QHttp Problem
    By musaulker in forum Newbie
    Replies: 4
    Last Post: 29th March 2007, 00:40
  5. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02

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.