1.0.1 • Published 7 years ago

ls-network v1.0.1

Weekly downloads
4
License
MIT
Repository
-
Last release
7 years ago

LSNetwork

安装

LSNetwork is available through NPM. To install it, simply add the following line to your command line:

npm install
react-native link

使用

ReactNative调用

import { NativeModules } from 'react-native'  //方法调用
import { NativeEventEmitter } from 'react-native' //通知调用
const netModules = NativeModules.LSRNRequest;
const networkStateEmitter = new NativeEventEmitter(netModules);

网络配置

netModules.networkConfig(object)

object为对象,可选属性为

baseUrl(string)//全局请求首地址

header(object)//公共Header

parameter(object)//公共参数

useSecurityPolicy(bool) //是否启用证书验证

networkEnvironment(int)//环境切换,影响证书使用,1:开发环境、2:预发环境、3:正式环境,预发环境和正式环境会启用对应的证书验证

网络请求

netModules.post(object).then(response => {
}).catch((error) => {
});
netModules.get(object).then(response => {
}).catch((error) => {
});

object可选属性为

url(string)//请求地址

params(object)//请求参数

tag(int)//请求tag

header(object)//请求header

baseUrl(string)//请求首地址,覆盖全局请求首地址

timeout(int)//超时时间,默认60秒

loading(bool)//是否显示loading,loading样式设置见另外的文档

cache(bool)//是否启用缓存

cacheSecond(int)//缓存时间,单位秒,启用缓存必设缓存时间或版本

cacheVersion(longlong)//缓存版本,同上

批量网络请求

netModules.batch(array).then(response => {
}).catch((error) => {
});

入参为object的array,object的设置同网络请求,增加了

method(string)//请求方式,“post”或“get”

返回结果的response为各个请求返回结果组成的array

返回结果的error为第一个失败请求的error

取消网络请求

netModules.cancelRequest(tag)

通知网络状态

netModules.startNetworkMonitor();//启动网络状态通知
componentWillMount() {
  subscription = networkStateEmitter.addListener(
    'NetworkState',
    (networkState) => {
      console.log(networkState);
    }
  );//监听
}
componentWillUnmount() {
  subscription.remove()//移除监听
}

networkState(int)

状态为

0:未连接网络

1:wifi

2:2g

3:3g

4:4g

5:蜂窝网络

9:未知网络

Author

Mi, roy.mi@lianshang.com

License

LSNetwork is available under the MIT license. See the LICENSE file for more info.