2017-06-16 249 views
1

在以原生答案编写的天气应用程序上工作时,我在Android上遇到了“网络请求失败”错误,而应用程序在iOS上正常工作。网络请求失败Android

enter image description here

这里是取功能 -

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(); 
    }, 

我也参考了其他的问题在这里计算器,但他们都围绕着变化的localhost像127.0.0.1你的本地IP地址。在我的情况下,我没有使用localhost。

+0

你在模拟器或物理设备 –

+0

测试它@RaviRaj我在运行的牛轧糖在Nexus 6P模拟器测试它。 –

+1

检查您的模拟器附近的模拟器选项面板,在那里你会有更多的选择,然后去蜂窝和检查数据状态和网络类型。尝试将网络类型更改为“完整”,将数据状态更改为“主页”。检查它的工作。 –

回答

1

检查相邻的Android模拟器选项面板到你的模拟器中,你将有更多的选项然后去细胞和检查数据状态网络类型。尝试将网络类型更改为'完整'并将数据状态更改为'主页'。尝试对网络类型和数据状态的各种选项,并检查其工作...