Android Netinfo.isConnected.fetch()返回true
我在我的应用程序中使用NetInfo,检查它是否连接到internetAndroid Netinfo.isConnected.fetch()返回true,android,ios,reactjs,react-native,Android,Ios,Reactjs,React Native,我在我的应用程序中使用NetInfo,检查它是否连接到internet NetInfo.isConnected.fetch().then( (isConnected) => { console.log('isConnected: ', isConnected); }); 如果我连接到路由器或移动数据处于打开状态,则返回true,如果未连接,则返回false 我的问题是,如果我连接到路由器或我的移动数据处于打开状态,即使它没有互联网连接。这仍然是真的 有办法解决这个问题吗?或其他检
NetInfo.isConnected.fetch().then( (isConnected) => {
console.log('isConnected: ', isConnected);
});
如果我连接到路由器或移动数据处于打开状态,则返回true,如果未连接,则返回false
我的问题是,如果我连接到路由器或我的移动数据处于打开状态,即使它没有互联网连接。这仍然是真的
有办法解决这个问题吗?或其他检查internet连接的解决方法/替代方法?这是由于RN存在错误造成的 这里列出了一些解决方法:
componentDidMount() {
const dispatchConnected = isConnected => this.props.dispatch(setIsConnected(isConnected));
NetInfo.isConnected.fetch().then().done(() => {
NetInfo.isConnected.addEventListener('change', dispatchConnected);
});
}
或
1-清除gradlew,在android文件夹中运行命令:
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import NetInfo from '@ react-native-community / netinfo';
class ConnectionInfo extends Component {
NetInfoSubcription = null;
constructor (props) {
super (props);
this.state = {
connection_status: false,
connection_type: null,
connection_net_reachable: false,
connection_wifi_enabled: false,
connection_details: null,
}
}
componentDidMount () {
this.NetInfoSubscribtion = NetInfo.addEventListener (
this._handleConnectivityChange,
);
}
componentWillUnmount () {
this.NetInfoSubscribtion && this.NetInfoSubscribtion ();
}
_handleConnectivityChange = (state) => {
this.setState ({
connection_status: state.isConnected,
connection_type: state.type,
connection_net_reachable: state.isInternetReachable,
connection_wifi_enabled: state.isWifiEnabled,
connection_details: state.details,
})
}
render () {
return (
<View>
<Text>
Connection Status: {this.state.connection_status? 'Connected': 'Disconnected'}
</Text>
<Text>
Connection Type: {this.state.connection_type}
</Text>
<Text>
Internet Reachable: {this.state.connection_net_reachable? 'YES': 'NO'}
</Text>
<Text>
Wifi Enabled: {this.state.connection_wifi_enabled? 'YES': 'NO'}
</Text>
<Text>
Connection Details: {'\ n'}
{this.state.connection_type == 'wifi'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'ssid:' + this.state.connection_details.ssid
+ '\ n'
+ 'bssid:' + this.state.connection_details.bssid
+ '\ n'
+ 'strength:' + this.state.connection_details.strength
+ '\ n'
+ 'ipAddress:' + this.state.connection_details.ipAddress
+ '\ n'
+ 'subnet:' + this.state.connection_details.subnet
+ '\ n'
+ 'frequency:' + this.state.connection_details.frequency
:
this.state.connection_type == 'cellular'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'cellularGeneration:' + this.state.connection_details.cellularGeneration
+ '\ n'
+ 'carrier:' + this.state.connection_details.carrier
:
'---'
}
</Text>
</View>
);
}
}
export default ConnectionInfo;
- Linux/MacOS----------------->./gradlew clean
- Windows PowerShell---------------->。\gradlew已清除
- Windows cmd----------------->gradlew clean
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import NetInfo from '@ react-native-community / netinfo';
class ConnectionInfo extends Component {
NetInfoSubcription = null;
constructor (props) {
super (props);
this.state = {
connection_status: false,
connection_type: null,
connection_net_reachable: false,
connection_wifi_enabled: false,
connection_details: null,
}
}
componentDidMount () {
this.NetInfoSubscribtion = NetInfo.addEventListener (
this._handleConnectivityChange,
);
}
componentWillUnmount () {
this.NetInfoSubscribtion && this.NetInfoSubscribtion ();
}
_handleConnectivityChange = (state) => {
this.setState ({
connection_status: state.isConnected,
connection_type: state.type,
connection_net_reachable: state.isInternetReachable,
connection_wifi_enabled: state.isWifiEnabled,
connection_details: state.details,
})
}
render () {
return (
<View>
<Text>
Connection Status: {this.state.connection_status? 'Connected': 'Disconnected'}
</Text>
<Text>
Connection Type: {this.state.connection_type}
</Text>
<Text>
Internet Reachable: {this.state.connection_net_reachable? 'YES': 'NO'}
</Text>
<Text>
Wifi Enabled: {this.state.connection_wifi_enabled? 'YES': 'NO'}
</Text>
<Text>
Connection Details: {'\ n'}
{this.state.connection_type == 'wifi'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'ssid:' + this.state.connection_details.ssid
+ '\ n'
+ 'bssid:' + this.state.connection_details.bssid
+ '\ n'
+ 'strength:' + this.state.connection_details.strength
+ '\ n'
+ 'ipAddress:' + this.state.connection_details.ipAddress
+ '\ n'
+ 'subnet:' + this.state.connection_details.subnet
+ '\ n'
+ 'frequency:' + this.state.connection_details.frequency
:
this.state.connection_type == 'cellular'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'cellularGeneration:' + this.state.connection_details.cellularGeneration
+ '\ n'
+ 'carrier:' + this.state.connection_details.carrier
:
'---'
}
</Text>
</View>
);
}
}
export default ConnectionInfo;
import React,{Component}来自'React';
从“react native”导入{样式表、文本、视图};
从“@react native community/NetInfo”导入NetInfo;
类ConnectionInfo扩展组件{
netinfo subcription=null;
建造师(道具){
超级(道具);
此.state={
连接状态:false,
连接类型:null,
连接网络可访问:false,
连接\u wifi\u已启用:错误,
连接\u详细信息:空,
}
}
组件安装(){
this.NetInfo subscription=NetInfo.addEventListener(
这是.\u handleConnectivityChange,
);
}
组件将卸载(){
this.netinfo subscription&&this.netinfo subscription();
}
_HandleConnectionChange=(状态)=>{
这是我的国家({
连接状态:state.isConnected,
连接类型:state.type,
连接\u网络\u可访问:state.isInternetReach,
连接\u wifi\u已启用:state.isWifiEnabled,
连接详细信息:state.details,
})
}
渲染(){
返回(
连接状态:{this.state.Connection_Status?'Connected':'Disconnected'}
连接类型:{this.state.Connection\u Type}
可访问Internet:{this.state.connection\u net\u Reachable?'YES':'NO'}
已启用Wifi:{this.state.connection_Wifi_Enabled?'YES':'NO'}
连接详细信息:{'\n'}
{this.state.connection_type=='wifi'?
(this.state.connection\u details.isconnection昂贵?“连接昂贵:是”:“连接昂贵:否”)
+“\n”
+“ssid:”+this.state.connection_details.ssid
+“\n”
+'bssid:'+this.state.connection\u details.bssid
+“\n”
+“强度:”+this.state.connection_details.strength
+“\n”
+“ipAddress:”+this.state.connection\u details.ipAddress
+“\n”
+“子网:”+this.state.connection\u details.subnet
+“\n”
+“频率:”+this.state.connection_details.frequency
:
this.state.connection_type=='cellular'?
(this.state.connection\u details.isconnection昂贵?“连接昂贵:是”:“连接昂贵:否”)
+“\n”
+“CellArgeneration:”+this.state.connection\u details.CellArgeneration
+“\n”
+“carrier:”+this.state.connection_details.carrier
:
'---'
}
);
}
}
导出默认连接信息;
3。从以下位置从Android Studio重建项目:
import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import NetInfo from '@ react-native-community / netinfo';
class ConnectionInfo extends Component {
NetInfoSubcription = null;
constructor (props) {
super (props);
this.state = {
connection_status: false,
connection_type: null,
connection_net_reachable: false,
connection_wifi_enabled: false,
connection_details: null,
}
}
componentDidMount () {
this.NetInfoSubscribtion = NetInfo.addEventListener (
this._handleConnectivityChange,
);
}
componentWillUnmount () {
this.NetInfoSubscribtion && this.NetInfoSubscribtion ();
}
_handleConnectivityChange = (state) => {
this.setState ({
connection_status: state.isConnected,
connection_type: state.type,
connection_net_reachable: state.isInternetReachable,
connection_wifi_enabled: state.isWifiEnabled,
connection_details: state.details,
})
}
render () {
return (
<View>
<Text>
Connection Status: {this.state.connection_status? 'Connected': 'Disconnected'}
</Text>
<Text>
Connection Type: {this.state.connection_type}
</Text>
<Text>
Internet Reachable: {this.state.connection_net_reachable? 'YES': 'NO'}
</Text>
<Text>
Wifi Enabled: {this.state.connection_wifi_enabled? 'YES': 'NO'}
</Text>
<Text>
Connection Details: {'\ n'}
{this.state.connection_type == 'wifi'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'ssid:' + this.state.connection_details.ssid
+ '\ n'
+ 'bssid:' + this.state.connection_details.bssid
+ '\ n'
+ 'strength:' + this.state.connection_details.strength
+ '\ n'
+ 'ipAddress:' + this.state.connection_details.ipAddress
+ '\ n'
+ 'subnet:' + this.state.connection_details.subnet
+ '\ n'
+ 'frequency:' + this.state.connection_details.frequency
:
this.state.connection_type == 'cellular'?
(this.state.connection_details.isConnectionExpensive? 'Connection Expensive: YES': 'Connection Expensive: NO')
+ '\ n'
+ 'cellularGeneration:' + this.state.connection_details.cellularGeneration
+ '\ n'
+ 'carrier:' + this.state.connection_details.carrier
:
'---'
}
</Text>
</View>
);
}
}
export default ConnectionInfo;
Android Studio>文件>使缓存无效并重新启动