我正在尝试在我的react-native应用程序中获取()一些数据并且它失败了.因此,我发现NetInfo和我在应用程序启动时使用它,并在onRegionChangeComplete使用iOS模拟器的每个MapView 事件(如果重要的是,iPhone5s和iPhone 6)上使用它,并且unknown每次都返回NetInfo返回的状态.MapView工作正常,我可以在模拟器上使用Safari浏览网页.我发现与fetch()失败有关的最常见问题是开发人员在其url中使用localhost.我正在使用http://api.openweathermap.org/data/2.5/weather?作为我的网址,所以我相信这是一个模拟器问题.我没有iOS设备可以测试,而MapView还没有用于Android,所以我无法在Android上测试.有人能指出我正确的方向吗?
谢谢.
PS.我正在登录到控制台的获取错误是 - TypeError: Network request failed
这是我的代码:
module.exports = function(latitude, longitude) {
var rootURL = 'http://api.openweathermap.org/data/2.5/weather?APPID=MYAPIKEY&lat=35&lon=139';
console.log(rootURL);
return fetch(rootURL) // fetch is part of react or react-native, no need to import
.then(function(response){ // method chaining used here
// Shorthand to check for an HTTP 2xx response status.
// See https://fetch.spec.whatwg.org/#dom-response-ok
if (response.ok) {
console.log('Response from fetch was ok, returning response to next then');
return response
} else {
// Raise …Run Code Online (Sandbox Code Playgroud)