android - React native 如何每 x 分钟从 api 中获取一次

标签 android react-native

我想在 android 上每 x 分钟运行一次获取(使用 React native)

function getMoviesFromApiAsync() { 
    return fetch('https://facebook.github.io/react-native/movies.json').then((response) => response.json()) .then((responseJson) => { 
  return responseJson.movies; }) .catch((error) => { console.error(error); }); 
}

我使用了 documentaion 中的示例,因为我仍然不确定我将如何进行这种提取。

我不确定我应该如何开始这个(也许制作一个 android native 处理程序?)。我只找到了一个组件,但它适用于 ios iOS Background Fetch API Implementation

最佳答案

 componentDidMount(){
  this.timer = setInterval(()=> this.getMovies(), 1000)
 }

async getMovies(){

 fetch('https://facebook.github.io/react-native/movies.json', {method: "GET"})
  .then((response) => response.json())
  .then((responseData) =>
  {
    //set your data here
     console.log(responseData);
  })
  .catch((error) => {
      console.error(error);
  });

}

关于android - React native 如何每 x 分钟从 api 中获取一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238609/

相关文章:

java - 如何将Gradle项目编译为可以在Android应用程序中使用的jar文件

android - gradle 从编译中排除组

android - I18nManager.forceRtl(false) 在 android 中不起作用

javascript - React Native 中的传播运算符导致 mocha 出现 'Unexpected token' 错误

javascript - 在 React Native 中使用 GraphRequest 将 mp4 发布到 Facebook

Java 2 API 导致 : Duplicate files copied in APK

java - 底部导航图标颜色更改

java - 如何使用android解析xml

node.js - 如何立即加载更改 - React Native

react-native - react native 签名的 APK 崩溃