Sending http request to server
-
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("devices_list.xml"); if(!file->open(QIODevice::WriteOnly)) { 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 }
}@
-
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
-
[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.
-
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).