PDA

View Full Version : QnetworkReply Problems



sai_3289
24th January 2013, 07:45
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.

ChrisW67
24th January 2013, 08:23
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.

sai_3289
24th January 2013, 10:26
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

anda_skoa
24th January 2013, 13:51
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,
_

sai_3289
24th January 2013, 14:34
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

ChrisW67
24th January 2013, 22:20
SampleClass::SampleClass(QWidget *parent) : QWidget(parent), ui(new Ui::SampleClass)
{
-----somestatements-----
-----somestatements-----
-----somestatements-----
postingTheDataToService();
}

void SampleClass:: postingTheDataToService()
{
// unchanged
}

void SampleClass::slotRequestFinished(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError) { // get into the habit of checking errors
qDebug()<<"Service TEst ------------T------------1111111----\n";
QByteArray data = reply->readAll();
qDebug()<<"Service TEst ------------T------------22222----\n";
qdebug()<<"required response is---------"<<data;
qdebug("test--------------------1");
qdebug("test--------------------2");
qdebug("test--------------------3");
}
}

sai_3289
25th January 2013, 07:46
Hey ChrisW67 thankzzzzzzzzzz a lot .....it solved my problem.........