標籤:
React Native中的網路請求fetch使用方法最為簡單,但卻可以實現大多數的網路請求,需要瞭解更多的可以訪問:
1190000003810652
/** * Sample React Native App * https://github.com/facebook/react-native * 周少停 2016-09-28 * fetch請求資料 header 參數 response轉json 請求方式 */import React, {Component} from ‘react‘;import { AppRegistry, StyleSheet, Text, View, TouchableOpacity} from ‘react-native‘;var Project = React.createClass({ render() { return ( <View style={styles.container}> <TouchableOpacity onPress={this.ssss}> <Text>訪問</Text> </TouchableOpacity> </View> ); }, ssss(){ fetch(‘http://www.pintasty.cn/home/homedynamic‘, { method: ‘POST‘, headers: { //header ‘token‘: ‘eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJVLTliZGJhNjBjMjZiMDQwZGJiMTMwYWRhYWVlN2FkYTg2IiwiZXhwaXJhdGlvblRpbWUiOjE0NzUxMTg4ODU4NTd9.ImbjXRFYDNYFPtK2_Q2jffb2rc5DhTZSZopHG_DAuNU‘ }, body: JSON.stringify({ //參數 ‘start‘: ‘0‘, ‘limit‘: ‘20‘, ‘isNeedCategory‘: true, ‘lastRefreshTime‘: ‘2016-09-25 09:45:12‘ }) }) .then((response) => response.json()) //把response轉為json .then((responseData) => { // 上面的轉好的json alert(responseData); / // console.log(responseData); }) .catch((error)=> { alert(‘錯誤了‘); }) }});const styles = StyleSheet.create({ container: { flex: 1, justifyContent: ‘center‘, alignItems: ‘center‘, backgroundColor: ‘#F5FCFF‘ }});AppRegistry.registerComponent(‘Project‘, () => Project);
完整源碼下載:https://github.com/pheromone/React-Native-1
React Native中的網路請求