js-apis-net-connection.md 65.2 KB
Newer Older
1
# @ohos.net.connection (网络连接管理)
2

C
clevercong 已提交
3
网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。
C
clevercong 已提交
4

5
> **说明:**
Z
zengyawen 已提交
6
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
7 8 9

## 导入模块

Z
zengyawen 已提交
10
```js
11 12
import connection from '@ohos.net.connection'
```
Z
zhanghaifeng 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## connection.createNetConnection

createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection

返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征,timeout是超时时间(单位是毫秒),netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名       | 类型                          | 必填 | 说明                                                         |
| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
| netSpecifier | [NetSpecifier](#netspecifier) | 否   | 指定网络的各项特征,不指定则关注默认网络。                   |
| timeout      | number                        | 否   | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效。 |

**返回值:**

| 类型                            | 说明                 |
| ------------------------------- | -------------------- |
| [NetConnection](#netconnection) | 所关注的网络的句柄。 |

**示例:**

```js
// 关注默认网络
let netConnection = connection.createNetConnection()

// 关注蜂窝网络
let netConnectionCellular = connection.createNetConnection({
    netCapabilities: {
        bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
    }
})
```
47 48 49 50 51

## connection.getDefaultNet

getDefaultNet(callback: AsyncCallback\<NetHandle>): void

M
maosiping 已提交
52
获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
53 54 55 56 57 58 59

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
60 61
| 参数名   | 类型                                    | 必填 | 说明       |
| -------- | --------------------------------------- | ---- | ---------- |
Z
zhanghaifeng 已提交
62 63 64 65 66 67 68 69 70
| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是   | 回调函数。当成功获取默认激活的数据网络时,err为undefined,data为默认激活的数据网络;否则为错误对象 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
71 72 73

**示例:**

Z
zengyawen 已提交
74
```js
Z
zhanghaifeng 已提交
75
connection.getDefaultNet(function (error, data) {
76
    console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
77
    console.log(JSON.stringify(data))
78 79 80 81 82 83 84
})
```

## connection.getDefaultNet

getDefaultNet(): Promise\<NetHandle>

M
maosiping 已提交
85
获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
86 87 88 89 90 91 92

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zengyawen 已提交
93 94 95
| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 |
96

Z
zhanghaifeng 已提交
97 98 99 100 101 102 103 104
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

105 106
**示例:**

Z
zengyawen 已提交
107
```js
Z
zhanghaifeng 已提交
108 109
connection.getDefaultNet().then(function (data) {
    console.log(JSON.stringify(data))
110 111 112
})
```

Z
zengyawen 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
## connection.getDefaultNetSync<sup>9+</sup>

getDefaultNetSync(): NetHandle

使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型      | 说明                               |
| --------- | ---------------------------------- |
| NetHandle | 以同步方式返回默认激活的数据网络。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
let netHandle = connection.getDefaultNetSync();
```

Z
zhanghaifeng 已提交
143
## connection.getGlobalHttpProxy<sup>10+</sup>
F
fuchao 已提交
144

Z
zhanghaifeng 已提交
145
getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
F
fuchao 已提交
146

Z
zhanghaifeng 已提交
147
获取网络的全局代理配置信息,使用callback方式作为异步方法。
F
fuchao 已提交
148

Y
Yangys 已提交
149 150
**系统接口**:此接口为系统接口。

Z
zhanghaifeng 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[HttpProxy](#httpproxy)> | 是   | 回调函数。当成功获取网络的全局代理配置信息时,err为undefined,data为网络的全局代理配置信息;否则为错误对象|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.getGlobalHttpProxy((error,data) => {
    console.info(JSON.stringify(error));
    console.info(JSON.stringify(data));
})
```

## connection.getGlobalHttpProxy<sup>10+</sup>

getGlobalHttpProxy(): Promise\<HttpProxy>;

获取网络的全局代理配置信息,使用Promise方式作为异步方法。
F
fuchao 已提交
180

Y
Yangys 已提交
181 182
**系统接口**:此接口为系统接口。

F
fuchao 已提交
183 184 185 186
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zhanghaifeng 已提交
187 188 189 190 191 192 193 194 195 196
| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
| Promise\<[HttpProxy](#httpproxy)> | 以Promise形式返回网络的全局代理配置信息。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
F
fuchao 已提交
197 198 199 200

**示例:**

```js
Z
zhanghaifeng 已提交
201 202 203 204 205
connection.getGlobalHttpProxy().then((data) => {
   console.info(JSON.stringify(data));
}).catch(error => {
   console.info(JSON.stringify(error));
})
F
fuchao 已提交
206 207
```

Z
zhanghaifeng 已提交
208
## connection.setGlobalHttpProxy<sup>10+</sup>
209

Z
zhanghaifeng 已提交
210
setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
211

Z
zhanghaifeng 已提交
212
设置网络全局Http代理配置信息,使用callback方式作为异步方法。
213

Y
Yangys 已提交
214 215
**系统接口**:此接口为系统接口。

Z
zhanghaifeng 已提交
216
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
F
fuchao 已提交
217

218 219 220 221
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zhanghaifeng 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235
| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | 是   | 网络全局Http代理配置信息 |
| callback  | AsyncCallback\<void> | 是   | 回调函数。当成功设置网络全局Http代理配置信息时,err为undefined,否则为错误对象|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
236 237 238

**示例:**

Z
zengyawen 已提交
239
```js
Y
Yangys 已提交
240 241
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
Z
zhanghaifeng 已提交
242
let httpProxy = {
Y
Yangys 已提交
243 244 245
    host: "192.168.xx.xxx",
    port: 8080,
    exclusionList: exclusionArray
Z
zhanghaifeng 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258
}
connection.setGlobalHttpProxy(httpProxy, (error, data) => {
   console.info(JSON.stringify(error));
   console.info(JSON.stringify(data));
});
```

## connection.setGlobalHttpProxy<sup>10+</sup>

setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;

设置网络全局Http代理配置信息,使用Promise方式作为异步方法。

Y
Yangys 已提交
259 260
**系统接口**:此接口为系统接口。

Z
zhanghaifeng 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| httpProxy | [HttpProxy](#httpproxy)                                      | 是   | 网络全局Http代理配置信息。 |

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 无返回值的Promise对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
Y
Yangys 已提交
290 291
let exclusionStr="192.168,baidu.com"
let exclusionArray = exclusionStr.split(',');
Z
zhanghaifeng 已提交
292
let httpProxy = {
Y
Yangys 已提交
293 294 295
    host: "192.168.xx.xxx",
    port: 8080,
    exclusionList: exclusionArray
Z
zhanghaifeng 已提交
296 297 298 299 300
}
connection.setGlobalHttpProxy(httpProxy).then((error, data) => {
   console.info(JSON.stringify(data));
}).catch(error=>{
   console.info(JSON.stringify(error));
301 302 303
})
```

Z
zhanghaifeng 已提交
304
## connection.getAppNet<sup>9+</sup>
305

Z
zhanghaifeng 已提交
306
getAppNet(callback: AsyncCallback\<NetHandle>): void
307

Z
zhanghaifeng 已提交
308
获取App绑定的网络信息,使用callback方式作为异步方法。
309

Z
zhanghaifeng 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| callback  | AsyncCallback\<[NetHandle](#nethandle)> | 是   | 回调函数。当成功获取App绑定的网络信息时,err为undefined,data为获取到App绑定的网络信息;否则为错误对象|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
Y
Yangys 已提交
328
connection.getAppNet(function(error, data) {
Z
zhanghaifeng 已提交
329 330
   console.log(JSON.stringify(error))
   console.log(JSON.stringify(data))
Y
Yangys 已提交
331
})
Z
zhanghaifeng 已提交
332 333 334 335 336 337 338
```

## connection.getAppNet<sup>9+</sup>

getAppNet(): Promise\<NetHandle>;

获取App绑定的网络信息,使用Promise方式作为异步方法。
F
fuchao 已提交
339

340 341 342 343
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

Z
zhanghaifeng 已提交
344 345 346 347 348 349 350 351 352 353
| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回App绑定的网络信息。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
354 355 356

**示例:**

Z
zengyawen 已提交
357
```js
Z
zhanghaifeng 已提交
358 359 360 361
connection.getAppNet().then((data) => {
   console.info(JSON.stringify(data));
}).catch(error => {
   console.info(JSON.stringify(error));
362 363 364
})
```

Z
zhanghaifeng 已提交
365 366
## connection.SetAppNet<sup>9+</sup>

Z
zhanghaifeng 已提交
367
setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void
Z
zhanghaifeng 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用callback方式作为异步方法。

**需要权限**:ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |
| callback  | AsyncCallback\<void> | 是   | 回调函数。当成功绑定App到指定网络时,err为undefined,否则为错误对象|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.getDefaultNet(function (error, netHandle) {
   connection.setAppNet(netHandle, (error, data) => {
       console.log(JSON.stringify(error))
       console.log(JSON.stringify(data))
   });
})
```

## connection.SetAppNet<sup>9+</sup>

setAppNet(netHandle: NetHandle): Promise\<void>;

绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用Promise方式作为异步方法。

**需要权限**:ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 无返回值的Promise对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.getDefaultNet().then(function (netHandle) {
   connection.setAppNet(netHandle).then((error, data) => {
        console.log(JSON.stringify(data))
   }).catch(error => {
        console.log(JSON.stringify(error))
   })
})
```

C
clevercong 已提交
447 448 449 450
## connection.getAllNets

getAllNets(callback: AsyncCallback&lt;Array&lt;NetHandle&gt;&gt;): void

451
获取所有处于连接状态的网络列表,使用callback方式作为异步方法。
C
clevercong 已提交
452

C
clevercong 已提交
453 454 455
**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
456 457

**参数:**
458

C
clevercong 已提交
459 460
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Z
zhanghaifeng 已提交
461 462 463 464 465 466 467 468 469
| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 是 | 回调函数。当成功获取所有处于连接状态的网络列表时,err为undefined,data为激活的数据网络列表;否则为错误对象 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
C
clevercong 已提交
470 471 472

**示例:**

Z
zengyawen 已提交
473
```js
Z
zhanghaifeng 已提交
474
connection.getAllNets(function (error, data) {
C
clevercong 已提交
475
    console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
476
    console.log(JSON.stringify(data))
C
clevercong 已提交
477 478 479 480 481 482 483
});
```

## connection.getAllNets

getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;

484
获取所有处于连接状态的网络列表,使用Promise方式作为异步方法。
C
clevercong 已提交
485

C
clevercong 已提交
486 487 488
**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
489 490

**返回值:**
491

C
clevercong 已提交
492 493 494 495
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 以Promise形式返回激活的数据网络列表。 |

Z
zhanghaifeng 已提交
496 497 498 499 500 501 502 503
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

C
clevercong 已提交
504 505
**示例:**

Z
zengyawen 已提交
506
```js
Z
zhanghaifeng 已提交
507 508
connection.getAllNets().then(function (data) {
    console.log(JSON.stringify(data))
C
clevercong 已提交
509 510 511
});
```

512 513 514 515
## connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void

Z
zengyawen 已提交
516
获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。
517 518 519 520 521 522 523

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
524 525 526
| 参数名    | 类型                                                         | 必填 | 说明             |
| --------- | ------------------------------------------------------------ | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |
Z
zhanghaifeng 已提交
527 528 529 530 531 532 533 534 535 536 537
| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是   | 回调函数。当成功获取netHandle对应的网络的连接信息时,err为undefined,data为获取的网络连接信息;否则为错误对象|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
538 539 540

**示例:**

Z
zengyawen 已提交
541
```js
542
connection.getDefaultNet().then(function (netHandle) {
Z
zhanghaifeng 已提交
543
    connection.getConnectionProperties(netHandle, function (error, data) {
544
        console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
545
        console.log(JSON.stringify(data))
546 547 548 549 550 551 552 553
    })
})
```

## connection.getConnectionProperties

getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>

Z
zengyawen 已提交
554
获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
555 556 557 558 559 560 561

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
562 563 564
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
565 566 567

**返回值:**

Z
zengyawen 已提交
568 569 570
| 类型                                                    | 说明                              |
| ------------------------------------------------------- | --------------------------------- |
| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 |
571

Z
zhanghaifeng 已提交
572 573 574 575 576 577 578 579 580 581
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

582 583
**示例:**

Z
zengyawen 已提交
584
```js
585
connection.getDefaultNet().then(function (netHandle) {
Z
zhanghaifeng 已提交
586 587
    connection.getConnectionProperties(netHandle).then(function (data) {
        console.log(JSON.stringify(data))
588 589 590 591 592 593 594 595
    })
})
```

## connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void

Z
zengyawen 已提交
596
获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。
597 598 599 600 601 602 603

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
604 605 606
| 参数名    | 类型                                                | 必填 | 说明             |
| --------- | --------------------------------------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle)                             | 是   | 数据网络的句柄。 |
Z
zhanghaifeng 已提交
607 608 609 610 611 612 613 614 615 616 617
| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是   | 回调函数。当成功获取netHandle对应的网络的能力信息时,err为undefined,data为获取到的网络能力信息;否则为错误对象       |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
618 619 620

**示例:**

Z
zengyawen 已提交
621
```js
622
connection.getDefaultNet().then(function (netHandle) {
Z
zhanghaifeng 已提交
623
    connection.getNetCapabilities(netHandle, function (error, data) {
624
        console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
625
        console.log(JSON.stringify(data))
626 627 628 629 630 631 632 633
    })
})
```

## connection.getNetCapabilities

getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>

Z
zengyawen 已提交
634
获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
635 636 637 638 639 640 641

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
642 643 644
| 参数名    | 类型                    | 必填 | 说明             |
| --------- | ----------------------- | ---- | ---------------- |
| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
645 646 647

**返回值:**

Z
zengyawen 已提交
648 649 650
| 类型                                          | 说明                              |
| --------------------------------------------- | --------------------------------- |
| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 |
651

Z
zhanghaifeng 已提交
652 653 654 655 656 657 658 659 660 661
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

662 663
**示例:**

Z
zengyawen 已提交
664
```js
665
connection.getDefaultNet().then(function (netHandle) {
Z
zhanghaifeng 已提交
666 667
    connection.getNetCapabilities(netHandle).then(function (data) {
        console.log(JSON.stringify(data))
668 669 670 671
    })
})
```

L
liyufan 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685
## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(callback: AsyncCallback\<boolean>): void

检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                   |
| -------- | ----------------------- | ---- | -------------------------------------- |
Z
zhanghaifeng 已提交
686 687 688 689 690 691 692 693 694
| callback | AsyncCallback\<boolean> | 是   | 回调函数。当前网络上的数据流量使用被计量返回true。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
L
liyufan 已提交
695 696 697 698

**示例:**

```js
Z
zhanghaifeng 已提交
699
connection.isDefaultNetMetered(function (error, data) {
L
liyufan 已提交
700
    console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
701
    console.log('data: ' + data)
L
liyufan 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
})
```

## connection.isDefaultNetMetered<sup>9+</sup>

isDefaultNetMetered(): Promise\<boolean>

检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型              | 说明                                            |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | 以Promise形式返回,当前网络上的数据流量使用被计量true。 |

Z
zhanghaifeng 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.isDefaultNetMetered().then(function (data) {
    console.log('data: ' + data)
})
```

## connection.hasDefaultNet

hasDefaultNet(callback: AsyncCallback\<boolean>): void

检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                    | 必填 | 说明                                   |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是   | 回调函数。默认数据网络被激活返回true。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.hasDefaultNet(function (error, data) {
    console.log(JSON.stringify(error))
    console.log('data: ' + data)
})
```

## connection.hasDefaultNet

hasDefaultNet(): Promise\<boolean>

检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。

**需要权限**:ohos.permission.GET_NETWORK_INFO

**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型              | 说明                                            |
| ----------------- | ----------------------------------------------- |
| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.hasDefaultNet().then(function (data) {
    console.log('data: ' + data)
})
```

## connection.enableAirplaneMode

enableAirplaneMode(callback: AsyncCallback\<void>): void

开启飞行模式,使用callback方式作为异步方法。

**系统接口**:此接口为系统接口。

Y
Yangys 已提交
810 811
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL

Z
zhanghaifeng 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | 是   | 回调函数。         |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

L
liyufan 已提交
827 828 829
**示例:**

```js
Z
zhanghaifeng 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842
connection.enableAirplaneMode(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.enableAirplaneMode

enableAirplaneMode(): Promise\<void>

开启飞行模式,使用Promise方式作为异步方法。

**系统接口**:此接口为系统接口。

Y
Yangys 已提交
843 844
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL

Z
zhanghaifeng 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 无返回值的Promise对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.enableAirplaneMode().then(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.disableAirplaneMode

disableAirplaneMode(callback: AsyncCallback\<void>): void

关闭飞行模式,使用callback方式作为异步方法。

**系统接口**:此接口为系统接口。

Y
Yangys 已提交
876 877
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL

Z
zhanghaifeng 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<void> | 是   | 回调函数。当关闭飞行模式成功,err为undefined,否则为错误对象。|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.disableAirplaneMode(function (error) {
    console.log(JSON.stringify(error))
})
```

## connection.disableAirplaneMode

disableAirplaneMode(): Promise\<void>

关闭飞行模式,使用Promise方式作为异步方法。

**系统接口**:此接口为系统接口。

Y
Yangys 已提交
909 910
**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL

Z
zhanghaifeng 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
**系统能力**:SystemCapability.Communication.NetManager.Core

**返回值:**

| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | 无返回值的Promise对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

**示例:**

```js
connection.disableAirplaneMode().then(function (error) {
    console.log(JSON.stringify(error))
L
liyufan 已提交
931 932 933
})
```

C
clevercong 已提交
934 935 936 937
## connection.reportNetConnected

reportNetConnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

938
向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
939
使用callback方式作为异步方法。
C
clevercong 已提交
940

C
clevercong 已提交
941 942 943
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
944 945

**参数:**
946

C
clevercong 已提交
947 948 949
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
Z
zhanghaifeng 已提交
950 951 952 953 954 955 956 957 958 959 960
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当向网络管理报告网络处于可用状态成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
C
clevercong 已提交
961 962 963

**示例:**

Z
zengyawen 已提交
964
```js
C
clevercong 已提交
965 966 967 968 969 970 971 972 973 974 975
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetConnected

reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;

976
向网络管理报告网络处于可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
977
使用Promise方式作为异步方法。
C
clevercong 已提交
978

C
clevercong 已提交
979 980 981
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
982 983

**参数:**
984

C
clevercong 已提交
985 986 987 988 989 990 991
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
992
| Promise&lt;void&gt; | 无返回值的Promise对象。 |
C
clevercong 已提交
993

Z
zhanghaifeng 已提交
994 995 996 997 998 999 1000 1001 1002 1003
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

C
clevercong 已提交
1004 1005
**示例:**

Z
zengyawen 已提交
1006
```js
C
clevercong 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetConnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

## connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void

1018
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
1019
使用callback方式作为异步方法。
C
clevercong 已提交
1020

C
clevercong 已提交
1021 1022 1023
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1024 1025

**参数:**
1026

C
clevercong 已提交
1027 1028 1029
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
Z
zhanghaifeng 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当向网络管理报告网络处于不可用状态成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
C
clevercong 已提交
1041 1042 1043

**示例:**

Z
zengyawen 已提交
1044
```js
C
clevercong 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle, function (error) {
        console.log(JSON.stringify(error))
    });
});
```

## connection.reportNetDisconnected

reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;

1056
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
1057
使用Promise方式作为异步方法。
C
clevercong 已提交
1058

C
clevercong 已提交
1059 1060 1061
**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1062 1063

**参数:**
1064

C
clevercong 已提交
1065 1066 1067 1068 1069 1070 1071
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |

**返回值:**
| 类型 | 说明 |
| -------- | -------- |
1072
| Promise&lt;void&gt; | 无返回值的Promise对象。 |
C
clevercong 已提交
1073

Z
zhanghaifeng 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

C
clevercong 已提交
1084 1085
**示例:**

Z
zengyawen 已提交
1086
```js
C
clevercong 已提交
1087 1088 1089 1090 1091 1092 1093
connection.getDefaultNet().then(function (netHandle) {
    connection.reportNetDisconnected(netHandle).then(function () {
        console.log(`report success`)
    });
});
```

1094 1095 1096 1097
## connection.getAddressesByName

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Z
zengyawen 已提交
1098
使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
1099

L
liyufan 已提交
1100
**需要权限**:ohos.permission.INTERNET
1101 1102 1103 1104 1105

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1106 1107 1108
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
Z
zhanghaifeng 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。当使用默认网络解析主机名成功获取所有IP地址,err为undefined,data为获取到的所有IP地址;否则为错误对象。|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
1120 1121 1122

**示例:**

Z
zengyawen 已提交
1123
```js
C
clevercong 已提交
1124
let host = "xxxx";
Z
zhanghaifeng 已提交
1125
connection.getAddressesByName(host, function (error, data) {
C
clevercong 已提交
1126
    console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
1127
    console.log(JSON.stringify(data))
1128 1129 1130 1131 1132
})
```

## connection.getAddressesByName

C
clevercong 已提交
1133
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
1134

Z
zengyawen 已提交
1135
使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
1136

L
liyufan 已提交
1137
**需要权限**:ohos.permission.INTERNET
1138 1139 1140 1141 1142

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1143 1144 1145
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1146 1147 1148

**返回值:**

Z
zengyawen 已提交
1149 1150 1151
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
1152

Z
zhanghaifeng 已提交
1153
**错误码:**
C
clevercong 已提交
1154

Z
zhanghaifeng 已提交
1155 1156 1157 1158 1159 1160 1161
| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
C
clevercong 已提交
1162 1163 1164 1165

**示例:**

```js
Z
zhanghaifeng 已提交
1166 1167 1168
let host = "xxxx";
connection.getAddressesByName(host).then(function (data) {
    console.log(JSON.stringify(data))
C
clevercong 已提交
1169 1170 1171
})
```

Z
zhanghaifeng 已提交
1172
## NetConnection
C
clevercong 已提交
1173

Z
zhanghaifeng 已提交
1174
网络连接的句柄。
C
clevercong 已提交
1175

Z
zhanghaifeng 已提交
1176
### register
C
clevercong 已提交
1177

Z
zhanghaifeng 已提交
1178 1179 1180 1181 1182
register(callback: AsyncCallback\<void>): void

订阅指定网络状态变化的通知。

**需要权限**:ohos.permission.GET_NETWORK_INFO
C
clevercong 已提交
1183

C
clevercong 已提交
1184 1185
**系统能力**:SystemCapability.Communication.NetManager.Core

Z
zhanghaifeng 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
**参数:**

| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。当订阅指定网络状态变化的通知成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
| 2101008 | The callback is not exists.      |
| 2101022 | The number of requests exceeded the maximum. |
C
clevercong 已提交
1201 1202 1203 1204 1205


**示例:**

```js
Z
zhanghaifeng 已提交
1206
netConnection.register(function (error) {
C
clevercong 已提交
1207 1208 1209 1210
    console.log(JSON.stringify(error))
})
```

Z
zhanghaifeng 已提交
1211
### unregister
1212

Z
zhanghaifeng 已提交
1213
unregister(callback: AsyncCallback\<void>): void
1214

Z
zhanghaifeng 已提交
1215
取消订阅默认网络状态变化的通知。
1216 1217 1218 1219 1220

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zhanghaifeng 已提交
1221 1222 1223
| 参数名   | 类型                 | 必填 | 说明       |
| -------- | -------------------- | ---- | ---------- |
| callback | AsyncCallback\<void> | 是   | 回调函数。当取消订阅指定网络状态变化的通知成功,err为undefined,否则为错误对象。 |
1224

Z
zhanghaifeng 已提交
1225
**错误码:**
1226

Z
zhanghaifeng 已提交
1227 1228 1229 1230 1231
| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
| 2101007 | The same callback exists.      |
1232 1233 1234

**示例:**

Z
zengyawen 已提交
1235
```js
Z
zhanghaifeng 已提交
1236 1237
netConnection.unregister(function (error) {
    console.log(JSON.stringify(error))
1238 1239 1240
})
```

M
maosiping 已提交
1241
### on('netAvailable')
1242 1243 1244

on(type: 'netAvailable', callback: Callback\<NetHandle>): void

C
clevercong 已提交
1245
订阅网络可用事件。
1246

Z
zhanghaifeng 已提交
1247 1248
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。

1249 1250 1251 1252
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1253 1254
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
1255
| type     | string                             | 是   | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 |
Z
zhanghaifeng 已提交
1256
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数,返回数据网络句柄。|
1257 1258 1259

**示例:**

Z
zengyawen 已提交
1260
```js
Z
zhanghaifeng 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
// 创建NetConnection对象
let netCon = connection.createNetConnection()

// 先使用register接口注册订阅事件
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// 订阅网络可用事件。调用register后,才能接收到此事件通知
netCon.on('netAvailable', function (data) {
1271 1272
    console.log(JSON.stringify(data))
})
Z
zhanghaifeng 已提交
1273 1274 1275 1276 1277

// 使用unregister接口取消订阅
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
1278 1279
```

Z
zhanghaifeng 已提交
1280
### on('netBlockStatusChange')
1281

Z
zhanghaifeng 已提交
1282
on(type: 'netBlockStatusChange', callback: Callback&lt;{ netHandle: NetHandle, blocked: boolean }&gt;): void
1283

Z
zhanghaifeng 已提交
1284 1285 1286
订阅网络阻塞状态事件,使用callback方式作为异步方法。

**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1287 1288 1289 1290 1291

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1292 1293
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
Z
zhanghaifeng 已提交
1294 1295
| type     | string                                                       | 是   | 订阅事件,固定为'netBlockStatusChange'。<br/>netBlockStatusChange:网络阻塞状态事件。 |
| callback | Callback&lt;{&nbsp;netHandle:&nbsp;[NetHandle](#nethandle),&nbsp;blocked:&nbsp;boolean&nbsp;}&gt; | 是   | 回调函数,返回数据网络句柄(netHandle),及网络堵塞状态(blocked)。|
1296 1297 1298

**示例:**

Z
zengyawen 已提交
1299
```js
Z
zhanghaifeng 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
// 创建NetConnection对象
let netCon = connection.createNetConnection()

// 先使用register接口注册订阅事件
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// 订阅网络阻塞状态事件。调用register后,才能接收到此事件通知
netCon.on('netBlockStatusChange', function (data) {
1310 1311
    console.log(JSON.stringify(data))
})
Z
zhanghaifeng 已提交
1312 1313 1314 1315 1316

// 使用unregister接口取消订阅
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
1317 1318
```

Z
zhanghaifeng 已提交
1319
### on('netCapabilitiesChange')
1320

Z
zhanghaifeng 已提交
1321
on(type: 'netCapabilitiesChange', callback: Callback<{ netHandle: NetHandle, netCap: NetCapabilities }>): void
1322

Z
zhanghaifeng 已提交
1323 1324 1325
订阅网络能力变化事件。

**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1326 1327 1328 1329 1330

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1331 1332
| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
Z
zhanghaifeng 已提交
1333 1334
| type     | string                                                       | 是   | 订阅事件,固定为'netCapabilitiesChange'。<br/>netCapabilitiesChange:网络能力变化事件。 |
| callback | Callback<{ netHandle: [NetHandle](#nethandle), netCap: [NetCapabilities](#netcapabilities) }> | 是   | 回调函数,返回数据网络句柄(netHandle)和网络的能力信息(netCap)。|
1335 1336 1337

**示例:**

Z
zengyawen 已提交
1338
```js
Z
zhanghaifeng 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
// 创建NetConnection对象
let netCon = connection.createNetConnection()

// 先使用register接口注册订阅事件
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
netCon.on('netCapabilitiesChange', function (data) {
C
clevercong 已提交
1349 1350
    console.log(JSON.stringify(data))
})
Z
zhanghaifeng 已提交
1351 1352 1353 1354 1355

// 使用unregister接口取消订阅
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
C
clevercong 已提交
1356 1357
```

Z
zhanghaifeng 已提交
1358
### on('netConnectionPropertiesChange')
C
clevercong 已提交
1359

Z
zhanghaifeng 已提交
1360
on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void
C
clevercong 已提交
1361

Z
zhanghaifeng 已提交
1362 1363 1364
订阅网络连接信息变化事件。

**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
C
clevercong 已提交
1365 1366 1367 1368 1369 1370 1371

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名   | 类型                                                         | 必填 | 说明                                                         |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
Z
zhanghaifeng 已提交
1372 1373
| type     | string                                                       | 是   | 订阅事件,固定为'netConnectionPropertiesChange'。<br/>netConnectionPropertiesChange:网络连接信息变化事件。 |
| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是   | 回调函数,返回数据网络句柄(netHandle)和网络的连接信息(connectionProperties)|
C
clevercong 已提交
1374 1375 1376

**示例:**

Z
zengyawen 已提交
1377
```js
Z
zhanghaifeng 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
// 创建NetConnection对象
let netCon = connection.createNetConnection()

// 先使用register接口注册订阅事件
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// 订阅网络连接信息变化事件。调用register后,才能接收到此事件通知
netCon.on('netConnectionPropertiesChange', function (data) {
1388 1389
    console.log(JSON.stringify(data))
})
Z
zhanghaifeng 已提交
1390 1391 1392 1393 1394

// 使用unregister接口取消订阅
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
1395 1396
```

M
maosiping 已提交
1397
### on('netLost')
1398 1399 1400

on(type: 'netLost', callback: Callback\<NetHandle>): void

C
clevercong 已提交
1401
订阅网络丢失事件。
1402

Z
zhanghaifeng 已提交
1403 1404
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。

1405 1406 1407 1408
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1409 1410
| 参数名   | 类型                               | 必填 | 说明                                                         |
| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
1411
| type     | string                             | 是   | 订阅事件,固定为'netLost'。<br/>netLost:网络严重中断或正常断开事件。 |
Z
zhanghaifeng 已提交
1412
| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数,数据网络句柄(netHandle)|
1413 1414 1415

**示例:**

Z
zengyawen 已提交
1416
```js
Z
zhanghaifeng 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
// 创建NetConnection对象
let netCon = connection.createNetConnection()

// 先使用register接口注册订阅事件
netCon.register(function (error) {
    console.log(JSON.stringify(error))
})

// 订阅网络丢失事件。调用register后,才能接收到此事件通知
netCon.on('netLost', function (data) {
1427 1428
    console.log(JSON.stringify(data))
})
Z
zhanghaifeng 已提交
1429 1430 1431 1432 1433

// 使用unregister接口取消订阅
netCon.unregister(function (error) {
    console.log(JSON.stringify(error))
})
1434 1435
```

M
maosiping 已提交
1436
### on('netUnavailable')
1437 1438 1439

on(type: 'netUnavailable', callback: Callback\<void>): void

C
clevercong 已提交
1440
订阅网络不可用事件。
1441

Z
zhanghaifeng 已提交
1442 1443
**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。

1444 1445 1446 1447
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1448 1449
| 参数名   | 类型            | 必填 | 说明                                                         |
| -------- | --------------- | ---- | ------------------------------------------------------------ |
C
clevercong 已提交
1450
| type     | string          | 是   | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 |
Z
zhanghaifeng 已提交
1451
| callback | Callback\<void> | 是   | 回调函数,无返回结果。|
1452 1453 1454

**示例:**

Z
zengyawen 已提交
1455
```js
Z
zhanghaifeng 已提交
1456 1457
// 创建NetConnection对象
let netCon = connection.createNetConnection()
1458

Z
zhanghaifeng 已提交
1459 1460
// 先使用register接口注册订阅事件
netCon.register(function (error) {
1461 1462 1463
    console.log(JSON.stringify(error))
})

Z
zhanghaifeng 已提交
1464 1465 1466 1467
// 订阅网络不可用事件。调用register后,才能接收到此事件通知
netCon.on('netUnavailable', function (data) {
    console.log(JSON.stringify(data))
})
1468

Z
zhanghaifeng 已提交
1469 1470
// 使用unregister接口取消订阅
netCon.unregister(function (error) {
1471 1472 1473 1474
    console.log(JSON.stringify(error))
})
```

M
maosiping 已提交
1475
## NetHandle
1476

Z
zengyawen 已提交
1477
数据网络的句柄。
1478

Z
zengyawen 已提交
1479 1480
在调用NetHandle的方法之前,需要先获取NetHandle对象。

C
clevercong 已提交
1481 1482
**系统能力**:SystemCapability.Communication.NetManager.Core

Z
zengyawen 已提交
1483 1484
### 属性

L
liyufan 已提交
1485 1486 1487
| 名称    | 类型   | 必填 | 说明                      |
| ------ | ------ | --- |------------------------- |
| netId  | number | 是  |  网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 |
1488

L
liyufan 已提交
1489
### bindSocket<sup>9+</sup>
Z
zhuwenchao 已提交
1490

Z
zhanghaifeng 已提交
1491
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void
Z
zhuwenchao 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名      | 类型                     | 必填 | 说明                            |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
Z
zhanghaifeng 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
| callback    | AsyncCallback\<void>      | 是   | 回调函数。当TCPSocket或UDPSocket成功绑定到当前网络,err为undefined,否则为错误对象。|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
Z
zhuwenchao 已提交
1512 1513 1514 1515

**示例:**

```js
Y
YOUR_NAME 已提交
1516
import socket from "@ohos.net.socket";
Z
zhanghaifeng 已提交
1517
connection.getDefaultNet().then((netHandle) => {
Z
zhuwenchao 已提交
1518 1519
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
Y
YOUR_NAME 已提交
1520
    let socketType = "TCPSocket";
Z
zhuwenchao 已提交
1521 1522
    if (socketType == "TCPSocket") {
        tcp.bind({
Z
zhanghaifeng 已提交
1523 1524 1525
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
Y
YOUR_NAME 已提交
1526 1527
                console.log('bind fail');
            }
Z
zhanghaifeng 已提交
1528
            netHandle.bindSocket(tcp, (error, data) => {
Y
YOUR_NAME 已提交
1529 1530 1531 1532 1533 1534
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
1535 1536
        })
    } else {
Y
YOUR_NAME 已提交
1537
        let callback = value => {
Z
zhanghaifeng 已提交
1538
            console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
Y
YOUR_NAME 已提交
1539
        }
Z
zhuwenchao 已提交
1540 1541
        udp.on('message', callback);
        udp.bind({
Z
zhanghaifeng 已提交
1542 1543 1544
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
Y
YOUR_NAME 已提交
1545 1546
                console.log('bind fail');
            }
Z
zhuwenchao 已提交
1547
            udp.on('message', (data) => {
Y
YOUR_NAME 已提交
1548
                console.log(JSON.stringify(data))
Z
zhuwenchao 已提交
1549
            });
Z
zhanghaifeng 已提交
1550
            netHandle.bindSocket(udp, (error, data) => {
Y
YOUR_NAME 已提交
1551 1552 1553 1554 1555 1556
                if (error) {
                    console.log(JSON.stringify(error));
                } else {
                    console.log(JSON.stringify(data));
                }
            })
Z
zhuwenchao 已提交
1557
        })
Y
YOUR_NAME 已提交
1558 1559
    }
})
Z
zhuwenchao 已提交
1560 1561
```

L
liyufan 已提交
1562
### bindSocket<sup>9+</sup>
Z
zhuwenchao 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;

将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

| 参数名          | 类型                  | 必填  | 说明                           |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam     | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是   | 待绑定的TCPSocket或UDPSocket对象。|

**返回值:**

| 类型           | 说明                   |
| -------------- | ---------------------- |
1580
| Promise\<void> | 无返回值的Promise对象。 |
Z
zhuwenchao 已提交
1581

Z
zhanghaifeng 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

Z
zhuwenchao 已提交
1591 1592 1593
**示例:**

```js
Y
YOUR_NAME 已提交
1594
import socket from "@ohos.net.socket";
Z
zhanghaifeng 已提交
1595
connection.getDefaultNet().then((netHandle) => {
Z
zhuwenchao 已提交
1596 1597
    var tcp = socket.constructTCPSocketInstance();
    var udp = socket.constructUDPSocketInstance();
Y
YOUR_NAME 已提交
1598 1599
    let socketType = "TCPSocket";
    if (socketType == "TCPSocket") {
Z
zhuwenchao 已提交
1600
        tcp.bind({
Z
zhanghaifeng 已提交
1601 1602 1603
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
Y
YOUR_NAME 已提交
1604 1605
                console.log('bind fail');
            }
Z
zhanghaifeng 已提交
1606 1607 1608 1609
            netHandle.bindSocket(tcp).then((data) => {
                console.log(JSON.stringify(data));
            }).catch(error => {
                console.log(JSON.stringify(error));
Y
YOUR_NAME 已提交
1610
            })
Z
zhuwenchao 已提交
1611 1612
        })
    } else {
Y
YOUR_NAME 已提交
1613
        let callback = value => {
Z
zhanghaifeng 已提交
1614
            console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
Y
YOUR_NAME 已提交
1615
        }
Z
zhuwenchao 已提交
1616 1617
        udp.on('message', callback);
        udp.bind({
Z
zhanghaifeng 已提交
1618 1619 1620
            address: '192.168.xx.xxx', port: 8080, family: 1
        }, error => {
            if (error) {
Y
YOUR_NAME 已提交
1621 1622
                console.log('bind fail');
            }
Z
zhuwenchao 已提交
1623
            udp.on('message', (data) => {
Y
YOUR_NAME 已提交
1624 1625
                console.log(JSON.stringify(data));
            })
Z
zhanghaifeng 已提交
1626 1627 1628 1629
            netHandle.bindSocket(udp).then((data) => {
                console.log(JSON.stringify(data));
            }).catch(error => {
                console.log(JSON.stringify(error));
Y
YOUR_NAME 已提交
1630
            })
Z
zhuwenchao 已提交
1631
        })
Y
YOUR_NAME 已提交
1632 1633
    }
})
Z
zhuwenchao 已提交
1634 1635
```

M
maosiping 已提交
1636
### getAddressesByName
1637 1638 1639

getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void

Z
zengyawen 已提交
1640
使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
1641

L
liyufan 已提交
1642
**需要权限**:ohos.permission.INTERNET
C
clevercong 已提交
1643

1644 1645 1646 1647
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1648 1649 1650
| 参数名   | 类型                                              | 必填 | 说明               |
| -------- | ------------------------------------------------- | ---- | ------------------ |
| host     | string                                            | 是   | 需要解析的主机名。 |
Z
zhanghaifeng 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。当使用对应网络解析主机名成功获取所有IP地址,err为undefined,data为获取到的所有IP地址;否则为错误对象。|

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
1662 1663 1664

**示例:**

Z
zengyawen 已提交
1665
```js
1666
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1667
    let host = "xxxx";
Z
zhanghaifeng 已提交
1668
    netHandle.getAddressesByName(host, function (error, data) {
1669
        console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
1670
        console.log(JSON.stringify(data))
1671 1672 1673 1674
    })
})
```

M
maosiping 已提交
1675
### getAddressesByName
1676

C
clevercong 已提交
1677
getAddressesByName(host: string): Promise\<Array\<NetAddress>>
1678

Z
zengyawen 已提交
1679
使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
1680

L
liyufan 已提交
1681
**需要权限**:ohos.permission.INTERNET
C
clevercong 已提交
1682

1683 1684 1685 1686
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1687 1688 1689
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1690 1691 1692

**返回值:**

Z
zengyawen 已提交
1693 1694 1695
| 类型                                        | 说明                          |
| ------------------------------------------- | ----------------------------- |
| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
1696

Z
zhanghaifeng 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

1707 1708
**示例:**

Z
zengyawen 已提交
1709
```js
1710
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1711
    let host = "xxxx";
