问:如何从GET获得响应?

时间:2011-03-07 14:07:33

标签: qt get request response signals-slots

我无法收集来自网络请求的回复。 (因为我是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;
}

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

提前致谢!

1 个答案:

答案 0 :(得分:6)

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