diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/main.cpp | 73 | 
1 files changed, 73 insertions, 0 deletions
| diff --git a/src/main/main.cpp b/src/main/main.cpp new file mode 100644 index 0000000..cddb26b --- /dev/null +++ b/src/main/main.cpp @@ -0,0 +1,73 @@ +#include <QGuiApplication> +#include <QQmlEngine> +#include <QQmlContext> +#include <QQmlComponent> + +// networking +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QUrl> +#include <QUrlQuery> + +// json parsing +#include <QJsonValue> +#include <QJsonDocument> +#include <QJsonObject> +#include <QVariantMap> +#include <QJsonArray> + +// console output +#include <QDebug> +// #include <iostream> + +int main(int argc, char *argv[]) +{ +    QGuiApplication app(argc, argv); + +    QQmlEngine engine; +    QQmlContext *objectContext = new QQmlContext(engine.rootContext()); + +    QQmlComponent component(&engine, "../../res/gui/application.qml"); +    QObject *object = component.create(objectContext); + +    // ... delete object and objectContext when necessary + + +   // 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())); + +    QString endpoint { "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC" }; + +    // the HTTP request +    QNetworkRequest req( (QUrl( endpoint )) ); +    QNetworkReply *reply = mgr.get(req); +    eventLoop.exec(); // blocks stack until "finished()" has been called + +    if (reply->error() == QNetworkReply::NoError) { +        //success + +        QString strReply = (QString)reply->readAll(); + +        //parse json +        // qDebug() << "Response:" << strReply; +        QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + +        QJsonObject jsonObj = jsonResponse.object(); + +        qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); + +        delete reply; +    } +    else { +        //failure +        qDebug() << "Failure" <<reply->errorString(); +        delete reply; +    } + +    return app.exec(); +} | 
