Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Periodically check a url
Forum Updated to NodeBB v4.3 + New Features

Periodically check a url

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 2 Posters 522 Views
  • 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.
  • E Offline
    E Offline
    Eruvatar
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you need a thread?
      To trigger periodical activities you can use QTimer.
      The networking Qt APIs are also assynchronous.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eruvatar
        wrote on last edited by
        #3

        becose sometimes depending of the response i have to do several jobs, like open a webview, chage taht webview, lauch instalations, uninstalls, changes on files, etc

        1 Reply Last reply
        0
        • jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Keep in mind that changing UI is not allowed from other threads than the main (UI) thread

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Eruvatar
            wrote on last edited by Eruvatar
            #5

            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

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Eruvatar
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Eruvatar
                wrote on last edited by
                #7

                update2: tested with an intent casting the web on a chrome with an intent, once web is showing, the activity on back stops. and when we recover the view of the original apk, checking comes up again
                seems is a focus problem

                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