React Native從零開始(七)Fetch網絡請求

React Native從零開始(七)Fetch網絡請求




先上效果圖




因爲網絡請求比較簡單,所以我們直接先開始看語法然後看這個GET和POST不同的實現就好。


一、語法使用


/*
 語法:
 fetch(參數)
 .then(完成的回調函數)
 .catch(失敗的回調函數)

 fetch(url,opts)
 .then((response) => {
 //請求成功後返回的對象response
 //例如:json、text等
 return response.text();
 return response.json();
 })
 .then((responseData) => {
 //處理請求得到的數據
 })
 .catch((error) => {
 //網絡請求失敗執行該回調函數,得到錯誤信息
 })
 * */

語法很簡單,感覺類似於Android新建Notification一樣。
then第一個是獲取整個數據,第二個是根據第一個數據轉換而來的你所需要的數據,也就是說你需要一個json數據那麼在第一步返回json就好。

二、具體實現
POST和GET基本上是一樣的只不過POST比GET的參數多了一個BODY,GET請求是將參數添加在url中而POST則放在body中即可。

1、GET請求

  getRequest(url){
        /*網絡請求的配置*/
        var opts = {
            method:"GET"
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) =>{
                alert(error);
            })
    }

2、POST請求

 postRequest(url){
        let formData = new FormData();
        formData.append("username","SuperBigLw");
        formData.append("password","123456");
        var opts = {
            method:"POST",
            body:formData
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) => {
                alert(error)
            })
    }


三、附上完整代碼
1.getData.js
/**
 * Created by 11158 on 2017-01-16.
 */
/**
 * Created by 11158 on 2017-01-16.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    TouchableOpacity
} from 'react-native';

/*
 語法:
 fetch(參數)
 .then(完成的回調函數)
 .catch(失敗的回調函數)

 fetch(url,opts)
 .then((response) => {
 //請求成功後返回的對象response
 //例如:json、text等
 return response.text();
 return response.json();
 })
 .then((responseData) => {
 //處理請求得到的數據
 })
 .catch((error) => {
 //網絡請求失敗執行該回調函數,得到錯誤信息
 })
 * */
var getUrl = "http://project.lanou3g.com/projects/bj/reactnative/login.php?username=lanou&password=123";
var postUrl = "http://project.lanou3g.com/projects/bj/reactnative/login.php";
class getData extends Component{
    getRequest(url){
        /*網絡請求的配置*/
        var opts = {
            method:"GET"
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) =>{
                alert(error);
            })
    }
    postRequest(url){
        let formData = new FormData();
        formData.append("username","SuperBigLw");
        formData.append("password","123456");
        var opts = {
            method:"POST",
            body:formData
        }
        fetch(url,opts)
            .then((response) => {
                return response.text();
            })
            .then((responseText) => {
                alert(responseText);
            })
            .catch((error) => {
                alert(error)
            })
    }
    render(){
        return(
            <View style={styles.container}>
                <TouchableOpacity onPress={this.getRequest.bind(this,getUrl)}>
                    <View style={styles.btn}>
                        <Text>GET</Text>
                    </View>
                </TouchableOpacity>
                <TouchableOpacity onPress={this.postRequest.bind(this,postUrl)}>
                    <View style={styles.btn}>
                        <Text>POST</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}
const styles = StyleSheet.create({
    container:{
        backgroundColor:"cyan",
        flexDirection:"row",
        justifyContent:"space-around",
        alignItems:"center",
        flex:1
    },
    btn:{
        width:60,
        height:30,
        borderWidth:1,
        borderRadius:3,
        borderColor:"black",
        backgroundColor:"yellow",
        justifyContent:"center",
        alignItems:"center"

    }
});
module.exports = getData;

2.index.android.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
var GetData = require("./getData");
export default class FetchDemo extends Component {
  render() {
    return (
      <GetData/>
    );
  }
}

const styles = StyleSheet.create({

});

AppRegistry.registerComponent('FetchDemo', () => FetchDemo);


最後說明一下這個URL可以將你上傳的用戶名和密碼參數返回給你本身,大家可以拿這個測試。

大家可以加好友交流學習
QQ:1115856293
微信:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章