当我使用一个用react原生语言编写的天气应用程序时,我在安卓系统上得到了‘网络请求失败’错误,而这个应用程序在iOS上运行的很好。
这是提取函数-
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();
},
我在这里还提到了关于堆栈溢出的其他问题,但它们都围绕着将本地主机更改为本地ip地址(如127.0.0.1 )。在我的例子中,我没有使用本地主机的地方。
发布于 2017-06-19 04:27:52
检查与模拟器相邻的Android模拟器选项面板,因为您将拥有更多的选项,然后转到蜂窝,并检查数据状态和网络类型。尝试将网络类型更改为'Full',将数据状态更改为'Home'。尝试各种选择的网络类型和数据状态,并检查它是否工作.
发布于 2020-05-12 09:14:13
android:usesCleartextTraffic="true"
中添加AndroidManifest.xml
行。https://stackoverflow.com/questions/44579098
复制相似问题