PDA

View Full Version : HTTP postmethod in thread



mania
29th June 2015, 12:42
Hi,

Using thread concept i have to use HTTP post method , when run() function call i wrote below functionality(sample scratch code)

void classname::run()
{
while(1)
{
QEventLoop eventLoop;
QNetworkAccessManager mgr;

QString strReply;
QString json_string="HAI";
QString url="http://122.165.224.226:8180/json_response/jsonstatus";
QByteArray jsonString_byte=json_string.toUtf8 ();
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkRequest req( QUrl( QString(url+"") ) );

// startRequestTimer(eventLoop);
qDebug()<<"\r server convert json"<<json_string<<jsonString_byte;
QNetworkReply *reply;
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
reply = mgr.post(req,jsonString_byte);
// reply=mgr.get(req);
qDebug()<<"\r server reply post request"<<reply->readAll()<<"error code..>>"<<reply->error();
eventLoop.exec();

// stopRequestTimer(eventLoop);

sleep(2);
if (reply->error() == QNetworkReply::NoError)//reply->erro()
{
qDebug()<<"\r \n Server Response no error";
strReply = reply->readAll();
qDebug()<<"\r \n Server Response no error 1"<<strReply;
}
else
{
qDebug()<<"\r \nserver Server Response ERROR :: "<< reply->errorString()<<reply->error();
strReply="";
}
qDebug()<<"\r time"<<alarmtime;
//exec();
}
}

}

when i execute or run the application i am getting response as "Unknown error" error code as 99.
Any one hlep me its very important project.


Thanks in advance.

anda_skoa
29th June 2015, 12:54
And it works if you are running this in the main thread?

Cheers,
_

mania
29th June 2015, 13:57
Hi,
Thanks for your reply.
I am using two classes, one is widget class and another one is alarm(thread class). In widget class thread class is initialize and start method is called as like previously post.

Thanks in advance.

anda_skoa
29th June 2015, 14:37
Let me rephrase that:

if you do not use a thread but instead run the code post() code in the main thread, do you get also get the "unknown error"?

Cheers,
_

ChrisW67
29th June 2015, 22:58
There are many things not right here.

Assuming the Error 99 is coming from the server you should look at the request.
Your json_string is not, indeed, a JSON string.
You have not set a request content type.

mania
1st July 2015, 05:41
Hi,

We have set the content type as json, but even though we are getting the error.