Z
zhanghaifeng 已提交
1712 1713
    netHandle.getAddressesByName(host).then(function (data) {
        console.log(JSON.stringify(data))
1714 1715 1716 1717
    })
})
```

M
maosiping 已提交
1718
### getAddressByName
1719 1720 1721

getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void

Z
zengyawen 已提交
1722
使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。
1723

L
liyufan 已提交
1724
**需要权限**:ohos.permission.INTERNET
C
clevercong 已提交
1725

1726 1727 1728 1729
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1730 1731 1732
| 参数名   | 类型                                      | 必填 | 说明               |
| -------- | ----------------------------------------- | ---- | ------------------ |
| host     | string                                    | 是   | 需要解析的主机名。 |
Z
zhanghaifeng 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是   | 回调函数。当使用对应网络解析主机名获取第一个IP地址成功,err为undefined,data为获取的第一个IP地址;否则为错误对象。         |

**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |
1744 1745 1746

**示例:**

Z
zengyawen 已提交
1747
```js
1748
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1749
    let host = "xxxx";
Z
zhanghaifeng 已提交
1750
    netHandle.getAddressByName(host, function (error, data) {
1751
        console.log(JSON.stringify(error))
Z
zhanghaifeng 已提交
1752
        console.log(JSON.stringify(data))
1753 1754 1755 1756
    })
})
```

M
maosiping 已提交
1757
### getAddressByName
1758

C
clevercong 已提交
1759
getAddressByName(host: string): Promise\<NetAddress>
1760

Z
zengyawen 已提交
1761
使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。
1762

L
liyufan 已提交
1763
**需要权限**:ohos.permission.INTERNET
C
clevercong 已提交
1764

1765 1766 1767 1768
**系统能力**:SystemCapability.Communication.NetManager.Core

**参数:**

Z
zengyawen 已提交
1769 1770 1771
| 参数名 | 类型   | 必填 | 说明               |
| ------ | ------ | ---- | ------------------ |
| host   | string | 是   | 需要解析的主机名。 |
1772 1773 1774

**返回值:**

Z
zengyawen 已提交
1775 1776 1777
| 类型                                | 说明                            |
| ----------------------------------- | ------------------------------- |
| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
1778

Z
zhanghaifeng 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
**错误码:**

| 错误码ID | 错误信息                        |
| ------- | -----------------------------  |
| 201     | Permission denied.             |
| 401     | Parameter error.               |
| 2100001 | Invalid parameter value.                |
| 2100002 | Operation failed. Cannot connect to service.|
| 2100003 | System internal error.         |

1789 1790
**示例:**

Z
zengyawen 已提交
1791
```js
1792
connection.getDefaultNet().then(function (netHandle) {
C
clevercong 已提交
1793
    let host = "xxxx";
Z
zhanghaifeng 已提交
1794 1795
    netHandle.getAddressByName(host).then(function (data) {
        console.log(JSON.stringify(data))
1796 1797 1798 1799
    })
})
```

M
maosiping 已提交
1800
## NetCap
1801

M
maosiping 已提交
1802
网络具体能力。
1803

Z
zengyawen 已提交
1804
**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1805

L
liyufan 已提交
1806
| 名称                  | 值   | 说明                   |
Z
zengyawen 已提交
1807
| ------------------------ | ---- | ---------------------- |
C
clevercong 已提交
1808 1809
| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia&nbsp;Message&nbsp;Service,多媒体短信服务)发送和接收彩信。 |
| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 |
1810
| NET_CAPABILITY_INTERNET  | 12   | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 |
C
clevercong 已提交
1811
| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual&nbsp;Private&nbsp;Network,虚拟专用网络)。 |
1812
| NET_CAPABILITY_VALIDATED | 16   | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 |
1813

M
maosiping 已提交
1814
## NetBearType
1815

M
maosiping 已提交
1816
网络类型。
1817

Z
zengyawen 已提交
1818
**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1819 1820

| 名称         | 值   | 说明        |
Z
zengyawen 已提交
1821 1822 1823
| --------------- | ---- | ----------- |
| BEARER_CELLULAR | 0    | 蜂窝网络。  |
| BEARER_WIFI     | 1    | Wi-Fi网络。 |
C
clevercong 已提交
1824
| BEARER_ETHERNET | 3 | 以太网网络。 |
1825

Y
Yangys 已提交
1826
## HttpProxy<sup>10+</sup>
Z
zhanghaifeng 已提交
1827 1828 1829 1830 1831 1832 1833 1834 1835

网络全局代理配置信息

**系统能力**:SystemCapability.Communication.NetManager.Core

| 名称    | 类型   | 必填 | 说明                      |
| ------ | ------ | --- |------------------------- |
| host  | string | 否  |  代理服务器主机名。 |
| port  | number | 否  |  主机端口。 |
Y
Yangys 已提交
1836
| exclusionList  | Array<string> | 否  |  不使用代理服务器的屏蔽列表。 |
Z
zhanghaifeng 已提交
1837 1838 1839 1840 1841

## NetSpecifier

提供承载数据网络能力的实例。

Z
zengyawen 已提交
1842
**系统能力**:SystemCapability.Communication.NetManager.Core
Z
zhanghaifeng 已提交
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852

| 名称                     | 类型                                | 必填  | 说明                                                         |
| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
| netCapabilities         | [NetCapabilities](#netcapabilities) |  是  | 存储数据网络的传输能力和承载类型。                                |
| bearerPrivateIdentifier | string                              |  否  |  网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 |

## NetCapabilities

网络的能力集。

Z
zengyawen 已提交
1853
**系统能力**:SystemCapability.Communication.NetManager.Core
Z
zhanghaifeng 已提交
1854 1855 1856 1857 1858 1859 1860 1861

| 名称                  | 类型                                | 必填 | 说明                     |
| --------------------- | ---------------------------------- | --- | ------------------------ |
| linkUpBandwidthKbps   | number                             |  否 |  上行(设备到网络)带宽。  |
| linkDownBandwidthKbps | number                             |  否 |  下行(网络到设备)带宽。   |
| networkCap            | Array\<[NetCap](#netcap)>           |  否 |  网络具体能力。           |
| bearerTypes           | Array\<[NetBearType](#netbeartype)> |  是 |  网络类型。               |

M
maosiping 已提交
1862
## ConnectionProperties
1863

M
maosiping 已提交
1864
网络连接信息。
1865

Z
zengyawen 已提交
1866
**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1867

L
liyufan 已提交
1868 1869 1870 1871 1872 1873
| 名称           | 类型                               | 必填 |  说明             |
| ------------- | ---------------------------------- | ----|---------------- |
| interfaceName | string                             | 是 |网卡名称。       |
| domains       | string                             | 是 |所属域,默认""。 |
| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。       |
| routes        | Array\<[RouteInfo](#routeinfo)>     | 是 |路由信息。       |
Z
zhanghaifeng 已提交
1874
| dnses     | Array\<[NetAddress](#netaddress)> | 是 |网络地址,参考[NetAddress](#netaddress)。 |
L
liyufan 已提交
1875
| mtu           | number                             | 是 |最大传输单元。   |
1876

M
maosiping 已提交
1877
## RouteInfo
1878

M
maosiping 已提交
1879
网络路由信息。
1880

Z
zengyawen 已提交
1881
**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1882

L
liyufan 已提交
1883 1884 1885 1886 1887 1888 1889
| 名称           | 类型                        | 必填 |说明             |
| -------------- | --------------------------- | --- |---------------- |
| interface      | string                      | 是 |网卡名称。       |
| destination    | [LinkAddress](#linkaddress) | 是 |目的地址。       |
| gateway        | [NetAddress](#netaddress)   | 是 |网关地址。       |
| hasGateway     | boolean                     | 是 |是否有网关。     |
| isDefaultRoute | boolean                     | 是 |是否为默认路由。 |
1890

Z
zhanghaifeng 已提交
1891 1892 1893 1894
## LinkAddress

网络链路信息。

Z
zengyawen 已提交
1895
**系统能力**:SystemCapability.Communication.NetManager.Core
Z
zhanghaifeng 已提交
1896 1897 1898 1899 1900 1901

| 名称        | 类型                      | 必填 |说明                 |
| ------------ | ----------------------- |---- |-------------------- |
| address      | [NetAddress](#netaddress) | 是 | 链路地址。           |
| prefixLength | number                    | 是 |链路地址前缀的长度。 |

M
maosiping 已提交
1902
## NetAddress
1903

Z
zengyawen 已提交
1904
网络地址。
1905

Z
zengyawen 已提交
1906
**系统能力**:SystemCapability.Communication.NetManager.Core
C
clevercong 已提交
1907

L
liyufan 已提交
1908 1909 1910 1911 1912
| 名称    | 类型   | 必填 | 说明                           |
| ------- | ------ | -- |------------------------------ |
| address | string | 是 |地址。                         |
| family  | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port    | number | 否 |端口,取值范围\[0, 65535]。    |