ios - React-Native NetInfo 总是返回 No Internet connection
<p><p>我目前正在尝试在我的一个 react-native 应用程序中实现 Web 服务调用,并使用 <a href="https://www.npmjs.com/package/react-native-web-service-handler" rel="noreferrer noopener nofollow">react-native-web-service-handler</a>调用 <code>Get</code> 和 <code>Post</code> Web API。</p>
<p>请看下面我的函数。</p>
<pre><code>getRandomUserList() {
WebServiceHandler.get(APIConstants.baseURL, null, null)
.then((val)=>{
console.log('callapi: ' + JSON.stringify(val))
this.setState({data:val})
})
.catch((error) => console.log('callapi:'+ JSON.stringify(error)));
}
</code></pre>
<p><code>APIConstants</code> 如下,</p>
<pre><code>const APIConstants = {
baseURL:"https://randomuser.me/api/?results=50"
}
</code></pre>
<p>我是这样调用这个函数的,</p>
<pre><code>componentDidMount() {
this.getRandomUserList();
}
</code></pre>
<p>请找到<code>WebServiceHandler.get</code>函数如下,</p>
<pre><code>static get(url: String, headerParam: Object, parameter: Object){
console.log('WebServiceHandler:Initiating GET request');
return new Promise(function(success, failed){
NetInfo.isConnected.fetch().done((isConnected) => {
console.log('WebServiceHandler: Network Connectivity status: ' + isConnected);
if (!isConnected) {
failed({name:'503',message:"No Internet connection"});
}else {
let URL = url + WebServiceHandler.parameter(parameter);
console.log('URL:-' + URL);
fetch(URL,{
method: 'get',
'headers': WebServiceHandler.header(headerParam)
})
.then(function(response) {
console.log(response.status);
if (!response.ok) {
throw {name:response.status,message:"http request failed", value:response.json()};
}
return response.json();
})
.then(function(jsonResponse) {
console.log('************************ HTTP GET Succes ************************ ');
success(jsonResponse);
})
.catch(function(err) {
console.log('************************ HTTP GET Failed **************************');
failed(err)
});
}
});
});
}
</code></pre>
<p>它总是返回以下输出,</p>
<pre><code>WebServiceHandler: Network Connectivity status: false
App.js:50 callapi:{"name":"503","message":"No Internet connection"}
</code></pre>
<p>我在 Wi-Fi 和宽带连接上尝试了很多,但没有成功。</p>
<p>经过一番研究,我发现<a href="https://github.com/facebook/react-native/issues/8615" rel="noreferrer noopener nofollow">this article on Github</a>并实现了这个功能。</p>
<pre><code>static isNetworkConnected() {
if (Platform.OS === 'ios') {
return new Promise(resolve => {
const handleFirstConnectivityChangeIOS = isConnected => {
NetInfo.isConnected.removeEventListener('connectionChange', handleFirstConnectivityChangeIOS);
resolve(isConnected);
};
NetInfo.isConnected.addEventListener('connectionChange', handleFirstConnectivityChangeIOS);
});
}
return NetInfo.isConnected.getConnectionInfo();
}
</code></pre>
<p>但它仍然给我同样的错误信息。谁能帮我解决这个问题?</p>
<p>仅供引用。您可以找到相同的 <a href="https://stackoverflow.com/questions/38730904/react-native-netinfo-always-returns-offline" rel="noreferrer noopener nofollow">question</a>在这里没有答案。</p>
<p>谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>终于得到答案了,变通办法是这样的,</p>
<pre><code>static isNetworkConnected() {
return NetInfo.getConnectionInfo().then(reachability => {
if (reachability === 'unknown') {
return new Promise(resolve => {
const handleFirstConnectivityChangeIOS = isConnected => {
NetInfo.isConnected.removeEventListener('connectionChange', handleFirstConnectivityChangeIOS);
resolve(isConnected);
};
NetInfo.isConnected.addEventListener('connectionChange', handleFirstConnectivityChangeIOS);
});
}
reachability = reachability.toLowerCase();
return (reachability !== 'none' && reachability !== 'unknown');
});
}
</code></pre>
<p>我们应该实现 <code>getConnectionInfo()</code> 因为 <code>fetch()</code> 是不推荐使用的方法。</p>
<p>或者</p>
<p>用 <code>NetInfo.getConnectionInfo()</code> 更新 <code>NetInfo.isConnected.fetch()</code>。这也可以。</p>
<p>谢谢。</p></p>
<p style="font-size: 20px;">关于ios - React-Native NetInfo 总是返回 No Internet connection,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49572815/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49572815/
</a>
</p>
页:
[1]