1、Fetch的使用

  fetch的使用非常简单,只需传入请求的url

fetch('https://facebook.github.io/react-native/movies.json');

  当然是否请求成功与数据的处理,我们还需处理成功与失败的回调

function getMoviesFromApiAsync() {
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
}

  通过response.json()将请求的返回数据转化成json数据以便使用。通过.then来对数据进行转化处理或最终暴露给调用者;.catch对异常的处理。

2、Fetch的简单封装

  废话少说,直接上代码;

  (1)封装工具

  代码如下:

//一个 Promise 就是一个代表了异步操作最终完成或者失败的对象
export default class HttpUtils{
static get=(url)=>{
return new Promise(((resolve, reject) => {//resolve 和 reject 函数被调用时,分别将promise的状态改为fulfilled(完成)或rejected(失败)
fetch(url)//默认是GET
.then(response=>response.json())//把数据解析成json格式,然后取出
.then(result=>{
resolve(result);//表示完成
})
.catch(error=>{
reject(error);//表示失败
})
})
)
};
static post=(url,data)=>{
return new Promise(((resolve, reject) => {
fetch(url,{
method:'POST',
header:{
'Accept':'application/json',//告诉服务器,我们能接受json格式的返回类型,
'Content-Type':'application/json',//告诉服务器,我们提交的数据类型
},
body:JSON.stringify(data),//(把你想提交得数据序列化为json字符串类型,然后提交)body中的数据就是我们需要向服务器提交的数据,比如用户名,密码等
})//返回 服务器处理的结果
.then(response=>response.json())
.then(result=>{
resolve(result);
})
.catch(error=> {
reject(error);
})
})
)
}
}
//数据转换成字符串 JSON.stringify(params)     //将数据JSON化 JSON.parse(responseJSON)

  (2)封装工具的使用

   1、首先引入头文件

import HttpUtils from './HttpUtils';

   2、实现对应的请求方法

  //get数据
