diff --git a/zh-cn/application-dev/connectivity/http-request.md b/zh-cn/application-dev/connectivity/http-request.md index e56553118ca9f6ac114d0e0b75183c222bce422f..ff6ec5ed4113c8c9c14cb6a1613dd86dab7ff232 100644 --- a/zh-cn/application-dev/connectivity/http-request.md +++ b/zh-cn/application-dev/connectivity/http-request.md @@ -18,32 +18,41 @@ HTTP数据请求功能主要由http模块提供。 | ----------------------------------------- | ----------------------------------- | | createHttp() | 创建一个http请求。 | | request() | 根据URL地址,发起HTTP网络请求。 | +| request2()10+ | 根据URL地址,发起HTTP网络请求并返回流式响应| | destroy() | 中断请求任务。 | | on(type: 'headersReceive') | 订阅HTTP Response Header 事件。 | | off(type: 'headersReceive') | 取消订阅HTTP Response Header 事件。 | +| once\('headersReceive'\)8+ | 订阅HTTP Response Header 事件,但是只触发一次。| +| on\('dataReceive'\)10+ | 订阅HTTP流式响应数据接收事件。 | +| off\('dataReceive'\)10+ | 取消订阅HTTP流式响应数据接收事件。 | +| on\('dataEnd'\)10+ | 订阅HTTP流式响应数据接收完毕事件。 | +| off\('dataEnd'\)10+ | 取消订阅HTTP流式响应数据接收完毕事件。 | +| on\('dataProgress'\)10+ | 订阅HTTP流式响应数据接收进度事件。 | +| off\('dataProgress'\)10+ | 取消订阅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(); } } diff --git a/zh-cn/application-dev/reference/apis/js-apis-http.md b/zh-cn/application-dev/reference/apis/js-apis-http.md index 07bf05fe19ae96577c5b59f9ef73099c8ef5abf9..1cdf224165953ca89bc5d02298ff045a4f45b23c 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-http.md +++ b/zh-cn/application-dev/reference/apis/js-apis-http.md @@ -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\):void +request\(url: string, options: HttpRequestOptions, callback: AsyncCallback\\):void 根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。 @@ -219,7 +223,7 @@ httpRequest.request("EXAMPLE_URL", ### request -request\(url: string, options? : HttpRequestOptions\): Promise +request\(url: string, options? : HttpRequestOptions\): Promise\ 根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。 @@ -317,6 +321,209 @@ destroy\(\): void httpRequest.destroy(); ``` +### request210+ + +request2(url: string, callback: AsyncCallback): void + +根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.INTERNET + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | +| url | string | 是 | 发起网络请求的URL地址。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**错误码:** + +| 错误码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)); + } +}) +``` + +### request210+ + +request2(url: string, options: HttpRequestOptions, callback: AsyncCallback): void + +根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.INTERNET + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | +| url | string | 是 | 发起网络请求的URL地址。 | +| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**错误码:** + +| 错误码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)); + } +}) +``` +### request210+ + +request2\(url: string, options? : HttpRequestOptions\): Promise\ + +根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。 + +**需要权限**:ohos.permission.INTERNET + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------ | ---- | ----------------------------------------------- | +| url | string | 是 | 发起网络请求的URL地址。 | +| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | + +**返回值:** + +| 类型 | 说明 | +| :------------------------------------- | :-------------------------------- | +| Promise\ | 以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\): void @@ -443,7 +650,148 @@ httpRequest.once('headersReceive', (header) => { console.info('header: ' + JSON.stringify(header)); }); ``` +### on\('dataReceive'\)10+ + +on\(type: 'dataReceive', callback: Callback\\): void + +订阅HTTP流式响应数据接收事件。 + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | --------------------------------- | +| type | string | 是 | 订阅的事件类型,'dataReceive'。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```js +httpRequest.on('dataReceive', (data) => { + console.info('dataReceive length: ' + JSON.stringify(data.byteLength)); +}); +``` + +### off\('dataReceive'\)10+ + +off\(type: 'dataReceive', callback?: Callback\\): void + +取消订阅HTTP流式响应数据接收事件。 + +>![](public_sys-resources/icon-note.gif) **说明:** +>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------ | ---- | -------------------------------------- | +| type | string | 是 | 取消订阅的事件类型:'dataReceive'。 | +| callback | Callback\ | 否 | 回调函数。 | + +**示例:** + +```js +httpRequest.off('dataReceive'); +``` + +### on\('dataEnd'\)10+ + +on\(type: 'dataEnd', callback: Callback\\): void + +订阅HTTP流式响应数据接收完毕事件。 + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | --------------------------------- | +| type | string | 是 | 订阅的事件类型,'dataEnd'。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```js +httpRequest.on('dataReceive', () => { + console.info('Receive dataEnd!'); +}); +``` + +### off\('dataEnd'\)10+ + +off(type: 'dataEnd', callback?: Callback\): void + +取消订阅HTTP流式响应数据接收完毕事件。 + +>![](public_sys-resources/icon-note.gif) **说明:** +>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 + +**系统能力**:SystemCapability.Communication.NetStack + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------ | ---- | -------------------------------------- | +| type | string | 是 | 取消订阅的事件类型:'dataEnd'。 | +| callback | Callback\ | 否 | 回调函数。 | + +**示例:** + +```js +httpRequest.off('dataEnd'); +``` + +### on\('dataProgress'\)10+ + + 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'\)10+ + +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 发起请求可选参数的类型和取值范围。 @@ -454,13 +802,14 @@ httpRequest.once('headersReceive', (header) => { | -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | | method | [RequestMethod](#requestmethod) | 否 | 请求方式。 | | extraData | string \| Object \| ArrayBuffer6+ | 否 | 发送请求的额外数据。
- 当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content。
- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求的参数补充,参数内容会拼接到URL中进行发送。6+
- 开发者传入string对象,开发者需要自行编码,将编码后的string传入。6+ | -| expectDataType9+ | [HttpDataType](#httpdatatype9) | 否 | 指定返回数据的类型。如果设置了此参数,系统将优先返回指定的类型。 | +| expectDataType9+ | [HttpDataType](#httpdatatype9) | 否 | 指定返回数据的类型。如果设置了此参数,系统将优先返回指定的类型。 | | usingCache9+ | boolean | 否 | 是否使用缓存,默认为true。 | | priority9+ | number | 否 | 优先级,范围\[1,1000],默认是1。 | -| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 | -| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。 | -| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。 | -| usingProtocol9+ | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。 | +| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 | +| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。 | +| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。 | +| usingProtocol9+ | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。 | +| usingProxy10+ | boolean \| Object | 否 | 是否使用HTTP代理,默认为false,不使用代理。
- 当usingProxy为布尔类型true时,使用默认网络代理。
- 当usingProxy为object类型时,使用指定网络代理。 | ## RequestMethod @@ -587,10 +936,10 @@ flush(callback: AsyncCallback\): 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\ ```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 ```js httpResponseCache.delete(err => { if (err) { - console.log('delete fail'); + console.info('delete fail'); return; } - console.log('delete success'); + console.info('delete success'); }); ``` ### delete9+ @@ -661,9 +1010,9 @@ delete(): Promise\ ```js httpResponseCache.delete().then(() => { - console.log('delete success'); + console.info('delete success'); }).catch(err => { - console.log('delete fail'); + console.info('delete fail'); }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-connection.md b/zh-cn/application-dev/reference/apis/js-apis-net-connection.md index 3df21e05a765610e5c22aab750c7c66679a699f8..cfd1405bc9385985c34cee0500f6a82aa9e7cf98 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-net-connection.md +++ b/zh-cn/application-dev/reference/apis/js-apis-net-connection.md @@ -146,6 +146,8 @@ getGlobalHttpProxy(callback: AsyncCallback\): void 获取网络的全局代理配置信息,使用callback方式作为异步方法。 +**系统接口**:此接口为系统接口。 + **系统能力**:SystemCapability.Communication.NetManager.Core **参数:** @@ -176,6 +178,8 @@ getGlobalHttpProxy(): Promise\; 获取网络的全局代理配置信息,使用Promise方式作为异步方法。 +**系统接口**:此接口为系统接口。 + **系统能力**:SystemCapability.Communication.NetManager.Core **返回值:** @@ -207,6 +211,8 @@ setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\): void 设置网络全局Http代理配置信息,使用callback方式作为异步方法。 +**系统接口**:此接口为系统接口。 + **需要权限**:ohos.permission.CONNECTIVITY_INTERNAL **系统能力**:SystemCapability.Communication.NetManager.Core @@ -231,12 +237,12 @@ setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\): 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\; 设置网络全局Http代理配置信息,使用Promise方式作为异步方法。 +**系统接口**:此接口为系统接口。 + **需要权限**:ohos.permission.CONNECTIVITY_INTERNAL **系统能力**:SystemCapability.Communication.NetManager.Core @@ -279,12 +287,12 @@ setGlobalHttpProxy(httpProxy: HttpProxy): Promise\; **示例:** ```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\): void **示例:** ```js -connection.getAppNet(function(error, data)) { +connection.getAppNet(function(error, data) { console.log(JSON.stringify(error)) console.log(JSON.stringify(data)) -} +}) ``` ## connection.getAppNet9+ @@ -799,6 +807,8 @@ enableAirplaneMode(callback: AsyncCallback\): void **系统接口**:此接口为系统接口。 +**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL + **系统能力**:SystemCapability.Communication.NetManager.Core **参数:** @@ -830,6 +840,8 @@ enableAirplaneMode(): Promise\ **系统接口**:此接口为系统接口。 +**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL + **系统能力**:SystemCapability.Communication.NetManager.Core **返回值:** @@ -861,6 +873,8 @@ disableAirplaneMode(callback: AsyncCallback\): void **系统接口**:此接口为系统接口。 +**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL + **系统能力**:SystemCapability.Communication.NetManager.Core **参数:** @@ -892,6 +906,8 @@ disableAirplaneMode(): Promise\ **系统接口**:此接口为系统接口。 +**需要权限**: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 +## HttpProxy10+ 网络全局代理配置信息 @@ -1817,7 +1833,7 @@ connection.getDefaultNet().then(function (netHandle) { | ------ | ------ | --- |------------------------- | | host | string | 否 | 代理服务器主机名。 | | port | number | 否 | 主机端口。 | -| parsedExclusionList | Array | 否 | 不使用代理服务器的屏蔽列表。 | +| exclusionList | Array | 否 | 不使用代理服务器的屏蔽列表。 | ## NetSpecifier diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md b/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md index 3a80d9cf90063e5b6ef98e24c78bffa6c8e74d2e..823f3f1aa66f05d8d518deeb7c6d43fcba681282 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md +++ b/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md @@ -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\): ```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\ ```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\): 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\ ```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\>): 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\> ```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)); }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-policy.md b/zh-cn/application-dev/reference/apis/js-apis-net-policy.md index 0d2eef9ee6be3b6fd5d807b954367ba4fed1ba88..a628c3346a7f923f1c170b340d922fa2336882bc 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-net-policy.md +++ b/zh-cn/application-dev/reference/apis/js-apis-net-policy.md @@ -84,7 +84,7 @@ setBackgroundPolicy(isAllowed: boolean): Promise\ **示例:** ```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\; **示例:** ```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\; 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\; **示例:** ```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\>; **示例:** ```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\>; **示例:** ```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\; ```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\; 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\; 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\; 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\>; **示例:** ```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\; ```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\; **示例:** ```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\; 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\>; **示例:** ```js -policy.getPowerSaveAllowList().then((error, data) { +policy.getPowerSaveAllowList().then(function(error, data) { console.log(JSON.stringify(error)) console.log(JSON.stringify(data)) }) diff --git a/zh-cn/application-dev/reference/apis/js-apis-system-fetch.md b/zh-cn/application-dev/reference/apis/js-apis-system-fetch.md index f6c7b4b5eb360d1bf4cf18c3ddd37450445be811..ed8c646e3d6082c779bd9e02b726cc4c634dbac0 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-system-fetch.md +++ b/zh-cn/application-dev/reference/apis/js-apis-system-fetch.md @@ -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关系 diff --git a/zh-cn/application-dev/reference/apis/js-apis-system-network.md b/zh-cn/application-dev/reference/apis/js-apis-system-network.md index 7ac1f1ea18ba295deea28cfa49a3cd49018d6b7b..dafd8dfb0cc77456536366353b7b3a3a01e9851b 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-system-network.md +++ b/zh-cn/application-dev/reference/apis/js-apis-system-network.md @@ -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 diff --git a/zh-cn/application-dev/reference/apis/js-apis-webSocket.md b/zh-cn/application-dev/reference/apis/js-apis-webSocket.md index 032b6e8440d5b2483fd30241499a12235082a998..bb4cff3e2293e3d89c9ec53f1367f41d51b18104 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-webSocket.md +++ b/zh-cn/application-dev/reference/apis/js-apis-webSocket.md @@ -442,11 +442,6 @@ on\(type: 'open', callback: AsyncCallback\): void | type | string | 是 | 'open':WebSocket的打开事件。 | | callback | AsyncCallback\ | 是 | 回调函数。 | -**错误码:** - -| 错误码ID | 错误信息 | -|-------|---------------------------| -| 401 | Parameter error. | **示例:** @@ -476,12 +471,6 @@ off\(type: 'open', callback?: AsyncCallback\): void | type | string | 是 | 'open':WebSocket的打开事件。 | | callback | AsyncCallback\ | 否 | 回调函数。 | -**错误码:** - -| 错误码ID | 错误信息 | -|-------|---------------------------| -| 401 | Parameter error. | - **示例:** ```js @@ -513,12 +502,6 @@ on\(type: 'message', callback: AsyncCallback\): void | type | string | 是 | 'message':WebSocket的接收到服务器消息事件。 | | callback | AsyncCallback\8+\> | 是 | 回调函数。 | -**错误码:** - -| 错误码ID | 错误信息 | -|--------|---------------------------| -| 401 | Parameter error. | - **示例:** ```js @@ -548,12 +531,6 @@ off\(type: 'message', callback?: AsyncCallback\): void | type | string | 是 | 'message':WebSocket的接收到服务器消息事件。 | | callback | AsyncCallback\8+\> | 否 | 回调函数。 | -**错误码:** - -| 错误码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 diff --git a/zh-cn/application-dev/reference/errorcodes/errorcode-net-socket.md b/zh-cn/application-dev/reference/errorcodes/errorcode-net-socket.md index 45366518689a31beb9c8e6e0deb7d8e61342378f..f0bb305a18f0dcab466b678a3bf3c2c8e66f0031 100644 --- a/zh-cn/application-dev/reference/errorcodes/errorcode-net-socket.md +++ b/zh-cn/application-dev/reference/errorcodes/errorcode-net-socket.md @@ -54,11 +54,11 @@ No such process. 排查进程信息。 -## 2300004 Interrupted system call +## 2301004 系统调用中断 **错误信息** -Couldn't resolve host name. +Interrupted system call. **错误描述** diff --git "a/zh-cn/readme/\347\275\221\347\273\234\347\256\241\347\220\206\345\255\220\347\263\273\347\273\237.md" "b/zh-cn/readme/\347\275\221\347\273\234\347\256\241\347\220\206\345\255\220\347\263\273\347\273\237.md" index 5a138c74e854c22b2dd9589b4903f830b9de762c..a1ceefd6cd9ece20e944100169737d28b6413e78 100644 --- "a/zh-cn/readme/\347\275\221\347\273\234\347\256\241\347\220\206\345\255\220\347\263\273\347\273\237.md" +++ "b/zh-cn/readme/\347\275\221\347\273\234\347\256\241\347\220\206\345\255\220\347\263\273\347\273\237.md" @@ -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(); } ); ```