Results 1 to 7 of 7

Thread: QnetworkReply Problems

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default QnetworkReply Problems

    Hi all,

    I written a code to hit the the web service and print the response which is send by webservice.Its working fine but its behaving in different manner.

    SampleClass.h
    public slots:
    void slotRequestFinished(QNetworkReply* reply);
    public:
    void postingTheDataToService();
    QNetworkAccessManager *networkManager;
    -------------------------------------------------------------------------------------------------------
    SampleClass.cpp

    SampleClass::SampleClass(QWidget *parent) : QWidget(parent), ui(new Ui::SampleClass)
    {
    -----somestatements-----
    -----somestatements-----
    -----somestatements-----
    postingTheDataToService();
    qdebug("test--------------------1");
    qdebug("test--------------------2");
    qdebug("test--------------------3");
    }
    void SampleClass:: postingTheDataToService()
    {

    qDebug("in posting------------11111");
    networkManager=new QNetworkAccessManager(this);
    qDebug("in posting------------22222222222");
    QUrl serviceurl=QUrl("http://a.sampleserviceurl.appspot.com/qtservice");
    qDebug("in posting------------333333333");
    networkManager->post(QNetworkRequest(serviceurl),postData);
    qDebug("in posting------------44444444");
    connect(networkManager,SIGNAL(finished(QNetworkRep ly*)),this,SLOT(slotRequestFinished(QNetworkReply* )));
    qDebug("in posting------------55555555555555");

    }

    void SampleClass::slotRequestFinished(QNetworkReply* reply)
    {
    qDebug()<<"Service TEst ------------T------------1111111----\n";
    QByteArray data = reply->readAll();
    qDebug()<<"Service TEst ------------T------------22222----\n";
    qdebug()<<"required response is---------"<<data;
    }
    --------------------------------------------------------------------------------------------------------------------
    Output which i am getting is
    in posting------------11111
    in posting------------22222222222
    in posting------------333333333
    in posting------------44444444
    in posting------------55555555555555
    test--------------------1
    test--------------------2
    test--------------------3
    Service TEst ------------T------------1111111----
    Service TEst ------------T------------22222----
    required response is--------"something send by service"
    Here MyQuestion is now before printing the print statements in "postingTheDataToService" it should print and i need to print "slotRequestFinished" printing statements but its printing the "constructor" print statements..? ,pls correct me where i am wrong.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QnetworkReply Problems

    Qt networking is asynchronous, postingTheDataToService() is non-blocking, and execution runs straight through the constructor. As documented, QNetworkAccessManager queues requests and sends them when the program returns to the event loop. The program does not return to the event loop until some time after the constructor has finished. Before that time the request has not even been sent, let alone answered.

  3. #3
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Re: QnetworkReply Problems

    hi...thanks for ur response........den how could i solve my problem because before returning to eventloop i has to post some data to server and use the response data which is sended by server. please provide me a solution to my problem or let me know the other way to solve it....

    Regards
    beggin

  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 Problems

    Do you really want to block a widget's constructor (and thus the whole UI) until a server request/response has been full processed?

    Cheers,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Re: QnetworkReply Problems

    Hii...thanks for reply.....

    The question u asked wont be possible i think ??????Actually after hitting the service want to get the response from service ,may it take time tooo also to get response .
    Is their any other way so that my output could be like dis ...
    in posting------------11111
    in posting------------22222222222
    in posting------------333333333
    in posting------------44444444
    in posting------------55555555555555
    Service TEst ------------T------------1111111----
    Service TEst ------------T------------22222----
    required response is--------"something send by service"
    test--------------------1
    test--------------------2
    test--------------------3

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QnetworkReply Problems

    Qt Code:
    1. SampleClass::SampleClass(QWidget *parent) : QWidget(parent), ui(new Ui::SampleClass)
    2. {
    3. -----somestatements-----
    4. -----somestatements-----
    5. -----somestatements-----
    6. postingTheDataToService();
    7. }
    8.  
    9. void SampleClass:: postingTheDataToService()
    10. {
    11. // unchanged
    12. }
    13.  
    14. void SampleClass::slotRequestFinished(QNetworkReply* reply)
    15. {
    16. if (reply->error() == QNetworkReply::NoError) { // get into the habit of checking errors
    17. qDebug()<<"Service TEst ------------T------------1111111----\n";
    18. QByteArray data = reply->readAll();
    19. qDebug()<<"Service TEst ------------T------------22222----\n";
    20. qdebug()<<"required response is---------"<<data;
    21. qdebug("test--------------------1");
    22. qdebug("test--------------------2");
    23. qdebug("test--------------------3");
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #7
    Join Date
    Jul 2012
    Posts
    40
    Qt products
    Qt4 Qt/Embedded

    Default Re: QnetworkReply Problems

    Hey ChrisW67 thankzzzzzzzzzz a lot .....it solved my problem.........

Similar Threads

  1. QNetworkReply::ProtocolUnknownError
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 12th April 2012, 09:45
  2. Replies: 2
    Last Post: 18th January 2012, 15:01
  3. QNetworkReply: how to know when data is sent?
    By mentalmushroom in forum Qt Programming
    Replies: 17
    Last Post: 24th June 2011, 08:22
  4. Subclassing QNetworkReply
    By piotr.dobrogost in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2010, 08:42
  5. QNetworkReply::HostNotFoundError
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2009, 10:58

Tags for this Thread

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.