提交 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();
}
}
......
......@@ -16,6 +16,7 @@ import http from '@ohos.net.http';
## 完整示例
```js
// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个HTTP请求任务,不可复用
......@@ -44,16 +45,19 @@ httpRequest.request(
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);
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));
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
......@@ -136,7 +140,7 @@ httpRequest.request("EXAMPLE_URL", (err, data) => {
### request
request\(url: string, options: HttpRequestOptions, callback: AsyncCallback<HttpResponse\>\):void
request\(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>\):void
根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。
......@@ -219,7 +223,7 @@ httpRequest.request("EXAMPLE_URL",
### request
request\(url: string, options? : HttpRequestOptions\): Promise<HttpResponse\>
request\(url: string, options? : HttpRequestOptions\): Promise\<HttpResponse\>
根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。
......@@ -317,6 +321,209 @@ destroy\(\): void
httpRequest.destroy();
```
### request2<sup>10+</sup>
request2(url: string, callback: AsyncCallback<void>): void
根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
| url | string | 是 | 发起网络请求的URL地址。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|---------|-------------------------------------------------------|
| 401 | Parameter error. |
| 201 | Permission denied. |
| 2300003 | URL using bad/illegal format or missing URL. |
| 2300007 | Couldn't connect to server. |
| 2300028 | Timeout was reached. |
| 2300052 | Server returned nothing (no headers, no data). |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
**示例:**
```js
httpRequest.request2("EXAMPLE_URL", (err) => {
if (!err) {
console.info(request2 OK!);
} else {
console.info("request2 ERROR : err = " + JSON.stringify(err));
}
})
```
### request2<sup>10+</sup>
request2(url: string, options: HttpRequestOptions, callback: AsyncCallback<void>): void
根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
| url | string | 是 | 发起网络请求的URL地址。 |
| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
|---------|-------------------------------------------------------|
| 401 | Parameter error. |
| 201 | Permission denied. |
| 2300001 | Unsupported protocol. |
| 2300003 | URL using bad/illegal format or missing URL. |
| 2300005 | Couldn't resolve proxy name. |
| 2300006 | Couldn't resolve host name. |
| 2300007 | Couldn't connect to server. |
| 2300008 | Weird server reply. |
| 2300009 | Access denied to remote resource. |
| 2300016 | Error in the HTTP2 framing layer. |
| 2300018 | Transferred a partial file. |
| 2300023 | Failed writing received data to disk/application. |
| 2300025 | Upload failed. |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory. |
| 2300028 | Timeout was reached. |
| 2300047 | Number of redirects hit maximum amount. |
| 2300052 | Server returned nothing (no headers, no data). |
| 2300055 | Failed sending data to the peer. |
| 2300056 | Failure when receiving data from the peer. |
| 2300058 | Problem with the local SSL certificate. |
| 2300059 | Couldn't use specified SSL cipher. |
| 2300060 | SSL peer certificate or SSH remote key was not OK. |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded. |
| 2300070 | Disk full or allocation exceeded. |
| 2300073 | Remote file already exists. |
| 2300077 | Problem with the SSL CA cert (path? access rights?). |
| 2300078 | Remote file not found. |
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
**示例:**
```js
httpRequest.request2("EXAMPLE_URL",
{
method: http.RequestMethod.GET,
header: {
'Content-Type': 'application/json'
},
readTimeout: 60000,
connectTimeout: 60000
}, (err) => {
if (!err) {
console.info(request2 OK!);
} else {
console.info("request2 ERROR : err = " + JSON.stringify(err));
}
})
```
### request2<sup>10+</sup>
request2\(url: string, options? : HttpRequestOptions\): Promise\<void\>
根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.INTERNET
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------ | ---- | ----------------------------------------------- |
| url | string | 是 | 发起网络请求的URL地址。 |
| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 |
**返回值:**
| 类型 | 说明 |
| :------------------------------------- | :-------------------------------- |
| Promise\<void\> | 以Promise形式返回发起请求的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
|---------|-------------------------------------------------------|
| 401 | Parameter error. |
| 201 | Permission denied. |
| 2300001 | Unsupported protocol. |
| 2300003 | URL using bad/illegal format or missing URL. |
| 2300005 | Couldn't resolve proxy name. |
| 2300006 | Couldn't resolve host name. |
| 2300007 | Couldn't connect to server. |
| 2300008 | Weird server reply. |
| 2300009 | Access denied to remote resource. |
| 2300016 | Error in the HTTP2 framing layer. |
| 2300018 | Transferred a partial file. |
| 2300023 | Failed writing received data to disk/application. |
| 2300025 | Upload failed. |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory. |
| 2300028 | Timeout was reached. |
| 2300047 | Number of redirects hit maximum amount. |
| 2300052 | Server returned nothing (no headers, no data). |
| 2300055 | Failed sending data to the peer. |
| 2300056 | Failure when receiving data from the peer. |
| 2300058 | Problem with the local SSL certificate. |
| 2300059 | Couldn't use specified SSL cipher. |
| 2300060 | SSL peer certificate or SSH remote key was not OK. |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded. |
| 2300070 | Disk full or allocation exceeded. |
| 2300073 | Remote file already exists. |
| 2300077 | Problem with the SSL CA cert (path? access rights?). |
| 2300078 | Remote file not found. |
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:
**示例:**
```js
let promise = httpRequest.request("EXAMPLE_URL", {
method: http.RequestMethod.GET,
connectTimeout: 60000,
readTimeout: 60000,
header: {
'Content-Type': 'application/json'
}
});
promise.then(() => {
console.info(request2 OK!);
}).catch((err) => {
console.info("request2 ERROR : err = " + JSON.stringify(err));
});
```
### on\('headerReceive'\)
on\(type: 'headerReceive', callback: AsyncCallback<Object\>\): void
......@@ -443,7 +650,148 @@ httpRequest.once('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
```
### on\('dataReceive'\)<sup>10+</sup>
on\(type: 'dataReceive', callback: Callback\<ArrayBuffer\>\): void
订阅HTTP流式响应数据接收事件。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | --------------------------------- |
| type | string | 是 | 订阅的事件类型,'dataReceive'。 |
| callback | AsyncCallback\<ArrayBuffer\> | 是 | 回调函数。 |
**示例:**
```js
httpRequest.on('dataReceive', (data) => {
console.info('dataReceive length: ' + JSON.stringify(data.byteLength));
});
```
### off\('dataReceive'\)<sup>10+</sup>
off\(type: 'dataReceive', callback?: Callback\<ArrayBuffer\>\): void
取消订阅HTTP流式响应数据接收事件。
>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------ | ---- | -------------------------------------- |
| type | string | 是 | 取消订阅的事件类型:'dataReceive'。 |
| callback | Callback\<ArrayBuffer\> | 否 | 回调函数。 |
**示例:**
```js
httpRequest.off('dataReceive');
```
### on\('dataEnd'\)<sup>10+</sup>
on\(type: 'dataEnd', callback: Callback\<void\>\): void
订阅HTTP流式响应数据接收完毕事件。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | --------------------------------- |
| type | string | 是 | 订阅的事件类型,'dataEnd'。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**示例:**
```js
httpRequest.on('dataReceive', () => {
console.info('Receive dataEnd!');
});
```
### off\('dataEnd'\)<sup>10+</sup>
off(type: 'dataEnd', callback?: Callback\<void\>): void
取消订阅HTTP流式响应数据接收完毕事件。
>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------ | ---- | -------------------------------------- |
| type | string | 是 | 取消订阅的事件类型:'dataEnd'。 |
| callback | Callback\<void\> | 否 | 回调函数。 |
**示例:**
```js
httpRequest.off('dataEnd');
```
### on\('dataProgress'\)<sup>10+</sup>
on\(type: 'dataProgress', callback: Callback\<{ receiveSize: number, totalSize: number }\>\): void
订阅HTTP流式响应数据接收进度事件。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | --------------------------------- |
| type | string | 是 | 订阅的事件类型,'dataProgress'。 |
| callback | AsyncCallback\<{ receiveSize: number, totalSize: number }\> | 是 | 回调函数。 |
**示例:**
```js
httpRequest.on('dataProgress', (data) => {
if (!err) {
console.info('dataProgress:' + JSON.stringify(data));
}
});
```
### off\('dataProgress'\)<sup>10+</sup>
off(type: 'dataProgress', callback?: Callback\<{ receiveSize: number, totalSize: number }\>): void
取消订阅HTTP流式响应数据接收进度事件。
>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**:SystemCapability.Communication.NetStack
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------ | ---- | -------------------------------------- |
| type | string | 是 | 取消订阅的事件类型:'dataProgress'。 |
| callback | Callback\<{ receiveSize: number, totalSize: number }\> | 否 | 回调函数。 |
**示例:**
```js
httpRequest.off('dataProgress');
```
## HttpRequestOptions
发起请求可选参数的类型和取值范围。
......@@ -461,6 +809,7 @@ httpRequest.once('headersReceive', (header) => {
| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。 |
| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。 |
| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。 |
| usingProxy<sup>10+</sup> | boolean \| Object | 否 | 是否使用HTTP代理,默认为false,不使用代理。<br />- 当usingProxy为布尔类型true时,使用默认网络代理。<br />- 当usingProxy为object类型时,使用指定网络代理。 |
## RequestMethod
......@@ -587,10 +936,10 @@ flush(callback: AsyncCallback\<void>): void
```js
httpResponseCache.flush(err => {
if (err) {
console.log('flush fail');
console.info('flush fail');
return;
}
console.log('flush success');
console.info('flush success');
});
```
......@@ -612,9 +961,9 @@ flush(): Promise\<void>
```js
httpResponseCache.flush().then(() => {
console.log('flush success');
console.info('flush success');
}).catch(err => {
console.log('flush fail');
console.info('flush fail');
});
```
......@@ -637,10 +986,10 @@ delete(callback: AsyncCallback\<void>): void
```js
httpResponseCache.delete(err => {
if (err) {
console.log('delete fail');
console.info('delete fail');
return;
}
console.log('delete success');
console.info('delete success');
});
```
### delete<sup>9+</sup>
......@@ -661,9 +1010,9 @@ delete(): Promise\<void>
```js
httpResponseCache.delete().then(() => {
console.log('delete success');
console.info('delete success');
}).catch(err => {
console.log('delete fail');
console.info('delete fail');
});
```
......
......@@ -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关系
......
......@@ -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,22 +122,29 @@ 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);
}
// 当该请求使用完毕时,调用destroy()方法主动销毁。
console.info('error:' + JSON.stringify(err));
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
}
);
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册