PDA

View Full Version : QNetworkReply and "unknown error" problem



hybrid_snyper
30th November 2012, 10:48
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?


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.


void onFinished(QNetworkReply* r)
{
qDebug() << "Finished checking internet connection" << r->errorString();
emit connectionVaild();
}

All the best

anda_skoa
30th November 2012, 17:49
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,
_

Talei
1st December 2012, 00:50
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".


QString QIODevice::errorString() const
{
Q_D(const QIODevice);
if (d->errorString.isEmpty()) {
#ifdef QT_NO_QOBJECT
return QLatin1String(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
#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");"

ChrisW67
3rd December 2012, 00:45
Your QNetworkAccessManager and QNetworkReply are memory leaks. If you call this often or intend running for a long time then you should address these.