首页 > 其他 > 详细

React Native中的网络请求

时间:2016-10-06 10:34:23      阅读:229      评论:0      收藏:0      [点我收藏+]

React Native中的网络请求fetch使用方法最为简单,但却可以实现大多数的网络请求,需要了解更多的可以访问:

https://segmentfault.com/a/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中的网络请求

原文:http://www.cnblogs.com/shaoting/p/5933405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!