繁体   English   中英

网络请求失败 Android

[英]Network request failed Android

在使用 React Native 编写的天气应用程序时,我在 Android 上收到“网络请求失败”错误,而该应用程序在 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();
  },

我还在 stackoverflow 上提到了其他问题,但它们都围绕着将 localhost 更改为您的本地 IP 地址(如 127.0.0.1)。 就我而言,我没有在哪里使用过本地主机。

检查模拟器旁边的Android 模拟器选项面板,因为您将有更多选项,然后转到蜂窝并检查数据状态网络类型 尝试将网络类型更改为'Full'并将数据状态更改为'Home' 尝试网络类型和数据状态的各种选项,并检查其是否正常工作...

  1. AndroidManifest.xml添加android:usesCleartextTraffic="true"行。
  2. 从您的 android 文件夹中删除所有调试文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM