问:如何在QNetworkRequest重现之后发出QNetworkReply Slot?

时间:2017-05-10 10:11:21

标签: qt visual-studio-2010 qt4.8

我正在尝试使用多个线程从云端请求数据。我的问题是在QnetworkReply完成replyFinished(QnetworkReply *)插槽后从未发出。我该怎么做?

pc_gateway.h

//Main class 
#include "dhvac_datacollector_thread.h"

class PC_Gateway : public QMainWindow
{
   Q_OBJECT 

    DHVAC_DataCollector_Thread *m_DHVAC_DataCollector_Thread[70]; 

    PC_Gateway(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~PC_Gateway();

   public slots:
      void slot_start_comm(); 
};

pc_gateway.cpp

#include "pc_gateway.h"

PC_Gateway::PC_Gateway(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{

}

PC_Gateway::~PC_Gateway()
{

}

void PC_Gateway::slot_start_comm()
{
    int nNumOfThreads = 4;

    for(int i=0; i<nNumOfThreads ; i++)
    {
       this->m_DHVAC_DataCollector_Thread[i] = new DHVAC_DataCollector_Thread(this);
       this->m_DHVAC_DataCollector_Thread[i]->start();
    } 
}

DHVAC_DataCollector_Thread.h

//Thread class
class DHVAC_DataCollector_Thread : public QThread
{
    Q_OBJECT

    public:
      DHVAC_DataCollector_Thread(QObject *parent);
      ~DHVAC_DataCollector_Thread();

      void run();
      void Stop();

    public slots:
       void replyFinished(QNetworkReply*);
};

DHVAC_DataCollector_Thread.cpp

#include "dhvac_datacollector_thread.h"

DHVAC_DataCollector_Thread::DHVAC_DataCollector_Thread(QObject *parent)
: QThread(parent)
{

}

DHVAC_DataCollector_Thread::~DHVAC_DataCollector_Thread()
{
}

void DHVAC_DataCollector_Thread::run()
{
    while(true)
    {
        QString sUrl = "https://api.uno.taww.com/api/v2/data/sensor/"+sNodeId+"/10m/_";  
        QUrl url(sUrl);
        bool bUrl = url.isValid(); 

        if(!bUrl) //If URL is invalid
         return false;

        QNetworkRequest request;    
        request.setUrl(url);    
        request.setRawHeader("api_token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9");
        request.setRawHeader("user_id","102473df5c9106e55d");
        request.setRawHeader("cache-control","no-cache");

       QNetworkAccessManager *manager = new QNetworkAccessManager();
       connect(manager, SIGNAL(finished(QNetworkReply*)),this,
               SLOT(replyFinished(QNetworkReply*)));
       QNetworkReply *reply = manager->get(request);
    }
}

void DHVAC_DataCollector_Thread::replyFinished(QNetworkReply *reply)  
{
  QByteArray responseData;
  if(reply->error() == QNetworkReply::NoError)
  {
    responseData = reply->readAll();
    QString sData (responseData);
    reply->close();
    reply->deleteLater();
    qDebug()<<"Raw data:" <<sData;  

  }
}

1 个答案:

答案 0 :(得分:1)

您可以在收到回复后尝试在replyFinished广告位中发送请求,而不是循环播放。只需执行一个请求,如果replyFinished正常工作,那么请执行第二个请求。

void DHVAC_DataCollector_Thread::replyFinished(QNetworkReply *reply)  
{
    QByteArray responseData;
    if(reply->error() == QNetworkReply::NoError)
    {
        responseData = reply->readAll();
        QString sData (responseData);
        reply->close();
        reply->deleteLater();
        qDebug()<<"Raw data:" <<sData;  
    }
    // nextRequest()
}

你也有泄漏。你创造了大量的QNetworkAccessManager并且永远不会释放。