gpt4 book ai didi

c++ - BB 10 Cascades 互联网检查

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:55:08 26 4
gpt4 key购买 nike

如果出现套接字错误,我需要检查 Internet 连接。我是这样进行的,

void Client::socketError(QAbstractSocket::SocketError socketError) {
HttpPost("https://www.google.co.in/");
}

void Client::HttpPost(QString URL ) {

QNetworkRequest request = QNetworkRequest();
request.setUrl(QUrl(URL));

QNetworkAccessManager *mNetworkAccessManager = new QNetworkAccessManager(this);
bool result = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(HttpResponse(QNetworkReply *)));

mNetworkAccessManager->get(request);
qDebug() << "::: Client.cpp Request made to Service :::";
}

void Client::HttpResponse(QNetworkReply* reply) {

if (reply->error() == QNetworkReply::NoError) {
qDebug() << "\n Internet Ok **********";
} else {
qDebug() << "\n No Internet **********" << reply->errorString();
showDialog("No Inernet");
}
}

我正在连接信号和插槽,例如

connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(socketError(QAbstractSocket::SocketError)));

但有时我会收到“没有 Internet 对话框”,即使 wi-fi 可用。为什么?


方法二:

// this method will return true If Net is available false other wise
// but even this is return false some times even Wi fi signal is Available

bool Client::isNetworkAvailable() {
bool isFound = false;
QNetworkConfigurationManager netMgr;
QList<QNetworkConfiguration> mNetList = netMgr.allConfigurations(QNetworkConfiguration::Active);

if (mNetList.count() > 0) {
if (netMgr.isOnline()) {
isFound = true;
}
}
qDebug() << "\n ************** isNetworkAvailable:::" << isFound;
return isFound;
}

我的代码有问题吗?

最佳答案

您的网络检查片段似乎有点太复杂了。您可以简单地调用以下方法:

bool QNetworkConfigurationManager::isOnline () const

Returns true if the system is considered to be connected to another device via an active network interface; otherwise returns false.

如果这对某人不起作用,则很可能意味着互联网连接不可靠,尤其是当它不是随机工作时。

关于c++ - BB 10 Cascades 互联网检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20812004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com