Vijay13
Vijay13

Reputation: 605

How can I download data from any link?

I want to get string that appears on following link in my Qt program how can I get it ?

http://en.wikipedia.org/w/api.php?action=opensearch&search=centaurus_constellation

I have coded following files to get string appearing on above page in m_DownloadedData, but it is storing empty string at the end of the program , can someone please help me get data from above link ?

.h file:

#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

and .cpp file

    #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;
    }

part of main.cpp

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

If there is other way to get data from link please explain it. Thank you very much :)

Upvotes: 0

Views: 208

Answers (1)

epsilon
epsilon

Reputation: 2969

What you probably experiment is caused by your double call on QNetworkReply::readAll.

Remembers it is a IO operation, and there is no way to read multiple times the information contain by the netwrk reply.

Just comment your debug line :

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

edit (for completness)

The test code I use :

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

Upvotes: 3

Related Questions