I create an QThread using moveToThread(), where I use a QTimer to pull stuff from network instantly.
In construct I call
_http = new HttpAdapter(this);
_http = new HttpAdapter(this);
To copy to clipboard, switch view to plain text mode
and in periodically function run
_http->Download();
_http->Download();
To copy to clipboard, switch view to plain text mode
In httpAdapter header
QNetworkReply* _reply;
QNetworkReply* _reply;
To copy to clipboard, switch view to plain text mode
void HttpAdapter::Download() {
url += Server::get()->GetBaseUrl();
url += "get.xml";
QNetworkAccessManager * http = new QNetworkAccessManager;
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentLengthHeader, "0");
request.
setUrl(QUrl::fromUserInput(url
));
_reply = http->get(request);
connect(_reply,SIGNAL(finished()),this, SLOT(Finished()));
connect(_reply,SIGNAL(readyRead()),this, SLOT(ReadyData()));
connect(_reply,SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(foldersError(QNetworkReply::NetworkError)));
connect(_reply,SIGNAL(sslErrors ( const QList<QSslError> & )),this, SLOT(SslErrors( const QList<QSslError> &)));
}
void HttpAdapter::Download() {
QString url;
url += Server::get()->GetBaseUrl();
url += "get.xml";
QNetworkAccessManager * http = new QNetworkAccessManager;
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentLengthHeader, "0");
request.setUrl(QUrl::fromUserInput(url));
_reply = http->get(request);
connect(_reply,SIGNAL(finished()),this, SLOT(Finished()));
connect(_reply,SIGNAL(readyRead()),this, SLOT(ReadyData()));
connect(_reply,SIGNAL(error(QNetworkReply::NetworkError)),this, SLOT(foldersError(QNetworkReply::NetworkError)));
connect(_reply,SIGNAL(sslErrors ( const QList<QSslError> & )),this, SLOT(SslErrors( const QList<QSslError> &)));
}
To copy to clipboard, switch view to plain text mode
The on finish...
void HttpAdapter::Finished() {
DEBUGME_MSG("finished");
int httpCode = _reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if (httpCode != 200) {
INFO_DEBUGME
("HTTP CODE " + QString::number(httpCode
));
return;
}
if (!_reply->isFinished()) {
return;
}
if (!_reply->isReadable()) {
return;
}
_data = _reply->readAll();
_reply->deleteLater();
if (_data.isEmpty()) {
INFO_DEBUGME("DATA EMPTY");
return;
}
//success
}
void HttpAdapter::Finished() {
DEBUGME_MSG("finished");
int httpCode = _reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if (httpCode != 200) {
INFO_DEBUGME("HTTP CODE " + QString::number(httpCode));
return;
}
if (!_reply->isFinished()) {
return;
}
if (!_reply->isReadable()) {
return;
}
_data = _reply->readAll();
_reply->deleteLater();
if (_data.isEmpty()) {
INFO_DEBUGME("DATA EMPTY");
return;
}
//success
}
To copy to clipboard, switch view to plain text mode
And THE CRASH IS ON
_data = _reply->readAll();
_data = _reply->readAll();
To copy to clipboard, switch view to plain text mode
The crash happen randomly, I would say in 5% of the calls, Any idea why ??
Bookmarks