我在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;
}