Periodically check a url
-
Hello, long time on this forum, but this is the first time i have to check.
Im diveing on the qt for adroid crazy world now and actually i feel like when i started with qt.I am trying to do a library on a thread to check periodically a url looking for orders.
Actually i was able to run the thread and make a single check to the url, but i was unable to make it keep running the checking.tested with and without qeventloop, without it dont even start the thread.
tested with qtimer in several modes but no way.
actually tried to sleep for 5secs and relauch check_url(), this was the only way to try to resend the http check, but it doenst work.this is my actual code
#include "url_checker.h" #include <QNetworkRequest> #include <QSettings> #include <QDebug> #include <QEventLoop> url_checker::url_checker(QObject *parent) : QThread(parent), running(true), network_manager(nullptr) {} void url_checker::run() { qCritical()<< "escaparateII STARTING run() on url_checker"; QSettings datos("ex.me.escaApRate", "data"); my_id = datos.value("id").toString(); network_manager = new QNetworkAccessManager(); network_manager->moveToThread(this); connect(this, &url_checker::started, this, &url_checker::check_url); connect(this, &url_checker::finished, this, &url_checker::deleteLater); QTimer timer; connect(&timer, &QTimer::timeout, this, &url_checker::check_url); timer.start(5000); QEventLoop loop; connect(this, &QThread::finished, &loop, &QEventLoop::quit); loop.exec(); qCritical()<< "escaparateII LOOP OUT"; } void url_checker::stop() { running = false; quit(); qCritical()<< "escaparateII Loop STOP"; } void url_checker::check_url() { if (!running) { qCritical()<< "escaparateII THREAD STOPPED."; return; } qCritical()<< "escaparateII READY TO READ. "; QNetworkRequest request(QUrl(url + "receive.php")); QUrlQuery post_data; post_data.addQueryItem("id", my_id); QByteArray post_data_byte_array = post_data.toString(QUrl::FullyEncoded).toUtf8(); QNetworkReply *reply = network_manager->post(request, post_data_byte_array); connect(reply, &QNetworkReply::finished, this, &url_checker::on_finished); connect(reply, &QNetworkReply::errorOccurred, this, [](QNetworkReply::NetworkError code) { qDebug() << "Error : " << code; }); qCritical()<< "escaparateII Check SENT. "+request.url().toString(); } void url_checker::on_finished() { QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); if (reply) { if (reply->error() == QNetworkReply::NoError) { QString response = reply->readAll(); emit url_checked(response); qCritical()<< "escaparateII REPLY: " << response; } else { qDebug() << "Error checking URL:" << reply->errorString(); } reply->deleteLater(); } qCritical()<< "escaparateII WAITING FOR RECALL: "; // QThread::sleep(15); // qCritical()<< "escaparateII ReCALL: "; // check_url(); QTimer::singleShot(5000, this, &url_checker::check_url); }
Really apreciate any tip cose im starting getting crazy
Thjx in advance
-
Why do you need a thread?
To trigger periodical activities you can use QTimer.
The networking Qt APIs are also assynchronous. -
Keep in mind that changing UI is not allowed from other threads than the main (UI) thread
-
do you mean that if I create a webview from a response, and then i have to change it it will not work?
indeed, i just commented the "emit" and the thread keep asking the webpage.
but if in the slot of the emit i open a webview, then it stops asking the webpage for orders.how do you recommend me to proceed then?
by the way i do all the controls on mainwindow.cpp, this thread its supposed only to get commands from web
-
Update: tested using programatic inside the webfullscreenview.cpp ( the code that load the front web)
but webview keeps blocking background checks on the second web. basically when webview load the webpage on front, blocks "emit" and rest of web checks
i will try to do it with intents to a external webview, ill keep updateing post for if ppl on future have same issue