首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >网络请求失败

网络请求失败
EN

Stack Overflow用户
提问于 2017-06-16 00:45:26
回答 2查看 11K关注 0票数 2

当我使用一个用react原生语言编写的天气应用程序时,我在安卓系统上得到了‘网络请求失败’错误,而这个应用程序在iOS上运行的很好。

这是提取函数-

代码语言:javascript
运行
复制
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 )。在我的例子中,我没有使用本地主机的地方。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-19 04:27:52

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

票数 4
EN

Stack Overflow用户

发布于 2020-05-12 09:14:13

  1. android:usesCleartextTraffic="true"中添加AndroidManifest.xml行。
  2. 从android文件夹中删除所有调试文件夹。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44579098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档