onLoad=(url)=>{
HttpUtils.get(url)//调用自定义组件方法,返回一个Promise
.then(result=>{//then函数会返回一个新的promise
this.setState({
result:JSON.stringify(result),//序列化:转换为一个 (字符串)JSON字符串 });
console.log(result)
})
.catch(error=> {
this.setState({
result: JSON.stringify(error),//把错误信息格式化为字符串 });
console.log(result);
})
}; //模拟登陆Post
onSubmit=(url,data)=>{
HttpUtils.post(url,data)
.then(result=>{
this.setState({
result:JSON.stringify(result),//序列化:转换为一个 (字符串)JSON字符串 });
console.log(result);
})
.catch(error=> {
this.setState({
result: JSON.stringify(error),//把错误信息格式化为字符串 });
console.log(result);
})
};

   3、在需要使用的地方,发起请求;

        <TouchableOpacity style={{marginVertical: 20 }} onPress={() => this.onLoad('https://******/submit_ajax.ashx?action=APP_GetLines')}>
<Text>发送Get网络请求</Text>
</TouchableOpacity>
<TouchableOpacity style={{marginVertical: 20 }} onPress={() => this.onSubmit('https://******/submit_ajax.ashx?action=APP_GetCardslist',{Uid:'37985'})}>
<Text>发送POST网络请求</Text>
</TouchableOpacity>

  更多内容,请参考:React Native网络请求传送门>>>

3、获取网络状态

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
NetInfo,
ToastAndroid,
View
} from 'react-native'; class NetworkInfo extends Component {
constructor(props) {
super(props); this.state = {
isConnected: null,
connectionInfo: null,
}; } componentDidMount() {
//网络是否连接的监听
NetInfo.isConnected.addEventListener(
'isConnected',
this._handleConnectivityChange
); //网络状态变化的监听
NetInfo.addEventListener(
'statusChange',
this._handleNetStatusChange
); //检测网络是否连接
NetInfo.isConnected.fetch().done(
(isConnected) => { this.setState({ isConnected: isConnected }); }
);
//检测网络连接状态
NetInfo.fetch().done(
(connectionInfo) => { this.setState({ connectionInfo }); }
);
} componentWillUnmount() {
//卸载两个监听
NetInfo.isConnected.removeEventListener(
'isConnected',
this._handleConnectivityChange
);
NetInfo.removeEventListener(
'statusChange',
this._handleNetStatusChange
);
} _handleConnectivityChange = (isConnected) => {
ToastAndroid.show((isConnected ? 'online' : 'offline'), ToastAndroid.SHORT);
} _handleNetStatusChange = (status) => { ToastAndroid.show('当然网络状态:' + status, ToastAndroid.SHORT); } render(){ }
}

  这个组件已经在RN刚出来(俩平台同时支持)的时候就已经存在了,用法大家都已经很熟悉了,但是在0.48+版本中,出现了一些变化,前面的用法都会过期。

  主要完善了两个方面的问题

  - 目前的NetInfo API是分平台的.在iOS和Android平台上返回完全不同的值.

  - 目前的NetInfo API无法判定连接的手机网络是 2g, 3g, 还是 4g.

  贡献者为了不造成breaking changes,所以直接新增新的api,将前面的api改为黄色警告

  - `fetch`方法过时了,用`getConnection`替代
  - `change`事件过时了,用`connectionchange`替代.
  - `fetch`/`change`过时了,用`getConnection`/`connectionchange`替代。返回的枚举值也变了。具体查看下面的值 ConnectionType(设备的网络类型):

  跨平台:

    - none - 设备处于离线状态。

wifi - 设备处于联网状态且通过wifi链接,或者是一个iOS的模拟器。

cellular - 设备是通过Edge、3G、WiMax或是LTE网络联网的。

unknown - 发生错误,网络状况不可知

Android平台:

bluetooth - 蓝牙数据连接

ethernet - 以太网数据连接

wimax - WiMAX数据连接

EffectiveConnectionType(无线网络类型) :

    - 2g

    - 3g

    - 4g

    - unknown

  具体用法,请参考:NetInfo传送门>>>

4、@react-native-community/netinfo

  第一步:安装

yarn add @react-native-community/netinfo

npm install --save @react-native-community/netinfo

  第二步:关联项目

react-native link @react-native-community/netinfo

  第三步:在项目中引用

import NetInfo from "@react-native-community/netinfo";

  注意此处:使用过这个API之后,就不要再引用原有的NetInfo了,可能会引起冲突。如下:

import { NetInfo } from "react-native";

  案例代码。如下:

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/ import React from 'react';
import {Text, View,TouchableWithoutFeedback} from 'react-native';
import NetInfo from '@react-native-community/netinfo'; export default class IsConnected extends React.Component<{}, $FlowFixMe> {
state = {
isConnected: null,
// isConnectionExpensive: null,
};
// _checkIfExpensive = () => {
// NetInfo.isConnectionExpensive().then(isConnectionExpensive => {
// this.setState({isConnectionExpensive});
// });
// };
componentDidMount() {
NetInfo.isConnected.addEventListener(
'connectionChange',
this._handleConnectivityChange,
);
NetInfo.isConnected.fetch().done(isConnected => {
this.setState({isConnected});
});
} componentWillUnmount() {
NetInfo.isConnected.removeEventListener(
'connectionChange',
this._handleConnectivityChange,
);
} _handleConnectivityChange = isConnected => {
this.setState({
isConnected,
});
}; render() {
return (
<View style={{backgroundColor:'white',marginTop:20,flex:1}}>
<Text>{this.state.isConnected ? 'Online' : 'Offline'}</Text>
{/* <TouchableWithoutFeedback onPress={this._checkIfExpensive}>
<View>
<Text>
Click to see if connection is expensive:
{this.state.isConnectionExpensive === true
? 'Expensive'
: this.state.isConnectionExpensive === false
? 'Not expensive'
: 'Unknown'}
</Text>
</View>
</TouchableWithoutFeedback>*/}
</View>
);
}
}

  提示:代码中注释部分,是进行判断是否是付费网络,仅支持Android,但是在iOS上也不会出错,但会有⚠️警告信息。

@react-native-community/netinfo传送门>>>

React Native之Fetch简单封装、获取网络状态的更多相关文章

  1. [React Native]获取网络状态

    使用React Native,可以使用NetInfo API获取手机当前的各个网络状态. componentWillMount() { NetInfo.fetch().done((status)=&g ...

  2. 解决React Native使用Fetch API请求网络报Network request failed

    问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...

  3. React Native使用NetInfo对当前系统网络的判断

    有网状态: 断网状态: 代码如下: 注意:第一次参考了http://www.hangge.com/blog/cache/detail_1614.html代码,一直显示的是unknow状态... 最后处 ...

  4. 封装获取网络信息Linux—API类

    封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...

  5. reactNative-解决react native使用fetch函数 Network request failed 问题

    解决react native使用fetch函数Network request failed问题 最近公司新开发一个app, 用react native架构好后,用xcode模拟器打开app,对接登陆接 ...

  6. iOS 获取网络状态

    在iOS开发者,获取网络状态比较常用 -(NSString *)getNetWorkStates{ UIApplication *app = [UIApplication sharedApplicat ...

  7. android开发获取网络状态,wifi,wap,2g,3g.工具类(一)

    android开发获取网络状态整理: package com.gzcivil.utils; import android.content.Context; import android.net.Con ...

  8. Android获取网络状态

    Android获取网络状态 学习自 https://developer.android.google.cn/reference/android/net/ConnectivityManager http ...

  9. 微信小程序 --- 获取网络状态

    获取网络状态:wx.getNetworkType btnclick:function(){ wx.getNetworkType({ success:function(res){ console.log ...

随机推荐

  1. Java反射基础(一)

    构造方法的获取   1. 四个方法:getConstructors()获取所有的构造方法: getConstructor(parameters)获取匹配参数的构造方法: getDeclaredCons ...

  2. 系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing)

    系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing) UAT SIT在前,UAT在后,UAT测完才可以上线

  3. 【BZOJ4504】K个串 可持久化线段树+堆

    [BZOJ4504]K个串 Description 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计 ...

  4. IE强制不使用兼容模式

    [caption id="attachment_471" align="alignnone" width="431"] 强制不使用兼容模式[ ...

  5. net.mvc中并发限制

    浏览器限制 会话限制 线程限制 这篇文章讲的很详细: https://weblogs.asp.net/imranbaloch/concurrent-requests-in-asp-net-mvc 明白 ...

  6. Oracle实例的恢复、介质恢复( crash recovery)( Media recovery)

    实例的恢复( crash recovery) 什么时候发生Oracle实例恢复? shutdown abort; 数据库异常down掉(机器死机,掉电...) 实例恢复的原因是数据有丢掉,使用redo ...

  7. IO流入门-第四章-FileReader

    FileReader基本用法和方法示例 /* java.io.Reader java.io.InputStreamReader 转换流(字节输入流---->字符输入流) java.io.File ...

  8. 2015-03-18——mongodb的简单配置

    参考网址:http://www.cnblogs.com/mecity/archive/2011/06/11/2078527.html#3060056 mongod 数据库启动程序 mongo 数据库操 ...

  9. Django限制请求method

    1.常用的请求method 1.1 GET请求: GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改.比如向服务器获取某篇文章的详情. 1.2 POST请求: PO ...

  10. python内存泄露查找

    1 前言: 1.1 像Java程序一样,虽然Python本身也有垃圾回收的功能,但是同样也会产生内存泄漏的问题 1.2 在Python程序里,内存泄漏是由于一个长期持有的对象不断的往一个dict或者l ...