Symptom
The mobile phone is connected to the Internet and can access web pages properly. However, calling connection.hasDefaultNet() fails, and the callback function returns an error.
Cause
The application has not applied for the ohos.permission.GET_NETWORK_INFO permission.
Solution
The ohos.permission.GET_NETWORK_INFO permission is required for using the connection.hasDefaultNet API. In the stage model, declare the permission in the module.json5 configuration file. The code snippet is as follows:
{
"module": {
// ...
"requestPermissions": [
{
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "$string:reason",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "inuse"
}
}
]
}
} Reference