Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending http request to server

Sending http request to server

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dzakens
    wrote on last edited by
    #1

    I'm trying to send http request to home automation controller ( Vera 2) but i've got problem with reading the response. nam->get is giving empty reply. So i tired firtst to write to file than read from file( xml files) but when i do nam->get first slot functions is being executed later than parse function and i got error

    @QNetworkReply *MainWindow::send_request(QString request, int flag)
    {
    QString req = request;

    QNetworkReply *reply;
    
    QNetworkRequest rreq;
    
    rreq.setUrl(QUrl(req));
    
    QNetworkAccessManager *nam = new QNetworkAccessManager();
    
    bool ok = connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    
    reply = nam->get(rreq);
    
    parse_device_list();
    
    return reply;
    

    }@

    @void MainWindow::parse_device_list()
    {
    QFile *file = new QFile("devices_list.xml");

    if(!file->open(QIODevice::ReadOnly))
    {
        qDebug() << "error";
        return;
    
    }
    qDebug() << "passed";
    

    }
    @
    @
    void MainWindow::finishedSlot(QNetworkReply *reply)
    {
    qDebug() << "here";
    if (reply->error() == QNetworkReply::NoError)
    {

            // read data from QNetworkReply here
            //qDebug() << reply->readAll();
            QFile *file = new QFile&#40;"devices_list.xml"&#41;;
    
            if(!file->open(QIODevice::WriteOnly&#41;)
            {
                qDebug() << "error file";
                return;
            }
            else
            {
    
               file->write(reply->readAll());
    
            }
            file->close();
            //this->parse_device_list();
    
    
        qDebug() << "end";
    }
    // Some http error received
    else
    {
        qDebug() << "error write";// handle errors here
    }
    

    }@

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      At the moment you send the request but are calling method parse_device_list before it is completed. You should send a request, then to wait until you receive the "finish signal":http://qt-project.org/doc/qt-5.0/qtnetwork/qnetworkaccessmanager.html#finished for completed download and finally to save and parse the XML file.

      Following the good practices for OOP it would be better to have separate classes for each operation, in your case download and file processing. Fortunately the Qt wiki contains a very simple and easy to use "class for download":http://qt-project.org/wiki/Download_Data_from_URL

      http://anavi.org/

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dzakens
        wrote on last edited by
        #3

        i thought that connect function is responsible for that - emit finished signal when transsmision is done

        1 Reply Last reply
        0
        • L Offline
          L Offline
          leon.anavi
          wrote on last edited by
          #4

          [quote author="dzakens" date="1363683056"]i thought that connect function is responsible for that - emit finished signal when transsmision is done[/quote]

          The connect methods connects signal to a slot. In your case you connect signal finished to slot finishedSlot. But the action is asynchronous so you should call parse_device_list after the download finishes.

          http://anavi.org/

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SetBetterPass
            wrote on last edited by
            #5

            If you want to download synchronous you can use QEventLoop to wait for signal, something like this:

            @ QNetworkAccessManager *nam = new QNetworkAccessManager();
            QNetworkReply reply;
            reply=nam.get(rreq);
            QEventLoop loop;
            connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
            connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot(QNetworkReply
            )));
            loop.exec();
            @
            this will run local loop until reply is finished and keep application responsive, also if you want download larger files you can use another slot to save data always there are some (QNetworkReply::readyRead() signal is sent) so you don't spend much RAM space(you have to write data at the end of file so new data wont overwrite older data).

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved