2

我在qt上写了这段代码,但是当我运行这个项目时,我的输出是“错误”。

如何解决我的问题?

例如,在这段代码中,我在 url 中添加了一个地址,我想从这个 url 中读取 Json,并显示一些信息。

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;
}
4

1 回答 1

1

要将键值添加到 url 你必须使用QUrlQuery如下所示:

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();
}

输出:

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:  "مرحبا العالم"

如果生成的 url 被修改,则与串联不同:

级联:

...&text=Hello World

编码:

...&text=Hello%20World
于 2018-06-28T23:55:20.597 回答