Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 反应本机iOS模拟器网络连接_React Native_Fetch - Fatal编程技术网

React native 反应本机iOS模拟器网络连接

React native 反应本机iOS模拟器网络连接,react-native,fetch,React Native,Fetch,我试图在react原生应用程序中获取()一些数据,但失败了。因此,当应用程序启动时,我发现了NetInfo,并在使用iOS模拟器的每个MapViewonRegionChangeComplete事件上使用它(如果有必要,iPhone 5s和iPhone 6),每次NetInfo返回的状态都是未知。MapView运行良好,我可以在模拟器上使用Safari浏览web。我发现的与fetch()失败相关的最常见问题来自于在url中使用localhost的开发人员。我在用什么?作为我的url,所以我相信这是

我试图在react原生应用程序中获取()一些数据,但失败了。因此,当应用程序启动时,我发现了NetInfo,并在使用iOS模拟器的每个MapView
onRegionChangeComplete
事件上使用它(如果有必要,iPhone 5s和iPhone 6),每次NetInfo返回的状态都是
未知
。MapView运行良好,我可以在模拟器上使用Safari浏览web。我发现的与fetch()失败相关的最常见问题来自于在url中使用localhost的开发人员。我在用什么?作为我的url,所以我相信这是一个模拟器问题。我没有iOS设备可以测试,而且MapView还不能用于android,所以我不能在android上测试。有人能告诉我为什么吗

多谢各位

另外,我记录到控制台的获取错误是-
TypeError:networkrequest失败

这是我的密码:

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 an exception to reject the promise and trigger the outer .catch() handler.
      // By default, an error response status (4xx, 5xx) does NOT cause the promise to reject!
      console.log('Throwing error to .catch with statusText: ' + response.statusText);
      throw Error(response.statusText);
    }

    })
    .then(function(response) {
      console.log('Returning JSON to next then')
      return response.json();
    })
    .then(function(json) { // chain a second function when JSON is returned
      console.log('Returning this json to Api call in index.os.js' + json);
      return {
        city: json.name,
        //temperature: kelvinToF(json.main.temp),
        //decription: json.weather[0].description
      }
    })
    .catch(function (error) {
      console.log('My fetch Error: ' + error + 'Specific error: ' + error.message);
    })
}
第二次更新:除了下面的更新和信息,我已经把它缩小到这是一个iOS模拟器的问题。我在安卓设备上没有同样的问题。同样-模拟器唯一出现问题的时候是使用http:url发出fetch()请求。当我使用https:url时,模拟器很高兴。我还在模拟器中发现了一些允许http请求的开发设置。这没有给我任何改变

更新:我已经将问题缩小到url。如果我在react native中使用fetch,并且这个url的MYAPPKEY等于我的key,那么它会失败。如果我在浏览器中使用相同的url,它将返回我期望的json

另外,如果我在react native中使用fetch并使用此url,我不会得到
网络请求失败的
,我会得到有效的json

还有一个有趣的注意事项——如果我在浏览器中输入OpenWeatherMapURL,地址栏在返回json时会去掉http://部分。当我在浏览器中输入github地址时,地址栏会保留https://


另一个有趣的注意事项-如果我使用非https url,它将失败。

如果您使用react-native@0.28或更高版本,在Plist.Info中删除了以下内容:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSLocationWhenUse用途说明
增加了以下内容:

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<!--See http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ -->
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
n不使用时的位置说明
NSAppTransportSecurity
NSExceptionDomains
本地服务器
NSTemporary ExceptionalLowsInSecureHttpLoads
这与iOS中的ATS有关,请查看以下链接了解更多信息。
您基本上需要将您使用的http域添加到plist.Info

您应该至少提供一些代码我认为这不是代码问题。我已经在“更新:”下更新了我的问题。可能是因为iOS ATS机制,请尝试禁用它。非常感谢。这是正确的答案!如果你想获得更多的信任,请随意将其作为正式答案!谢谢你的补充信息和答案。我在另一次讨论中读到,苹果将在今年年底完全废除这一做法。不让我们选择从一个简单的api获取不需要使用凭据的数据有点傻。