【问题标题】:Qt: How to get the response from a GET?Qt:如何从 GET 中获取响应?
【发布时间】:2011-03-07 14:07:32
【问题描述】:

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

为什么我有麻烦?

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

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

代码如下:

主窗口.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;
}

关于我如何做到这一点的任何想法?

提前致谢!

【问题讨论】:

  • 嗯,这很旧,很旧,但我看到了一些我认为值得一提的东西:我们不应该删除完成槽中的回复,正如文档所说:“注意:在请求已经完成,用户有责任在适当的时候删除 QNetworkReply 对象。不要在连接到finished()的槽内直接删除它。你可以使用deleteLater()函数。"

标签: qt get request response signals-slots


【解决方案1】:

每个QNetworkReply 都会发出finished() 信号,因此您应该将request->sendRequest("www.request.com"); 返回的来自QNetworkReply* 的信号连接到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
}

【讨论】:

  • 你确定是 QNetworkReply 发出 finsished() 吗?看起来是 NetworkAccssManager 执行此操作。
  • 我故意提出了错误的请求。我正在尝试获取错误回复的响应正文。我已经成功提取了未经授权访问的错误代码(401)。但是当我尝试使用reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString() 获取回复的正文时,给了我空字符串。但是如果我使用 QByteArray bts = reply->readAll();,它会在将其转换为字符串时给我错误消息,但是为什么第一种方法不起作用?
猜你喜欢
  • 1970-01-01
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多