我想获得出现在我的Qt程序中的以下链接上的字符串,我该如何获得它?
http://en.wikipedia.org/w/api.php?action=opensearch&search=centaurus_constellation
我已编码以下文件以获取字符串出现在m_DownloadedData的上一页,但是它在程序的末尾存储空字符串,有人可以帮我从上面的链接获取数据吗?
.h文件:
#ifndef SKYOBJDESCRIPTION_H
#define SKYOBJDESCRIPTION_H
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class SkyObjDescription : public QObject
{
Q_OBJECT
public:
explicit SkyObjDescription(const QString soName, const QString soType, QObject* parent = 0);
virtual ~SkyObjDescription();
QByteArray downloadedData() const;
signals:
void downloaded();
private slots:
void fileDownloaded(QNetworkReply* reply);
private:
QString soName,soType;
QByteArray m_DownloadedData;
};
#endif // SKYOBJDESCRIPTION_H
和 .cpp文件
#include <QString>
#include <QUrl>
#include <QDebug>
#include "skyobjdescription.h"
SkyObjDescription::SkyObjDescription(const QString so_Name, const QString so_Type, QObject* parent): soName(so_Name), soType(so_Type), QObject(parent)
{
QString wikiLink = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+ soName.replace(" ", "_").toLower() + "_" + soType.toLower() + "&format=xml&limit=1.xml";
// QUrl wikiUrl("http://en.wikipedia.org/w/api.php?action=opensearch&search=hello_world&format=xml&limit=1.xml");
m_DownloadedData = wikiUrl.toEncoded();
qDebug() << "wikiUrl.toEncoded(): " << m_DownloadedData;
QNetworkRequest req(wikiUrl);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(fileDownloaded(QNetworkReply*)));
manager->get(req);
}
SkyObjDescription::~SkyObjDescription()
{
}
void SkyObjDescription::fileDownloaded(QNetworkReply* reply)
{
m_DownloadedData = reply->readAll();
qDebug() << "received reply";
qDebug() << m_DownloadedData;
reply->deleteLater();
emit downloaded();
}
QByteArray SkyObjDescription::downloadedData() const
{
qDebug() << m_DownloadedData;
return m_DownloadedData;
}
main.cpp的一部分
SkyObjDescription * skd = new SkyObjDescription(m_Name, "Constellation");
QString data(skd->downloadedData());
qDebug() << data;
delete skd;
如果还有其他方法可以从链接获取数据,请解释一下。 非常感谢你:))
答案 0 :(得分:3)
您可能尝试的内容是由QNetworkReply::readAll
上的双重调用引起的。
记得这是一个IO操作,并且无法多次读取netwrk回复中包含的信息。
只需注释您的调试行:
void SkyObjDescription::fileDownloaded(QNetworkReply* pReply)
{
//qDebug() << pReply->readAll();
m_DownloadedData = pReply->readAll();
//emit a signal
pReply->deleteLater();
emit downloaded();
}
编辑(用于完整性)
我使用的测试代码:
#ifndef TMP_H
#define TMP_H
#include <QObject>
class QNetworkReply;
class Tmp : public QObject
{
Q_OBJECT
public:
explicit Tmp(QObject *parent = 0);
signals:
public slots:
void displayResult(QNetworkReply* reply);
};
#endif // TMP_H
//tmp.cpp
#include "tmp.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QDebug>
Tmp::Tmp(QObject *parent) :
QObject(parent)
{
QUrl url("http://en.wikipedia.org/wiki/Centaurus_constellation");
QNetworkRequest req(url);
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(displayResult(QNetworkReply*)));
manager->get(req);
}
void Tmp::displayResult(QNetworkReply *reply) {
QByteArray buffer = reply->readAll();
qDebug() << "received reply";
qDebug() << buffer;
reply->deleteLater();
}