Javascript 网络请求失败

Javascript 网络请求失败,javascript,android,react-native,Javascript,Android,React Native,在使用react native编写的天气应用程序时,我在Android上遇到了“网络请求失败”错误,而该应用程序在iOS上运行良好 以下是fetch函数- componentDidMount: function() { navigator.geolocation.getCurrentPosition( location => { // this variable will contain the full url with the new lat and lo

在使用react native编写的天气应用程序时,我在Android上遇到了“网络请求失败”错误,而该应用程序在iOS上运行良好

以下是fetch函数-

componentDidMount: function() {
    navigator.geolocation.getCurrentPosition(
    location => {
      // this variable will contain the full url with the new lat and lon
      var formattedURL = REQUEST_URL + "lat=" + location.coords.latitude + "&lon=" + location.coords.longitude+"&APPID=e5335c30e733fc682907a126dab045fa";

      // this will output the final URL to the Xcode output window
      console.log(location.coords.latitude);
      console.log(location.coords.longitude);
      console.log(formattedURL);

      // get the data from the API
      this.fetchData(formattedURL);

      },
    error => {
      console.log(error);
    });
  },

  // fetchdata takes the formattedURL, gets the json data and
  // sets the apps backgroundColor based on the temperature of
  // the location
  fetchData: function(url) {
    fetch(url)
      .then((response) => response.json())
      .then((responseData) => {

        // set the background colour of the app based on temperature
        var bg;
        var temp = parseInt(responseData.main.temp);
        if(temp < 14) {
          bg = BG_COLD;
        } else if(temp >= 14 && temp < 25) {
          bg = BG_WARM;
        } else if(temp >= 25) {
          bg = BG_HOT;
        }

        // update the state with weatherData and a set backgroundColor
        this.setState({
          weatherData: responseData,
          backgroundColor: bg
        });
      })
      .done();
  },
componentDidMount:function(){
navigator.geolocation.getCurrentPosition(
位置=>{
//此变量将包含带有新lat和lon的完整url
var formattedURL=REQUEST_URL+“lat=“+location.coords.latitude+”&lon=“+location.coords.longitude+”&APPID=e5335c30e733fc682907a126dab045fa”;
//这将把最终的URL输出到Xcode输出窗口
console.log(location.coords.latitude);
console.log(location.coords.longitude);
日志(格式化的URL);
//从API获取数据
此.fetchData(格式化URL);
},
错误=>{
console.log(错误);
});
},
//fetchdata获取格式化的URL,获取json数据并
//根据应用程序的温度设置应用程序背景颜色
//地点
fetchData:函数(url){
获取(url)
.then((response)=>response.json())
.然后((响应数据)=>{
//根据温度设置应用程序的背景颜色
var-bg;
var temp=parseInt(responseData.main.temp);
如果(温度<14){
bg=bg_冷;
}否则,如果(温度>=14和温度<25){
bg=bg_温暖;
}否则,如果(温度>=25){
bg=bg_热;
}
//使用weatherData和设置的背景色更新状态
这是我的国家({
天气数据:responseData,
背景颜色:bg
});
})
.完成();
},

我在这里也提到了关于stackoverflow的其他问题,但它们都围绕着将localhost更改为您的本地ip地址,如127.0.0.1。在我的例子中,我没有where used localhost。

检查你的Android emulator选项面板,在该面板中,你将有更多的选项,然后转到蜂窝并检查数据状态网络类型。尝试将网络类型更改为'Full',并将数据状态更改为'Home'。尝试网络类型和数据状态的各种选项,并检查其是否工作

  • 在android manifest.xml中添加
    android:usesCleartextTraffic=“true”
  • 从android文件夹中删除所有调试文件夹

  • 您正在模拟器或物理环境中测试它device@RaviRaj我正在运行Nougat的Nexus 6P上的仿真器中测试它。请检查仿真器旁边的android仿真器选项面板,这样您将有更多选项,然后转到cellular并检查数据状态和网络类型。尝试将网络类型更改为“已满”,并将数据状态更改为“主”。检查它是否工作。谢谢你的回答。默认设置如您所说。问题是仿真器上的手机已关闭,这就是导致错误的原因!我已经发布了评论作为答案,你能接受它吗?这样其他人会发现它有用,提高我的声誉,并且问题可以在回答时关闭。