QNetworkReply and "unknown error" problem
Hi,
I have seen this question raised on these forums and I feel I have done everything correctly so I was wondering if someone could help point out my fault.
So I have the following code that behaves as expected when there is a connection to the outside world, however when an error occurs errorString tells me there was an "unknown error". Why?
Code:
void myClass::checkInternetConnection()
{
qDebug() << "checking internet connection";
QNetworkAccessManager* am = new QNetworkAccessManager;
connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
am
->get
(QNetworkRequest
(QUrl("http://google.com")));
}
the slot, I would expect this to always be a QNetwokReply::NoError condition.
Code:
void onFinished(QNetworkReply* r)
{
qDebug() << "Finished checking internet connection" << r->errorString();
emit connectionVaild();
}
All the best
Re: QNetworkReply and "unknown error" problem
I am not sure what you want to know.
Is the problem that the error message is not more descriptive or that finished() is emitted when an error occurs?
Cheers,
_
Re: QNetworkReply and "unknown error" problem
If I understand Your question correctly, then r->errorString() has default string if errorString for the internal QIODevice is empty.
That's because r->error() in this case has QNetworkReply::NoError, and QNetworkReply don't set QIODevice errorString in that condition, and hence, according to the src, default value is returned as "Unknown error".
Code:
{
if (d->errorString.isEmpty()) {
#ifdef QT_NO_QOBJECT
#else
return tr("Unknown error");
#endif
}
return d->errorString;
}
Set breakpoint on return values and see for Yourself what is returned, for me it was "return tr("Unknown error");"
Re: QNetworkReply and "unknown error" problem
Your QNetworkAccessManager and QNetworkReply are memory leaks. If you call this often or intend running for a long time then you should address these.