Qt:如何获取 GET 的响应?

发布于 2024-10-20 19:36:40 字数 2792 浏览 5 评论 0原文

我无法从我所做的网络请求中收集响应。 (因为我是 Qt 新手)。

为什么我有麻烦?

我有一个请求类,它发送请求并接收响应。但我无法获得对请求对象的父对象的响应,因为我必须等待来自处理响应的 NetworkAccessMaanager 的“完成”信号。

因此,我在“完成”槽中处理响应,但无法将信息返回到保存请求对象的父主窗口。我该怎么做?

这是代码:

Main window.cpp:

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

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_buttonLogin_clicked()
{
    request->sendRequest("www.request.com");
}

Request.cpp:

Request::Request()
{
  oNetworkAccessManager = new QNetworkAccessManager(this);
  QObject::connect(oNetworkAccessManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
}

/*
 * Sends a request
 */
QNetworkReply* Request::sendRequest(QString url)
{
    QUrl httpRequest(url);
    QNetworkRequest request;
    request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // Set default ssl config
    request.setUrl(httpRequest); // Set the url
    QNetworkReply *reply = oNetworkAccessManager->get(QNetworkRequest(httpRequest));

    return reply;
}

/*
 * Runs when the request is finished and has received a response
 */
void Request::finishedSlot(QNetworkReply *reply)
{
       // Reading attributes of the reply
       // e.g. the HTTP status code
       QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
       // Or the target URL if it was a redirect:
       QVariant redirectionTargetUrl =
       reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
       // see CS001432 on how to handle this

       // no error received?
       if (reply->error() == QNetworkReply::NoError)
       {
           // Reading the data from the response
           QByteArray bytes = reply->readAll();
           QString jsonString(bytes); // string

           bool ok;
           QVariantMap jsonResult = Json::parse(jsonString,ok).toMap();
           if(!ok)
           {
               qFatal("An error occured during parsing");
               exit(1);
           }


           // Set the jsonResult
           setJsonResult(jsonResult);


       }
       // Some http error received
       else
       {
           // handle errors here
       }

       // We receive ownership of the reply object
       // and therefore need to handle deletion.
       delete reply;
}

/*
 * Set the json result so that other functions can get it
 */
void Request::setJsonResult(QVariantMap jsonResult)
{
    m_jsonResult = jsonResult;
}

/*
 * Get the json result
 * Return null if there is no result
 */
QVariantMap Request::getJsonResult()
{
    return m_jsonResult;
}

我有什么想法可以做到这一点吗?

提前致谢!

I'm having trouble collecting the response from a web request i do. (Because i'm new to Qt).

Why do i have trouble?

I have a request class which send a request and receive a response. But i can't get the response to the parent of the request object, because i have to wait for a "finished" signal from the NetworkAccessMaanager which handles the response.

So i handle the response in a "finished" slot, but i can't return the info to the parent main window holding the request object. How can i do this?

Here's the code:

Main window.cpp:

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

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_buttonLogin_clicked()
{
    request->sendRequest("www.request.com");
}

Request.cpp:

Request::Request()
{
  oNetworkAccessManager = new QNetworkAccessManager(this);
  QObject::connect(oNetworkAccessManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
}

/*
 * Sends a request
 */
QNetworkReply* Request::sendRequest(QString url)
{
    QUrl httpRequest(url);
    QNetworkRequest request;
    request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // Set default ssl config
    request.setUrl(httpRequest); // Set the url
    QNetworkReply *reply = oNetworkAccessManager->get(QNetworkRequest(httpRequest));

    return reply;
}

/*
 * Runs when the request is finished and has received a response
 */
void Request::finishedSlot(QNetworkReply *reply)
{
       // Reading attributes of the reply
       // e.g. the HTTP status code
       QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
       // Or the target URL if it was a redirect:
       QVariant redirectionTargetUrl =
       reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
       // see CS001432 on how to handle this

       // no error received?
       if (reply->error() == QNetworkReply::NoError)
       {
           // Reading the data from the response
           QByteArray bytes = reply->readAll();
           QString jsonString(bytes); // string

           bool ok;
           QVariantMap jsonResult = Json::parse(jsonString,ok).toMap();
           if(!ok)
           {
               qFatal("An error occured during parsing");
               exit(1);
           }


           // Set the jsonResult
           setJsonResult(jsonResult);


       }
       // Some http error received
       else
       {
           // handle errors here
       }

       // We receive ownership of the reply object
       // and therefore need to handle deletion.
       delete reply;
}

/*
 * Set the json result so that other functions can get it
 */
void Request::setJsonResult(QVariantMap jsonResult)
{
    m_jsonResult = jsonResult;
}

/*
 * Get the json result
 * Return null if there is no result
 */
QVariantMap Request::getJsonResult()
{
    return m_jsonResult;
}

Any ideas of how i can do this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-10-27 19:36:40

每个QNetworkReply都会发出finished()信号,因此您应该连接来自request->sendRequest("www"返回的QNetworkReply*的信号.request.com");MainWindow 的插槽。

例子:

void MainWindow::on_buttonLogin_clicked()
{
    QNetworkReply *reply = request->sendRequest("www.request.com");
    connect(reply, SIGNAL(finished()), this, SLOT(newslot()));
}

void MainWindow::newslot()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    // there you can handle reply as you wish
}

Each QNetworkReply emits finished() signal, so you should connect signal from QNetworkReply* returned by request->sendRequest("www.request.com"); to slot of MainWindow.

EXAMPLE:

void MainWindow::on_buttonLogin_clicked()
{
    QNetworkReply *reply = request->sendRequest("www.request.com");
    connect(reply, SIGNAL(finished()), this, SLOT(newslot()));
}

void MainWindow::newslot()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    // there you can handle reply as you wish
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文