2

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;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Masih
  • 23
  • 1
  • 6
  • This code seems also working. You need to provide the reply error instead of what you print "Error", show the output from: `qDebug() << reply->error();` – Mohammad Kanan Jun 30 '18 at 13:54

1 Answers1

1

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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @Mohammad I think it's weird, I've tested it. Have you copied my code completely or have you just modified it in parts? – eyllanesc Jun 29 '18 at 07:52
  • @Mohammad Try downloading the project from the following link: https://github.com/eyllanesc/stackoverflow/tree/master/51092115 – eyllanesc Jun 29 '18 at 07:54
  • I downloaded your project in github, but still show me ERROR. – Masih Jun 29 '18 at 08:13
  • Place this url in your browser: `https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180627T161429Z.7e64c91dd2016a6c.9901da9a44bc324388a2460776ab55b2d72b4c5a&lang=ar&text=Hello%20World`, What do you get? What version of Qt5 do you have? Do you use a proxy? It works correctly for me in Linux with Qt 5.11 – eyllanesc Jun 29 '18 at 08:15
  • I get a Json from that link, No i don't use proxy. – Masih Jun 29 '18 at 08:23
  • Use wireshark to check what's happening, what's your OS? Do not you get any warning about openssl? – eyllanesc Jun 29 '18 at 08:24
  • ok i use wireshark to check what's happening, windows 10, No i don't any warning about openssl. – Masih Jun 29 '18 at 08:30
  • One last recommendation: try it on another machine and with another access to the internet. – eyllanesc Jun 29 '18 at 08:31