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中不可或缺的一部 ...
随机推荐
- Shiro 学习
<转载于 凯涛 博客> Shiro目录 第一章 Shiro简介 第二章 身份验证 第三章 授权 第四章 INI配置 第五章 编码/加密 第六章 Realm及相关对象 第七章 ...
- Chapter03 第四节 c++的算术运算符
c++算术运算符 3.4.1 运算符的优先级和结合性 四则运算参考四则运算优先级,注意括号. 3.4.2 除法的默认类型转换和精度问题 /* * @Description: 除法 * @Author: ...
- 学习kettle遇到的问题
一. 解决mysql连接缺少驱动问题:http://www.mamicode.com/info-detail-1724584.html 1.下载驱动 https://dev.mysql.com/dow ...
- java基础/数据加解密(Mooc)
一.消息摘要算法 常用摘要算法: 以下 (HEX)内容:bc指Bouncy Castle | cc指:Apache commons Codec 1.消息摘要算法MD5及MD族(MD2,MD4) 消 ...
- echars 饼图 --》二次封装
<template> <!-- 饼状图 1. 调用页面引入 import EcharsPie from '@/components/echarsPie.vue'; 注:自定义的组件名 ...
- 使用Dockerfile制作镜像
组成部分 基础镜像信息 FROM 维护者信息 MAINTAINER.LABEL 镜像操作指令 RUN.COPY.ADD.EXPOSE.WORKDIR.ONBUILD.US ...
- [转帖]探秘华为(二):华为和H3C(华三)的分道扬镳
探秘华为(二):华为和H3C(华三)的分道扬镳 https://baijiahao.baidu.com/s?id=1620781715767053734&wfr=spider&for= ...
- 如何在ubuntu上安装 搜狗输入法(已经成功)
转自:https://blog.csdn.net/qq_37589838/article/details/81208409 本文链接:https://blog.csdn.net/qq_37589838 ...
- linux中文件IO
一. linux常用文件IO接口 1.1. 文件描述符 1.1.1. 文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指 ...
- 2015GDCPC广东省赛总结
第二年参加省赛,也是我接触ACM这个行列已经过去了一年有余的里程碑.省赛个人排位,直到省赛现场赛结束后这段时间确实学到了很多很多的东西.按照惯例,先讲下比赛过程,题目3人分,我前,盛爷 ...