void MainWindow::on_pushButton_clicked()
{
QNetworkReply* reply;
Request_reply( reply);
}
int MainWindow:: Request_reply(QNetworkReply* reply) {
// create custom temporary event loop on stack
// "quit()" the event-loop, when the network request "finished()"
QNetworkAccessManager mgr;
QObject::connect(&mgr,
SIGNAL(finished
(QNetworkReply
*)),
&eventLoop,
SLOT(quit
()));
// the HTTP request
//QNetworkRequest req( QUrl( QString("http://time.jsontest.com/") ) );
QNetworkRequest req
( QUrl( QString("https://10.201.58.87:2342/kirk/1.0/") ) );
reply = mgr.get(req);
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
//parse json
qDebug() << "INFO:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug() << "name:" << jsonObj["name"].toString();
qDebug() << "build_version:" << jsonObj["build_version"].toString();
QString str
= jsonObj
["name"].
toString();
ui->label1->setText(str);
//delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->errorString();
delete reply;
}
return 0;
}
void MainWindow::on_pushButton_clicked()
{
QNetworkReply* reply;
Request_reply( reply);
}
int MainWindow:: Request_reply(QNetworkReply* reply) {
// create custom temporary event loop on stack
QEventLoop eventLoop;
// "quit()" the event-loop, when the network request "finished()"
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
// the HTTP request
//QNetworkRequest req( QUrl( QString("http://time.jsontest.com/") ) );
QNetworkRequest req( QUrl( QString("https://10.201.58.87:2342/kirk/1.0/") ) );
reply = mgr.get(req);
eventLoop.exec(); // blocks stack until "finished()" has been called
if (reply->error() == QNetworkReply::NoError) {
QString strReply = (QString)reply->readAll();
//parse json
qDebug() << "INFO:" << strReply;
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug() << "name:" << jsonObj["name"].toString();
qDebug() << "build_version:" << jsonObj["build_version"].toString();
QString str = jsonObj["name"].toString();
ui->label1->setText(str);
//delete reply;
}
else {
//failure
qDebug() << "Failure" <<reply->errorString();
delete reply;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks