Search code examples
c++qtqt5qnetworkaccessmanagerqurl

Read Json from https in qt


I wrote this code on qt, but when i run this project, My output is "Error".

How can solve my problem?

For example in this code I add a address in url, and I want read Json from this url, and show some info.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QNetworkAccessManager* nam = new QNetworkAccessManager(this);
    QString test = "ar";
    QString test2 = "Hello World";
        QObject::connect(nam, SIGNAL(finished(QNetworkReply*)),
                 this, SLOT(onResult(QNetworkReply*)));

        QUrl url("https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a&lang=" + test + "&text=" + test2);
        QNetworkReply* reply = nam->get(QNetworkRequest(url));
}

void MainWindow::onResult(QNetworkReply *reply)
{
    if(reply->error() == QNetworkReply::NoError) {

            QStringList propertyNames;
            QStringList propertyKeys;

            QString strReply = (QString)reply->readAll();

            qDebug() << strReply;

            QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());

            QJsonObject jsonObject = jsonResponse.object();

            QJsonArray jsonArray = jsonObject["status"].toArray();

            qDebug() << jsonObject["status"].toString();

            foreach (const QJsonValue & value, jsonArray)
            {
                QJsonObject obj = value.toObject();
                qDebug() << value.toString();
            }

        } else {
            qDebug() << "ERROR";
        }

        delete reply;
}

Solution

  • To add key-values ​​to the url you must use QUrlQuery as shown below:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        nam = new QNetworkAccessManager(this);
        connect(nam, &QNetworkAccessManager::finished, this, &MainWindow::onResult);
    
        QString lang = "ar";
        QString text = "Hello World";
        QString key =  "trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a";
    
        QUrlQuery query;
        query.addQueryItem("key", key);
        query.addQueryItem("lang", lang);
        query.addQueryItem("text", text);
    
        QUrl url("https://translate.yandex.net/api/v1.5/tr.json/translate");
        url.setQuery(query);
    
        qDebug()<< "url: "<< url.toString(QUrl::FullyEncoded);
    
        nam->get(QNetworkRequest(url));
    
    }
    
    void MainWindow::onResult(QNetworkReply *reply){
        if(reply->error() == QNetworkReply::NoError){
    
            QByteArray result = reply->readAll();
            QJsonDocument jsonResponse = QJsonDocument::fromJson(result);
            QJsonObject obj = jsonResponse.object();
            qDebug()<<"code: " << obj["code"].toInt();
            qDebug()<<"lang: " << obj["lang"].toString();
            QJsonArray array = obj["text"].toArray();
    
            for(const QJsonValue & value : array) {
               qDebug()<< "text: " <<value.toString();
            }
        }
        else
            qDebug() << "ERROR";
        reply->deleteLater();
    }
    

    Output:

    url:  "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a&lang=ar&text=Hello%20World"
    code:  200
    lang:  "en-ar"
    text:  "مرحبا العالم"
    

    If the url generated is revised, it differs from the concatenation:

    Concatenation:

    ...&text=Hello World
    

    Encoded:

    ...&text=Hello%20World