如何从任何链接下载数据

如何从任何链接下载数据

本文介绍了如何从任何链接下载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在我的Qt程序中的以下链接上显示的字符串如何获得它?





我已编码以下文件,以获取字符串出现在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;

信号:
void downloaded();

私有插槽:

void fileDownloaded(QNetworkReply * reply);

private:
QString soName,soType;
QByteArray m_DownloadedData;

};

#endif // SKYOBJDESCRIPTION_H


.cpp档案

  #include< QString> 
#include< QUrl>
#include< QDebug>

#includeskyobjdescription.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()<<数据;
delete skd;

如果有其他方式从链接获取数据,请解释。
非常感谢:)

解决方案

你可能试验的是你的双重呼叫 QNetworkReply :: readAll



记住它是一个IO操作,并且没有办法多次读取netwrk回复。



只需注释您的调试行:

  void SkyObjDescription :: fileDownloaded(QNetworkReply * pReply)
{
// qDebug()<< pReply-> readAll();
m_DownloadedData = pReply-> readAll();
//发出一个信号
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);

信号:

公共位置:
void displayResult(QNetworkReply * reply);

};

#endif // TMP_H

//tmp.cpp
#includetmp.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()<<缓冲;
reply-> deleteLater();
}


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 :)

解决方案

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

这篇关于如何从任何链接下载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:33