提交 b2436fb0 编写于 作者: Y Yangys

Solving Problems

Signed-off-by: NYangys <yangyousheng@huawei.com>
上级 0c733bb3
......@@ -18,32 +18,41 @@ HTTP数据请求功能主要由http模块提供。
| ----------------------------------------- | ----------------------------------- |
| createHttp() | 创建一个http请求。 |
| request() | 根据URL地址,发起HTTP网络请求。 |
| request2()<sup>10+</sup> | 根据URL地址,发起HTTP网络请求并返回流式响应|
| destroy() | 中断请求任务。 |
| on(type: 'headersReceive') | 订阅HTTP Response Header 事件。 |
| off(type: 'headersReceive') | 取消订阅HTTP Response Header 事件。 |
| once\('headersReceive'\)<sup>8+</sup> | 订阅HTTP Response Header 事件,但是只触发一次。|
| on\('dataReceive'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收事件。 |
| off\('dataReceive'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收事件。 |
| on\('dataEnd'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收完毕事件。 |
| off\('dataEnd'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收完毕事件。 |
| on\('dataProgress'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收进度事件。 |
| off\('dataProgress'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收进度事件。 |
## 开发步骤
1. import需要的http模块。
2. 创建一个HTTP请求,返回一个HttpRequest对象。
3. (可选)订阅HTTP响应头。
4. 根据URL地址,发起HTTP网络请求。
5. (可选)处理HTTP响应头和HTTP网络请求的返回结果。
1. 从@ohos.net.http.d.ts中导入http命名空间。
2. 调用createHttp()方法,创建一个HttpRequest对象。
3. 调用该对象的on()方法,订阅http响应头事件,此接口会比request请求先返回。可以根据业务需要订阅此消息。
4. 调用该对象的request()方法,传入http请求的url地址和可选参数,发起网络请求。
5. 按照实际业务需要,解析返回结果。
6. 调用该对象的off()方法,取消订阅http响应头事件。
7. 当该请求使用完毕时,调用destroy()方法主动销毁。
```js
// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个http请求任务,不可复用
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
httpRequest.request(
// 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"EXAMPLE_URL",
{
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
......@@ -55,19 +64,26 @@ httpRequest.request(
extraData: {
"data": "data to send",
},
connectTimeout: 60000, // 可选,默认为60s
readTimeout: 60000, // 可选,默认为60s
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
usingCache: true, // 可选,默认为true
priority: 1, // 可选,默认为1
connectTimeout: 60000, // 可选,默认为60000ms
readTimeout: 60000, // 可选,默认为60000ms
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
}, (err, data) => {
if (!err) {
// data.result为http响应内容,可根据业务需要进行解析
console.info('Result:' + data.result);
console.info('code:' + data.responseCode);
// data.header为http响应头,可根据业务需要进行解析
// data.result为HTTP响应内容,可根据业务需要进行解析
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
// data.header为HTTP响应头,可根据业务需要进行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + data.cookies); // 8+
console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
} else {
console.info('error:' + JSON.stringify(err));
// 该请求不再使用,调用destroy方法主动销毁。
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
}
......
......@@ -146,6 +146,8 @@ getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
获取网络的全局代理配置信息,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
......@@ -176,6 +178,8 @@ getGlobalHttpProxy(): Promise\<HttpProxy>;
获取网络的全局代理配置信息,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
......@@ -207,6 +211,8 @@ setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
设置网络全局Http代理配置信息,使用callback方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
......@@ -231,12 +237,12 @@ setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
**示例:**
```js
let ExclusionList=""
let array = ExclusionList.split(',');
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
host: "host",
port: 1,
parsedExclusionList: array
host: "192.168.xx.xxx",
port: 8080,
exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy, (error, data) => {
console.info(JSON.stringify(error));
......@@ -250,6 +256,8 @@ setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;
设置网络全局Http代理配置信息,使用Promise方式作为异步方法。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
......@@ -279,12 +287,12 @@ setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;
**示例:**
```js
let ExclusionList=""
let array = ExclusionList.split(',');
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
let httpProxy = {
host: "host",
port: 1,
parsedExclusionList: array
host: "192.168.xx.xxx",
port: 8080,
exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy).then((error, data) => {
console.info(JSON.stringify(data));
......@@ -317,10 +325,10 @@ getAppNet(callback: AsyncCallback\<NetHandle>): void
**示例:**
```js
connection.getAppNet(function(error, data)) {
connection.getAppNet(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
}
})
```
## connection.getAppNet<sup>9+</sup>
......@@ -799,6 +807,8 @@ enableAirplaneMode(callback: AsyncCallback\<void>): void
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
......@@ -830,6 +840,8 @@ enableAirplaneMode(): Promise\<void>
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
......@@ -861,6 +873,8 @@ disableAirplaneMode(callback: AsyncCallback\<void>): void
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**参数:**
......@@ -892,6 +906,8 @@ disableAirplaneMode(): Promise\<void>
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
**系统能力**:SystemCapability.Communication.NetManager.Core
**返回值:**
......@@ -1807,7 +1823,7 @@ connection.getDefaultNet().then(function (netHandle) {
| BEARER_WIFI | 1 | Wi-Fi网络。 |
| BEARER_ETHERNET | 3 | 以太网网络。 |
## HttpProxy
## HttpProxy<sup>10+</sup>
网络全局代理配置信息
......@@ -1817,7 +1833,7 @@ connection.getDefaultNet().then(function (netHandle) {
| ------ | ------ | --- |------------------------- |
| host | string | 否 | 代理服务器主机名。 |
| port | number | 否 | 主机端口。 |
| parsedExclusionList | Array<string> | 否 | 不使用代理服务器的屏蔽列表。 |
| exclusionList | Array<string> | 否 | 不使用代理服务器的屏蔽列表。 |
## NetSpecifier
......
......@@ -57,7 +57,7 @@ ethernet.setIfaceConfig("eth0", {
domain: "2.2.2.2"
}, (error) => {
if (error) {
console.log("setIfaceConfig callback error = " + error);
console.log("setIfaceConfig callback error = " + JSON.stringify(error));
} else {
console.log("setIfaceConfig callback ok ");
}
......@@ -116,7 +116,7 @@ ethernet.setIfaceConfig("eth0", {
}).then(() => {
console.log("setIfaceConfig promiss ok ");
}).catch(error => {
console.log("setIfaceConfig promiss error = " + error);
console.log("setIfaceConfig promiss error = " + JSON.stringify(error));
});
```
......@@ -155,15 +155,15 @@ getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>):
```js
ethernet.getIfaceConfig("eth0", (error, value) => {
if (error) {
console.log("getIfaceConfig callback error = " + error);
console.log("getIfaceConfig callback error = " + JSON.stringify(error));
} else {
console.log("getIfaceConfig callback mode = " + value.mode);
console.log("getIfaceConfig callback ipAddr = " + value.ipAddr);
console.log("getIfaceConfig callback route = " + value.route);
console.log("getIfaceConfig callback gateway = " + value.gateway);
console.log("getIfaceConfig callback netMask = " + value.netMask);
console.log("getIfaceConfig callback dnsServers = " + value.dnsServers);
console.log("getIfaceConfig callback domain = " + value.domain);
console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode));
console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr));
console.log("getIfaceConfig callback route = " + JSON.stringify(value.route));
console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway));
console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask));
console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers));
console.log("getIfaceConfig callback domain = " + JSON.stringify(value.domain));
}
});
```
......@@ -207,15 +207,15 @@ getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
```js
ethernet.getIfaceConfig("eth0").then((data) => {
console.log("getIfaceConfig promiss mode = " + data.mode);
console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr);
console.log("getIfaceConfig promiss route = " + data.route);
console.log("getIfaceConfig promiss gateway = " + data.gateway);
console.log("getIfaceConfig promiss netMask = " + data.netMask);
console.log("getIfaceConfig promiss dnsServers = " + data.dnsServers);
console.log("getIfaceConfig promiss domain = " + data.domain);
console.log("getIfaceConfig promiss mode = " + JSON.stringify(data.mode));
console.log("getIfaceConfig promiss ipAddr = " + JSON.stringify(data.ipAddr));
console.log("getIfaceConfig promiss route = " + JSON.stringify(data.route));
console.log("getIfaceConfig promiss gateway = " + JSON.stringify(data.gateway));
console.log("getIfaceConfig promiss netMask = " + JSON.stringify(data.netMask));
console.log("getIfaceConfig promiss dnsServers = " + JSON.stringify(data.dnsServers));
console.log("getIfaceConfig promiss domain = " + JSON.stringify(data.domain));
}).catch(error => {
console.log("getIfaceConfig promiss error = " + error);
console.log("getIfaceConfig promiss error = " + JSON.stringify(error));
});
```
......@@ -254,9 +254,9 @@ isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
```js
ethernet.isIfaceActive("eth0", (error, value) => {
if (error) {
console.log("whether2Activate callback error = " + error);
console.log("whether2Activate callback error = " + JSON.stringify(error));
} else {
console.log("whether2Activate callback = " + value);
console.log("whether2Activate callback = " + JSON.stringify(value));
}
});
```
......@@ -300,9 +300,9 @@ isIfaceActive(iface: string): Promise\<number>
```js
ethernet.isIfaceActive("eth0").then((data) => {
console.log("isIfaceActive promiss = " + data);
console.log("isIfaceActive promiss = " + JSON.stringify(data));
}).catch(error => {
console.log("isIfaceActive promiss error = " + error);
console.log("isIfaceActive promiss error = " + JSON.stringify(error));
});
```
......@@ -337,11 +337,11 @@ getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
```js
ethernet.getAllActiveIfaces((error, value) => {
if (error) {
console.log("getAllActiveIfaces callback error = " + error);
console.log("getAllActiveIfaces callback error = " + JSON.stringify(error));
} else {
console.log("getAllActiveIfaces callback value.length = " + value.length);
console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length));
for (let i = 0; i < value.length; i++) {
console.log("getAllActiveIfaces callback = " + value[i]);
console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i]));
}
}
});
......@@ -377,12 +377,12 @@ getAllActiveIfaces(): Promise\<Array\<string>>
```js
ethernet.getAllActiveIfaces().then((data) => {
console.log("getAllActiveIfaces promiss data.length = " + data.length);
console.log("getAllActiveIfaces promiss data.length = " + JSON.stringify(data.length));
for (let i = 0; i < data.length; i++) {
console.log("getAllActiveIfaces promiss = " + data[i]);
console.log("getAllActiveIfaces promiss = " + JSON.stringify(data[i]));
}
}).catch(error => {
console.log("getAllActiveIfaces promiss error = " + error);
console.log("getAllActiveIfaces promiss error = " + JSON.stringify(error));
});
```
......
......@@ -84,7 +84,7 @@ setBackgroundPolicy(isAllowed: boolean): Promise\<void>
**示例:**
```js
policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))).then((error, data) {
policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -151,7 +151,7 @@ isBackgroundAllowed(): Promise\<boolean>;
**示例:**
```js
policy.isBackgroundAllowed().then((error, data) {
policy.isBackgroundAllowed().then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -236,7 +236,7 @@ setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>;
let param = {
uid: Number.parseInt(this.firstParam), policy: Number.parseInt(this.currentNetUidPolicy)
}
policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy)).then((error, data) {
policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy)).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -313,7 +313,7 @@ getPolicyByUid(uid: number): Promise\<NetUidPolicy>;
**示例:**
```js
policy.getPolicyByUid(Number.parseInt(this.firstParam)).then((error, data) {
policy.getPolicyByUid(Number.parseInt(this.firstParam)).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -390,7 +390,7 @@ function getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>;
**示例:**
```js
policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then((error, data) {
policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -456,7 +456,7 @@ getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>;
**示例:**
```js
policy.getNetQuotaPolicies().then((error, data) {
policy.getNetQuotaPolicies().then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -541,7 +541,7 @@ let param = {netType:Number.parseInt(this.netType), iccid:this.iccid, ident:this
limitBytes:Number.parseInt(this.limitBytes), lastWarningRemind:this.lastWarningRemind, lastLimitRemind:this.lastLimitRemind, metered:Boolean(Number.parseInt(this.metered)), limitAction:this.limitAction};
this.netQuotaPolicyList.push(param);
policy.setNetQuotaPolicies(this.netQuotaPolicyList).then((error, data) {
policy.setNetQuotaPolicies(this.netQuotaPolicyList).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -619,7 +619,7 @@ restoreAllPolicies(iccid: string): Promise\<void>;
```js
this.firstParam = iccid;
policy.restoreAllPolicies(this.firstParam).then((error, data){
policy.restoreAllPolicies(this.firstParam).then(function(error, data){
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -706,7 +706,7 @@ isUidNetAllowed(uid: number, isMetered: boolean): Promise\<boolean>;
let param = {
uid: Number.parseInt(this.firstParam), isMetered: Boolean(Number.parseInt(this.isBoolean))
}
policy.isUidNetAllowed(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then((error, data) {
policy.isUidNetAllowed(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -792,7 +792,7 @@ isUidNetAllowed(uid: number, iface: string): Promise\<boolean>;
let param = {
uid: Number.parseInt(this.firstParam), iface: this.secondParam
}
policy.isUidNetAllowed(Number.parseInt(this.firstParam), this.secondParam).then((error, data) {
policy.isUidNetAllowed(Number.parseInt(this.firstParam), this.secondParam).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -877,7 +877,7 @@ setDeviceIdleAllowList(uid: number, isAllowed: boolean): Promise\<void>;
let param = {
uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
}
policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then((error, data) {
policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -943,7 +943,7 @@ getDeviceIdleAllowList(): Promise\<Array\<number>>;
**示例:**
```js
policy.getDeviceIdleAllowList().then((error, data) {
policy.getDeviceIdleAllowList().then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -1021,7 +1021,7 @@ getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>;
```js
this.firstParam = uid
policy.getBackgroundPolicyByUid(Number.parseInt(this.firstParam)).then((error, data) {
policy.getBackgroundPolicyByUid(Number.parseInt(this.firstParam)).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -1098,11 +1098,11 @@ resetPolicies(iccid: string): Promise\<void>;
**示例:**
```js
policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then((error, data) {
policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then(function(error, data) {
})
this.firstParam = iccid
policy.resetPolicies(this.firstParam).then((error, data) {
policy.resetPolicies(this.firstParam).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -1189,7 +1189,7 @@ updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType):
let param = {
netType: Number.parseInt(this.netType), iccid: this.firstParam, remindType: this.currentRemindType
}
policy.updateRemindPolicy(Number.parseInt(this.netType), this.firstParam, Number.parseInt(this.currentRemindType)).then((error, data) {
policy.updateRemindPolicy(Number.parseInt(this.netType), this.firstParam, Number.parseInt(this.currentRemindType)).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -1274,7 +1274,7 @@ setPowerSaveAllowList(uid: number, isAllowed: boolean): Promise\<void>;
let param = {
uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean))
}
policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then((error, data) {
policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......@@ -1340,7 +1340,7 @@ getPowerSaveAllowList(): Promise\<Array\<number>>;
**示例:**
```js
policy.getPowerSaveAllowList().then((error, data) {
policy.getPowerSaveAllowList().then(function(error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
......
......@@ -45,11 +45,11 @@ fetch(Object): void
## FetchResponse
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- |
| code | number | 表示服务器的状态code。 |
| data | string \| Object | 返回数据类型由responseType确定,详见表 responseType与success中data关系。 |
| headers | Object | 表示服务器response的所有header。 |
| 参数名 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| code | number | 是 | 否 | 表示服务器的状态code。 |
| data | string \| Object | 是 | 否 | 返回数据类型由responseType确定,详见表 responseType与success中data关系。 |
| headers | Object | 是 | 否 | 表示服务器response的所有header。 |
**表2** responseType与success中data关系
......
......@@ -2,7 +2,7 @@
> **说明:**
> - 从API Version 7 开始,该接口不再维护,推荐使用新接口[`@ohos.telephony.observer`](js-apis-observer.md)。
>
>
> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
......@@ -122,7 +122,9 @@ export default {
## NetworkResponse
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- |
| metered | boolean | 是否按照流量计费。 |
| type | string | 网络类型,可能的值有2g,3g,4g,5g,wifi,none等。 |
\ No newline at end of file
**系统能力:** SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| metered | boolean | 否 |是否按照流量计费。 |
| type | string | 是|网络类型,可能的值有2g,3g,4g,5g,wifi,none等。 |
\ No newline at end of file
......@@ -442,11 +442,6 @@ on\(type: 'open', callback: AsyncCallback<Object\>\): void
| type | string | 是 | 'open':WebSocket的打开事件。 |
| callback | AsyncCallback\<Object\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|-------|---------------------------|
| 401 | Parameter error. |
**示例:**
......@@ -476,12 +471,6 @@ off\(type: 'open', callback?: AsyncCallback<Object\>\): void
| type | string | 是 | 'open':WebSocket的打开事件。 |
| callback | AsyncCallback\<Object\> | 否 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|-------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......@@ -513,12 +502,6 @@ on\(type: 'message', callback: AsyncCallback<string | ArrayBuffer\>\): void
| type | string | 是 | 'message':WebSocket的接收到服务器消息事件。 |
| callback | AsyncCallback\<string \| ArrayBuffer <sup>8+</sup>\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|--------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......@@ -548,12 +531,6 @@ off\(type: 'message', callback?: AsyncCallback<string | ArrayBuffer\>\): void
| type | string | 是 | 'message':WebSocket的接收到服务器消息事件。 |
| callback | AsyncCallback\<string \|ArrayBuffer <sup>8+</sup>\> | 否 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|-------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......@@ -605,12 +582,6 @@ off\(type: 'close', callback?: AsyncCallback<\{ code: number, reason: string \}\
| type | string | 是 | 'close':WebSocket的关闭事件。 |
| callback | AsyncCallback<{ code: number, reason: string }> | 否 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|-------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......@@ -634,12 +605,6 @@ on\(type: 'error', callback: ErrorCallback\): void
| type | string | 是 | 'error':WebSocket的Error事件。 |
| callback | ErrorCallback | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|-------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......@@ -668,12 +633,6 @@ off\(type: 'error', callback?: ErrorCallback\): void
| type | string | 是 | 'error':WebSocket的Error事件。 |
| callback | ErrorCallback | 否 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|--------|---------------------------|
| 401 | Parameter error. |
**示例:**
```js
......
......@@ -54,11 +54,11 @@ No such process.
排查进程信息。
## 2300004 Interrupted system call
## 2301004 系统调用中断
**错误信息**
Couldn't resolve host name.
Interrupted system call.
**错误描述**
......
......@@ -72,7 +72,7 @@ foundation/communication/
1. 从@ohos.net.sharing中导入sharing命名空间。
2. 设定共享类型
3. 开始共享
4. 止共享
4. 止共享
```
// 引入包名
import sharing from '@ohos.net.sharing';
......@@ -92,26 +92,28 @@ sharing.stopSharing(this.sharingType,(err)=>{
1. 从@ohos.net.http.d.ts中导入http命名空间。
2. 调用createHttp()方法,创建一个HttpRequest对象。
3. 调用该对象的on()方法,订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息。
3. 调用该对象的on()方法,订阅http响应头事件,此接口会比request请求先返回。可以根据业务需要订阅此消息。
4. 调用该对象的request()方法,传入http请求的url地址和可选参数,发起网络请求。
5. 按照实际业务需要,解析返回结果。
6. 当该请求使用完毕时,调用destroy()方法主动销毁。
6. 调用该对象的off()方法,取消订阅http响应头事件。
7. 当该请求使用完毕时,调用destroy()方法主动销毁。
```
// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个http请求任务,不可复用
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
httpRequest.on('headersReceive', (data) => {
console.info('header: ' + data.header);
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
httpRequest.request(
// 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"EXAMPLE_URL",
{
method: 'POST', // 可选,默认为“GET”
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
......@@ -120,21 +122,28 @@ httpRequest.request(
extraData: {
"data": "data to send",
},
connectTimeout: 60000, // 可选,默认为60000,即60s
readTimeout: 60000, // 可选,默认为60000,即60s
},(err, data) => {
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
usingCache: true, // 可选,默认为true
priority: 1, // 可选,默认为1
connectTimeout: 60000, // 可选,默认为60000ms
readTimeout: 60000, // 可选,默认为60000ms
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
}, (err, data) => {
if (!err) {
// data.result为http响应内容,可根据业务需要进行解析
console.info('Result:' + data.result);
console.info('code:' + data.responseCode);
// data.header为http响应头,可根据业务需要进行解析
console.info('header:' + data.header);
console.info('header:' + data.cookies);
// data.result为HTTP响应内容,可根据业务需要进行解析
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
// data.header为HTTP响应头,可根据业务需要进行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
} else {
console.info('error:' + err);
console.info('error:' + JSON.stringify(err));
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
// 当该请求使用完毕时,调用destroy()方法主动销毁。
httpRequest.destroy();
}
);
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册