我需要从中获取一些数据QNetworkReply
以便稍后解析,但每次返回data
都是0
在使用qDebug() << getData()
.
我不想将它保存在文件中,那么如何从QNetworkReply
插槽中获取数据并将其存储在字符串中?
请帮忙!非常感谢。
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class Fetch : public QObject
{
Q_OBJECT
public:
Fetch(){};
void go(const QString &str, QUrl &url)
{
QNetworkRequest request;
request.setUrl(url);
QByteArray data = str.toUtf8();
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
reply = manager->post(request, data);
connect(reply, SIGNAL(finished()), SLOT(httpFinished()));
}
QString getData()
{
return QString::fromUtf8(m_data);
}
private slots:
void httpFinished()
{
m_data = reply->readAll();
}
private:
QByteArray m_data;
QNetworkReply* reply;
};