danial weaber
danial weaber

Reputation: 876

using QNetworkAccessManager GET multiple times

I am writing an application to request a web page at equal intervals in order to get any changes in it (to check whether new data is received). here how i did it.

 private:

 QNetworkReply *r;
 QNetworkAccessManager *m;
 QNetworkRequest request;
 QTimer *timer;

in the constructor ,

m = new QNetworkAccessManager(this);
timer = new QTimer(this);
connect(r , SIGNAL(readyRead()), this , SLOT(readit()));
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);

readit function,

void MainWindow::readit(){
QString st;
st=r->readAll();
m->deleteResource(request);
ui->textBrowser->append(st);
}

update function,

void MainWindow::update()
{
 request.setUrl(QUrl("http://localhost/test/default.php"));
 r = m->get(request);
}

my problem is m->get(request) gets the request at its first call only, when it is called again it does nothing. I did several experiments but end up with no success results. i changed the second request to another web page using a button click but it did nothing too.

So I need help from an expert how to update the get request and get new reply multiple times.

and also i want to know am i doing a correct thing or is there mo reliable methods to get data on data change from the server than checking for the website at regular intervals.

Upvotes: 1

Views: 1280

Answers (1)

I see following problems:

  1. readyRead fires an arbitrary number of times per request - including zero times (!), but you treat it as if it fired exactly once. Use the finished signal, which is does what you want: fires once, no more, no less.

  2. The update slot doesn't connect any slots to the request.

Upvotes: 3

Related Questions