react native之使用 Fetch进行网络数据请求
这是一个单独的页面,可以从其他地方跳转过来。
输入语言关键字,从github检索相关数据
import React, {Component} from 'react';
import {
  StyleSheet,
  View,
  Text,
  Button,
  TextInput,
} from 'react-native';
export default class FetchDemoPage extends Component {
  constructor(props){
    super(props);
    this.state={
      showText:''
    }
  }
  loadData(){
    let url = `https://api.github.com/search/repositories?q=${this.searchKey}`;
    fetch(url)
          .then(response => response.text())
          .then(responseText => {
            this.setState({
                showText:responseText
            })
          })
  }
  loadData2(){
    let url = `https://api.github.com/search/repositories?q=${this.searchKey}`;
    fetch(url)
          .then(response => {
            if(response.ok){
              return response.text();
            }
            throw new Error('Network not ok');
          })
          .then(responseText => {
            this.setState({
                showText:responseText
            })
          })
          .catch(e=>{
            this.setState({
                showText:e.toString()
            })
          })
  }
  render(){
    const {navigation} = this.props;
    return (
      <View style={styles.container}>
        <Text>'FetchDemoPage'</Text>
        <View style={styles.input_container}>
          <TextInput
            style={styles.input}
            onChangeText={text => {
              this.searchKey = text;
            }}
            />
          <Button
            title='获取'
            onPress={()=>{
              this.loadData2();
            }}
          />
        </View>
        <Text>
         {this.state.showText}
        </Text>
      </View>
      );
  }
}
const styles = StyleSheet.create({
  container:{
    flex: 1,
    backgroundColor:'#F5FCFF',
  },
  text:{
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  input:{
    height: 30,
    flex: 1,
    borderColor: 'black',
    borderWidth: 1,
    marginRight: 10,
  },
  input_container:{
    flexDirection: 'row',
    justifyContent: 'center',
  }
});

react native之使用 Fetch进行网络数据请求的更多相关文章
- 在React Native中,使用fetch网络请求 实现get 和 post
		//在React Native中,使用fetch实现网络请求 /* fetch 是一个封装程度更高的网络API, 使用了Promise * Promise 是异步编程的一种解决方案 * Promise ... 
- 使用react native制作的一款网络音乐播放器
		使用react native制作的一款网络音乐播放器 基于第三方库 react-native-video设计"react-native-video": "^1.0.0&q ... 
- iOS - NetRequest   网络数据请求
		1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ... 
- 网络数据请求request
		关于网络数据请求的类很多,httpwebrequest,webrequest,webclient以及httpclient,具体差别在此不在赘述,在应用方面介绍webclient与httpclient则 ... 
- android翻译应用、地图轨迹、视频广告、React Native知乎日报、网络请求框架等源码
		Android精选源码 android实现高德地图轨迹效果源码 使用React Native(Android和iOS)实现的 知乎日报效果源码 一款整合百度翻译api跟有道翻译api的翻译君 RxEa ... 
- swift网络数据请求方法
		搭建一个apache服务器,用php编写一个返回给客户端请求数据的脚本 <?php // header("Content-type:text/html;charset=utf-8&qu ... 
- React native 中使用Fetch请求数据
		一.代码 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from ' ... 
- React Native原生模块向JS传递数据的几种方式(Android)
		一般情况可以分为三种方式: 1. 通过回调函数Callbacks的方式 2. 通过Promises的异步的方式 3. 通过发送事件的事件监听的方式. 参考文档:传送门 
- React Native网络编程之Fetch
		目录 1.前言 2.什么是Fetch 3.最简单的应用 4.支持的请求参数 - 4.1. 参数详讲 - 4.2. 示例 5.请求错误与异常处理 1. 前言 网络请求是开发APP中不可或缺的一部 ... 
随机推荐
- TField中的GetText和SetText
			在数据表中的某些字段出于性能或数据规范化的考虑,会用组编号代替,就像学生有学号,员工有员工ID一样,但我们看的时候如果直接输入这样的编号看的人可能就会头痛了,这时就可用TField中的GetText转 ... 
- 11.metasploit辅助模块----基本Exp----ARP欺骗中间人MITM----WordPress破解
			metasploit辅助模块 信息收集 auxiliary scanners 使用metasploitable靶机 桥接 同一局域网 msfconsole nmap -sT 靶机IP nmap -sS ... 
- [转帖]22款让Kubernetes锦上添花的开源工具
			22款让Kubernetes锦上添花的开源工具 http://soft.zhiding.cn/software_zone/2019/0506/3117650.shtml 找时间尝试一下. 至顶网软件频 ... 
- 解决MySql忘记密码
			描述:忘记了mysql的登录密码,无法登录的情况下该怎么办? 环境:CentOS 7,数据库:mysql 5.7 1.停止数据库(先查看mysql服务是否运行) # ps -ef | -i grep ... 
- 关于this与e.target区别以及data-*属性
			1 this与event.target 在编写事件函数时可以传入一个event参数,even参数可以使用一个target属性如even.target用以调用,其作用是指向返回事件的目标节点(触发该事件 ... 
- Vue Element使用第三库icon图标
			一:引入单设图标 1.打开 阿里icon,注册 >登录>图标管理>我的项目 2.新建项目 返回阿里icon首页,点进去你想要的icon库,因为没有批量导入购物车,所以一般情况下需要一 ... 
- mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement
			linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ... 
- 卸载yum-mysql
			注意事项:1. 卸载yum MYSQLsystemctl status mysqlsystemctl stop mysqlsystemctl disable mysqld rpm -qa | grep ... 
- shell脚本又是什么?
			操作系统命令的组合称为Shell脚本: 一个Shell脚本主要由原本需要在命令行输入的命令组成,或在一个文本编辑器中,用户可以使用脚本来把一些常用的操作组合成一组串行.主要用来书写这种脚本的语言叫做脚 ... 
- DEV控件的分页控件,实现勾选复选框
			/// <summary> /// 单元格的点击事件 /// </summary> /// <param name="sender"></ ... 
