1# @ohos.uri (URI字符串解析)
2
3本模块提供URI字符串解析的相关功能。URI遵循RFC3986规范标准,该标准定义了如何编码和解析用于定位网络资源的标识符,对于非标准场景解析不支持。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10## 导入模块
11
12```ts
13import { uri } from '@kit.ArkTS';
14```
15
16## URI
17
18构造URI对象,提供判断对象相等、规范路径等方法。
19
20### 属性
21
22**系统能力:** SystemCapability.Utils.Lang
23
24| 名称 | 类型 | 可读 | 可写 | 说明 |
25| -------- | -------- | -------- | -------- | -------- |
26| scheme | string | 是 | 否 | 获取URI的协议部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。|
27| userInfo | string | 是 | 否 | 获取URI的用户信息部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
28| host | string | 是 | 否 | 获取URI的主机名部分(不带端口),若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。|
29| port | string | 是 | 否 | 获取URI的端口部分。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
30| path | string | 是 | 否 | 获取URI的路径部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
31| query | string | 是 | 否 | 获取URI的查询部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
32| fragment | string | 是 | 否 | 获取URI的片段部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
33| authority | string | 是 | 否 | 获取此URI的解码权限组件部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
34| ssp | string | 是 | 否 | 获取URI的解码方案特定部分,方案特定部分是URI的一部分,它包含了特定于协议或方案的信息。<br/>**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。 |
35| encodedUserInfo<sup>12+</sup>  | string | 是   | 否   | 获取URI的编码用户信息部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。   |
36| encodedPath<sup>12+</sup>      | string | 是   | 否   | 获取URI的编码路径部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。       |
37| encodedQuery<sup>12+</sup>     | string | 是   | 否   | 获取URI的编码查询部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。      |
38| encodedFragment<sup>12+</sup>  | string | 是   | 否   | 获取URI的编码片段部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。      |
39| encodedAuthority<sup>12+</sup> | string | 是   | 否   | 获取URI的编码权限组件部分,若无此部分则返回null对象。 <br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。   |
40| encodedSSP<sup>12+</sup>       | string | 是   | 否   | 获取URI的编码方案特定部分。<br/>**原子化服务API:** 从API version 12 开始,该接口支持在原子化服务中使用。   |
41
42### 命名规则
43
44**命名形式:**
45
46标准uri定义主要由以下三个部分组成:
47[scheme:]scheme-specific-part[#fragment]。
48
49将URI格式细化一些则可分为:
50[scheme:][//authority][path][?query][#fragment]。
51
52将URI格式进一步细化可分为:
53[scheme:][//[user-info@]host[:port]][path][?query][#fragment]。
54
55- scheme: 协议名,与scheme-specific-part以:进行分隔,包含scheme部分的URI为绝对URI,不包含scheme部分的URI为相对URI,根据需要填写。例如http、https、ftp、datashare等。
56- scheme-specific-part: URI的特定解码方案特定部分,位于[scheme:]和[#fragment]之间由[//][authority][path][?query]组成,此部分以/开头的为分层URI,不以/开头的为不透明URI,根据需要填写。
57    - authority: URI的解码权限组件部分。由[userinfo@]host[:port]组成,根据需要填写。
58        - userinfo: 用户信息,与host通过@进行分隔,根据需要填写。
59        - host: 服务器的主机名部分,当authority存在时,此项必填。
60        - port: 服务器端口,默认值为-1。根据需要填写。
61    - path: 路径信息,位于host与query之间以 / 进行分割,根据需要填写。
62    - query: 查询部分,位于path和fragment之间,以 ? 开头的键值对格式,以&分割键值对,以=分割键值,根据需要填写。
63- fragment: 片段部分,以#与scheme-specific-part进行分隔,根据需要填写。
64
65**URI示例:**
66
67```ts
68const uriObj1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
69console.info(uriObj1.host) // ftp.aaa.bbb.ccc
70console.info(uriObj1.fragment) // null
71console.info(uriObj1.path) // /dddd/eee.txt
72console.info(uriObj1.scheme) // ftp
73console.info(uriObj1.userInfo) // null
74console.info(uriObj1.port) // -1
75console.info(uriObj1.query) // null
76
77const uriObj2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
78console.info(uriObj2.host) // spinaltap.micro.umn.edu
79console.info(uriObj2.fragment) // fragment
80console.info(uriObj2.path) // /00/Weather/California/Los Angeles
81console.info(uriObj2.scheme) // gopher
82console.info(uriObj2.userInfo) // null
83console.info(uriObj2.port) //-1
84console.info(uriObj2.query) // null
85
86const uriObj3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
87console.info(uriObj3.host) // null
88console.info(uriObj3.fragment) // null
89console.info(uriObj3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
90console.info(uriObj3.scheme) // datashare
91console.info(uriObj3.userInfo) // null
92console.info(uriObj3.port) // -1
93console.info(uriObj3.query) // null
94
95const uriObj4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
96console.info(uriObj4.host) // host
97console.info(uriObj4.fragment) // fragment
98console.info(uriObj4.path) // /directory/file
99console.info(uriObj4.scheme) // https
100console.info(uriObj4.userInfo) // username:password
101console.info(uriObj4.port) // 8080
102console.info(uriObj4.query) // foo=1&bar=2
103
104const uriObj5 = new uri.URI("dataability:///com.example.DataAbility");
105console.info(uriObj5.host) // null
106console.info(uriObj5.fragment) // null
107console.info(uriObj5.path) // /com.example.DataAbility:
108console.info(uriObj5.scheme) // dataability
109console.info(uriObj5.userInfo) // null
110console.info(uriObj5.port) // -1
111console.info(uriObj5.query) // null
112
113const uriObj6 = new uri.URI("https://username:my+name@host:8080/directory/my+file?foo=1&bar=2#fragment");
114console.info(uriObj6.encodedUserInfo) // username:my+name
115console.info(uriObj6.encodedPath) // /directory/my+file
116console.info(uriObj6.encodedQuery) // foo=1&bar=2
117console.info(uriObj6.encodedFragment) // fragment
118console.info(uriObj6.encodedAuthority) // username:my+name@host:8080
119console.info(uriObj6.encodedSSP) // //username:my+name@host:8080/directory/my+file?foo=1&bar=2
120
121let uriObj7 = new uri.URI("www.abc.com:8080/directory/file?ab=pppppp#qwer=da");
122console.log(uriObj7.scheme) // www.abc.com
123console.log(uriObj7.host) // null
124console.log(uriObj7.port) // -1
125console.log(uriObj7.path) // null
126console.log(uriObj7.query) // null
127console.log(uriObj7.authority) // null
128console.log(uriObj7.fragment) // qwer=da
129console.log(uriObj7.ssp) // 8080/directory/file?ab=pppppp
130console.log("result:", uriObj7.checkIsAbsolute()) // result: true
131```
132
133### constructor
134
135constructor(uri: string)
136
137constructor是URI的构造函数。
138
139**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。
140
141**系统能力:** SystemCapability.Utils.Lang
142
143**参数:**
144
145| 参数名 | 类型 | 必填 | 说明 |
146| -------- | -------- | -------- | -------- |
147| uri | string | 是 | 入参对象。 |
148
149**错误码:**
150
151以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
152
153| 错误码ID | 错误信息 |
154| -------- | -------- |
155| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
156| 10200002 | Invalid uri string. |
157
158**示例:**
159
160```ts
161let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
162new uri.URI(mm);
163```
164```ts
165new uri.URI('https://username:password@host:8080');
166```
167
168
169### toString
170
171toString(): string
172
173将URI转化为编码后的字符串。
174
175**系统能力:** SystemCapability.Utils.Lang
176
177**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。
178
179**返回值:**
180
181| 类型 | 说明 |
182| -------- | -------- |
183| string | 返回URI的字符串序列化。 |
184
185**示例:**
186
187```ts
188const result = new uri.URI('https://username:password@host:8080/directory/file?ab=pppppp#qwer da');
189let result1 = result.toString(); // https://username:password@host:8080/directory/file?ab=pppppp#qwer%20da
190```
191
192### equalsTo<sup>9+</sup>
193
194equalsTo(other: URI): boolean
195
196判断此URI是否与其他URI对象相等。
197
198**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。
199
200**系统能力:** SystemCapability.Utils.Lang
201
202**参数:**
203
204| 参数名 | 类型 | 必填 | 说明 |
205| -------- | -------- | -------- | -------- |
206| other | [URI](#uri) | 是 | 需要比较的URI对象。 |
207
208**返回值:**
209
210| 类型 | 说明 |
211| -------- | -------- |
212| boolean | 返回true表示相等,否则返回false。 |
213
214**错误码:**
215
216以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
217
218| 错误码ID | 错误信息 |
219| -------- | -------- |
220| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
221
222**示例:**
223
224```ts
225const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
226const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
227let result = uriInstance.equalsTo(uriInstance1); // true
228```
229
230### checkIsAbsolute
231
232checkIsAbsolute(): boolean
233
234判断此URI是否为绝对URI(是否定义了scheme组件)。
235
236**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。
237
238**系统能力:** SystemCapability.Utils.Lang
239
240**返回值:**
241
242| 类型 | 说明 |
243| -------- | -------- |
244| boolean | 如果是绝对URI返回true,否则返回false。|
245
246**示例:**
247
248```ts
249const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
250console.info(`${uriInstance.checkIsAbsolute()}`); // true
251const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
252console.info(`${uriInstance1.checkIsAbsolute()}`); // false
253```
254
255
256### normalize
257
258normalize(): URI
259
260规范化此URI的路径。
261
262**原子化服务API:** 从API version 11 开始,该接口支持在原子化服务中使用。
263
264**系统能力:** SystemCapability.Utils.Lang
265
266**返回值:**
267
268| 类型 | 说明 |
269| -------- | -------- |
270| [URI](#uri) | 返回一个path被规范化后的URI对象。 |
271
272**示例:**
273
274```ts
275const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
276console.info(uriInstance.path); // /path/path1/../path2/./path3
277let uriInstance1 = uriInstance.normalize();
278console.info(uriInstance1.path); // /path/path2/path3
279```
280
281### checkRelative<sup>12+</sup>
282
283checkRelative(): boolean
284
285判断此URI是否为相对URI,相对URI指的是不包含协议(scheme)部分的URI。
286
287**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
288
289**系统能力:** SystemCapability.Utils.Lang
290
291**返回值:**
292
293| 类型    | 说明                                       |
294| ------- | ------------------------------------------ |
295| boolean | 如果是相对URI返回true,否则返回false。 |
296
297**示例:**
298
299```ts
300const uriInstance = new uri.URI("https://username:password@www.qwer.com:8080?query=p");
301console.info(`${uriInstance.checkRelative()}`); // false
302const uriInstance1 = new uri.URI("/images/pic.jpg");
303console.info(`${uriInstance1.checkRelative()}`); // true
304```
305
306### checkOpaque<sup>12+</sup>
307
308checkOpaque(): boolean
309
310判断此URI是否为不透明URI,方案特定部分不以“/”开头的URI为不透明的URI。
311
312**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
313
314**系统能力:** SystemCapability.Utils.Lang
315
316**返回值:**
317
318| 类型    | 说明                                           |
319| ------- | ---------------------------------------------- |
320| boolean | 如果是不透明的URI返回true,否则返回false。 |
321
322**示例:**
323
324```ts
325const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
326console.info(`${uriInstance.checkOpaque()}`); // false
327const uriInstance1 = new uri.URI("mailto:user@example.com");
328console.info(`${uriInstance1.checkOpaque()}`); // true
329```
330
331### checkHierarchical<sup>12+</sup>
332
333checkHierarchical(): boolean
334
335判断此URI是否为分层URI,方案特定部分以“/”开头的URI为分层的URI。相对URI也是分层的。
336
337**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
338
339**系统能力:** SystemCapability.Utils.Lang
340
341**返回值:**
342
343| 类型    | 说明                                         |
344| ------- | -------------------------------------------- |
345| boolean | 如果是分层的URI返回true,否则返回false。 |
346
347**示例:**
348
349```ts
350const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg");
351console.info(`${uriInstance.checkHierarchical()}`); // true
352const uriInstance1 = new uri.URI("mailto:user@example.com");
353console.info(`${uriInstance1.checkHierarchical()}`); // false
354```
355
356### getQueryValue<sup>12+</sup>
357
358getQueryValue(key:string): string
359
360根据给定的查询关键词,从URI查询参数部分中提取出该关键词对应的第一个值,若查询参数中存在已编码过的内容,需将对应Key进行解码后获取Value。
361
362查询参数是出现在问号“?”之后的部分,它们由键值对组成,键和值之间用等号“=”连接,键值对之间使用与号“&”分隔。
363
364**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
365
366**系统能力:** SystemCapability.Utils.Lang
367
368**参数:**
369
370| 参数名 | 类型   | 必填 | 说明                    |
371| ------ | ------ | ---- | ----------------------- |
372| key    | string | 是   | 此URI查询参数的名称。 |
373
374**返回值:**
375
376| 类型   | 说明                          |
377| ------ | ----------------------------- |
378| string | 返回第一个此URI查询参数的值,若未找到对应值则返回null对象。 |
379
380**错误码:**
381
382以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
383
384| 错误码ID | 错误信息 |
385| -------- | -------- |
386| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
387
388**示例:**
389
390```ts
391const uriInstance = new uri.URI("https://www.com?param1=value1&param2=value2");
392console.info(uriInstance.getQueryValue("param1")); // value1
393let uriInstance1 = new uri.URI('https://www.zyy.ss?sa%3D=po%7E');
394console.info(uriInstance1.getQueryValue('sa=')) // po~
395console.info(uriInstance1.getQueryValue('abc')) // null
396```
397
398### addQueryValue<sup>12+</sup>
399
400addQueryValue(key:string, value:string): URI
401
402在当前URI的基础上添加查询参数,并创建一个新的URI,同时保持原有URI对象不变。
403
404**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
405
406**系统能力:** SystemCapability.Utils.Lang
407
408**参数:**
409
410| 参数名 | 类型   | 必填 | 说明                     |
411| ------ | ------ | ---- | ------------------------ |
412| key    | string | 是   | 需要增添查询参数的名称。 |
413| value  | string | 是   | 需要增添查询参数的值。   |
414
415**返回值:**
416
417| 类型 | 说明                             |
418| ---- | -------------------------------- |
419| [URI](#uri)  | 返回添加查询部分后的URI对象。 |
420
421**错误码:**
422
423以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
424
425| 错误码ID | 错误信息 |
426| -------- | -------- |
427| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
428
429**示例:**
430
431```ts
432const uriInstance = new uri.URI("https://www.test.com");
433const newRoute = uriInstance.addQueryValue("param1", "hello world");
434console.info(newRoute.toString()); // https://www.test.com?param1=hello%20world
435```
436
437### addSegment<sup>12+</sup>
438
439addSegment(pathSegment:string): URI
440
441对给定的字段进行编码并将其追加到当前URI的path字段中,并创建一个新的URI返回,同时保持原有URI对象不变。
442
443**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
444
445**系统能力:** SystemCapability.Utils.Lang
446
447**参数:**
448
449| 参数名      | 类型   | 必填 | 说明               |
450| ----------- | ------ | ---- | ------------------ |
451| pathSegment | string | 是   | 需要追加到路径部分的字段。 |
452
453**返回值:**
454
455| 类型 | 说明                             |
456| ---- | -------------------------------- |
457| [URI](#uri)  | 返回已追加字段的URI对象。 |
458
459**错误码:**
460
461以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
462
463| 错误码ID | 错误信息 |
464| -------- | -------- |
465| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
466
467**示例:**
468
469```ts
470const uriInstance = new uri.URI("http://www.test.com");
471const newRoute = uriInstance.addSegment("my image.jpg");
472console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
473```
474
475### addEncodedSegment<sup>12+</sup>
476
477addEncodedSegment(pathSegment:string): URI
478
479通过将已编码的字段追加到当前URI的path字段中,并创建一个新的URI返回,同时保持原有URI对象不变。
480
481**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
482
483**系统能力:** SystemCapability.Utils.Lang
484
485**参数:**
486
487| 参数名      | 类型   | 必填 | 说明               |
488| ----------- | ------ | ---- | ------------------ |
489| pathSegment | string | 是   | 需要追加到路径部分的编码字段。 |
490
491**返回值:**
492
493| 类型 | 说明                             |
494| ---- | -------------------------------- |
495| [URI](#uri)  | 返回已追加字段的URI对象。 |
496
497**错误码:**
498
499以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
500
501| 错误码ID | 错误信息 |
502| -------- | -------- |
503| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
504
505**示例:**
506
507```ts
508const uriInstance = new uri.URI("http://www.test.com");
509const newRoute = uriInstance.addEncodedSegment("my%20image.jpg");
510console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg
511```
512
513### getQueryNames<sup>12+</sup>
514
515getQueryNames(): string[]
516
517获取URI查询部分中所有不重复的键。查询参数是出现在问号“?”之后的部分,它们由键值对组成,键和值之间用等号“=”连接,键值对之间使用与号“&”分隔。
518
519**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
520
521**系统能力:** SystemCapability.Utils.Lang
522
523**返回值:**
524
525| 类型        | 说明                                |
526| ----------- | ----------------------------------- |
527| string[] | 返回此URI查询部分中所有不重复键。 |
528
529**示例:**
530
531```ts
532const uriInstance = new uri.URI("https://www.test.com?param1=value1&param2=value2");
533const paramNames = uriInstance.getQueryNames();
534console.info(Array.from(paramNames).toString()); // param1,param2
535```
536
537### getQueryValues<sup>12+</sup>
538
539getQueryValues(key:string): string[]
540
541获取此URI中查询参数的所有指定键对应值的集合。若查询参数中存在已编码过的内容,需将对应Key进行解码后获取Value。
542
543查询参数是出现在问号“?”之后的部分,它们由键值对组成,键和值之间用等号“=”连接,键值对之间使用与号“&”分隔。
544
545**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
546
547**系统能力:** SystemCapability.Utils.Lang
548
549**参数:**
550
551| 参数名 | 类型   | 必填 | 说明                    |
552| ------ | ------ | ---- | ----------------------- |
553| key    | string | 是   | 指定键的名称。 |
554
555**返回值:**
556
557| 类型     | 说明                                |
558| -------- | ----------------------------------- |
559| string[] | 返回此URI中查询参数内所有指定键对应值的集合,若没有找到则返回一个空字符串数组[]。 |
560
561**错误码:**
562
563以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
564
565| 错误码ID | 错误信息 |
566| -------- | -------- |
567| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
568
569**示例:**
570
571```ts
572const uriInstance = new uri.URI("https://www.test.com/search?query=name&query=my");
573console.info(uriInstance.getQueryValues("query").toString()); // name,my
574console.info(JSON.stringify(uriInstance.getQueryValues("abc"))); // []
575```
576
577### getBooleanQueryValue<sup>12+</sup>
578
579getBooleanQueryValue(key:string,defaultValue:boolean): boolean
580
581从URI查询参数中获取布尔类型的参数值。
582
583**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
584
585**系统能力:** SystemCapability.Utils.Lang
586
587**参数:**
588
589| 参数名       | 类型    | 必填 | 说明                                  |
590| ------------ | ------- | ---- | ------------------------------------- |
591| key          | string  | 是   | 要获取的查询参数的名称。               |
592| defaultValue | boolean | 是   | 返回的默认值。 |
593
594**返回值:**
595
596| 类型    | 说明                                                                   |
597| ------- | ---------------------------------------------------------------------- |
598| boolean | 如果指定的查询参数不存在,则返回默认值;查询参数对应第一个值为“false”或者“0”返回false,否则返回true。 |
599
600**错误码:**
601
602以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
603
604| 错误码ID | 错误信息 |
605| -------- | -------- |
606| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
607
608**示例:**
609
610```ts
611const uriInstance = new uri.URI("https://www.test.com/search?active=true");
612console.info(`${uriInstance.getBooleanQueryValue("active", false)}`); // true
613const uriInstance1 = new uri.URI("https://www.test.com/search");
614console.info(`${uriInstance1.getBooleanQueryValue("active", false)}`); // false
615const uriInstance2 = new uri.URI("https://www.test.com/search?active=aa&active=false");
616console.info(`${uriInstance2.getBooleanQueryValue("active", false)}`); // true
617const uriInstance3 = new uri.URI("https://www.test.com/search?active=0");
618console.info(`${uriInstance3.getBooleanQueryValue("active", true)}`); // false
619```
620
621### clearQuery<sup>12+</sup>
622
623clearQuery(): URI
624
625清除URI路径查询部分,并创建一个新的URI,同时保持原有URI对象不变。
626
627**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
628
629**系统能力:** SystemCapability.Utils.Lang
630
631**返回值:**
632
633| 类型 | 说明                                  |
634| ---- | ------------------------------------- |
635| [URI](#uri)  | 返回一个已被清除查询部分的URI对象。 |
636
637**示例:**
638
639```ts
640const uriInstance = new uri.URI("https://www.test.com?param1=value1");
641console.info(uriInstance.clearQuery().toString()); // https://www.test.com
642```
643
644### getLastSegment<sup>12+</sup>
645
646getLastSegment(): string
647
648获取此URI路径的最后一个段。每个段代表路径中的一个部分,通常通过“/”来进行分隔。对于以斜杠结尾的或者没有路径的不代表段。
649
650**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
651
652**系统能力:** SystemCapability.Utils.Lang
653
654**返回值:**
655
656| 类型 | 说明                          |
657| ---- | ----------------------------- |
658| string  | 返回此URI路径中的最后一个段。 |
659
660**示例:**
661
662```ts
663const uriInstance = new uri.URI("content://com.test.provider/files/image.jpg");
664console.info(uriInstance.getLastSegment()); // image.jpg
665```
666
667### getSegment<sup>12+</sup>
668
669getSegment(): string[]
670
671获取此URI路径中的所有段。
672
673**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
674
675**系统能力:** SystemCapability.Utils.Lang
676
677**返回值:**
678
679| 类型     | 说明                        |
680| -------- | --------------------------- |
681| string[] | 返回此URI路径中的所有段。 |
682
683**示例:**
684
685```ts
686const uriInstance = new uri.URI("http://www.test.com/path/to/image.jpg");
687console.info(uriInstance.getSegment().toString()); // path,to,image.jpg
688```
689
690### createFromParts<sup>12+</sup>
691
692createFromParts(scheme: string, ssp: string, fragment: string): URI
693
694根据提供的协议、方案特定部分以及片段部分创建一个新的URI。
695
696**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。
697
698**系统能力:** SystemCapability.Utils.Lang
699
700**参数:**
701
702| 参数名   | 类型   | 必填 | 说明                            |
703| -------- | ------ | ---- | ------------------------------- |
704| scheme   | string | 是   | 此URI协议部分。               |
705| ssp      | string | 是   | 此URI的方案特定部分。 |
706| fragment | string | 是   | 此URI的片段部分。片段部分是URI中的一个特定部分,即“#”符号后面的部分。             |
707
708**返回值:**
709
710| 类型 | 说明                                              |
711| ---- | ------------------------------------------------- |
712| [URI](#uri)  | 返回创建的URI对象。 |
713
714**错误码:**
715
716以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
717
718| 错误码ID | 错误信息 |
719| -------- | -------- |
720| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
721
722**示例:**
723
724```ts
725const uriInstance = uri.URI.createFromParts("mailto", "no body", "top");
726console.info(uriInstance.toString()); // mailto:no%20body#top
727```
728
729### equals<sup>(deprecated)</sup>
730
731equals(other: URI): boolean
732
733判断此URI是否与其他URI对象相等。
734
735> **说明:**
736>
737> 从API version 8开始支持,从API version 9开始废弃,建议使用[equalsTo<sup>9+</sup>](#equalsto9)替代。
738
739**系统能力:** SystemCapability.Utils.Lang
740
741**参数:**
742
743| 参数名 | 类型 | 必填 | 说明 |
744| -------- | -------- | -------- | -------- |
745| other | [URI](#uri) | 是 | 需要比较的URI对象。 |
746
747**返回值:**
748
749| 类型 | 说明 |
750| -------- | -------- |
751| boolean | 返回true表示相等,否则返回false。 |
752
753**示例:**
754
755```ts
756const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
757const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
758uriInstance.equals(uriInstance1); // true
759```