2013-10-11 55 views
0

我想獲得在我的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文件main.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; 
    } 

部分

SkyObjDescription * skd = new SkyObjDescription(m_Name, "Constellation"); 
     QString data(skd->downloadedData()); 
     qDebug() << data; 
     delete skd; 

如果有其他的方式來獲得鏈接數據請解釋一下。 非常感謝:)

回答

3

你可能試驗的是你的雙重電話QNetworkReply::readAll

記住它是一個IO操作,並且沒有辦法多次讀取netwrk回覆中包含的信息。

剛剛開始您調試行:

void SkyObjDescription::fileDownloaded(QNetworkReply* pReply) 
{ 
    //qDebug() << pReply->readAll(); 
    m_DownloadedData = pReply->readAll(); 
    //emit a signal 
    pReply->deleteLater(); 
    emit downloaded(); 
} 

Edit(對於completness)

測試代碼我使用:

#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(); 
} 
+0

仍然沒有工作 – Vijay13

+0

@ Vijay13:顯示我的編輯舉個例子,並在你這邊測試一小段代碼。你的問題應該可能依賴於你的代碼中的其他地方 – jbh

+0

我已經按照你的建議更新了代碼,但仍然不能正常工作,我得到了空的QByteArray作爲回覆。我認爲問題出在其他地方,你能否建議其他方式從html頁面獲取數據? – Vijay13