js-apis-uri.md 7.4 KB
Newer Older
1
# @ohos.uri (URI字符串解析)
Z
zengyawen 已提交
2

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


## 导入模块

J
jiangkai43 已提交
10
```ts
Z
zengyawen 已提交
11 12 13 14 15 16 17
import uri from '@ohos.uri'  
```

## URI

### 属性

18
**系统能力:** SystemCapability.Utils.Lang
Z
zengyawen 已提交
19

20
| 名称 | 类型 | 可读 | 可写 | 说明 |
Z
zengyawen 已提交
21 22
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | 是 | 否 | 获取URI 的协议部分。 |
23
| userInfo | string | 是 | 否 | 获取 URI 的用户信息部分。 |
Z
zengyawen 已提交
24 25 26 27 28 29 30 31
| host | string | 是 | 否 | 获取 URI 的主机名部分(不带端口)。 |
| port | string | 是 | 否 | 获取 URI 的端口部分。 |
| path | string | 是 | 否 | 获取 URI 的路径部分。 |
| query | string | 是 | 否 | 获取 URI 的查询部分。 |
| fragment | string | 是 | 否 | 获取 URI 的片段部分 |
| authority | string | 是 | 否 | 获取此URI的解码权限组件部分。 |
| ssp | string | 是 | 否 | 获取URI的解码方案特定部分。 |

Z
zhangyouyou 已提交
32 33 34 35 36 37
### 命名规则

**命名形式:**

标准uri定义由以下三个部分组成
[scheme:]scheme-specific-part[#fragment]
38
- scheme: 协议名,根据需要填写。例如http、https、ftp、datashare、dataability等。
Z
zhangyouyou 已提交
39 40 41 42 43 44 45 46 47 48 49
- scheme-specific-part: URI的特定解码方案特定部分,由[//][authority][path][?query]组成,根据需要填写。
    - authority: URI的解码权限组件部分。由[userinfo@]host[:port]组成,根据需要填写。
        - userinfo: 用户信息,根据需要填写。
        - host: 服务器的主机名部分,当authority存在时,此项必填。
        - port: 服务器端口,根据需要填写。
    - path: 路径信息,根据需要填写。
    - query: 查询部分,根据需要填写。
- fragment: 片段部分,根据需要填写。

**URI示例:**

J
jiangkai43 已提交
50
```ts
Z
zhangyouyou 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
console.log(result1.host) // ftp.aaa.bbb.ccc
console.log(result1.fragment) // null
console.log(result1.path) // /dddd/eee.txt
console.log(result1.scheme) // ftp
console.log(result1.userInfo) // null
console.log(result1.port) // -1
console.log(result1.query) // null

const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
console.log(result2.host) // spinaltap.micro.umn.edu
console.log(result2.fragment) // fragment
console.log(result2.path) // /dddd/eee.txt
console.log(result2.scheme) // ftp
console.log(result2.userInfo) // null
console.log(result2.port) //-1
console.log(result2.query) // null

const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
console.log(result3.host) // null
console.log(result3.fragment) // null
console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
console.log(result3.scheme) // datashare
console.log(result3.userInfo) // null
console.log(result3.port) // -1
console.log(result3.query) // null

const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
console.log(result4.host) // host
console.log(result4.fragment) // fragment
console.log(result4.path) // /directory/file
console.log(result4.scheme) // https
console.log(result4.userInfo) // username:password
console.log(result4.port) // 8080
console.log(result4.query) // foo=1&bar=2

const result5 = new uri.URI("dataability:///com.example.DataAbility");
console.log(result5.host) // null
console.log(result5.fragment) // null
console.log(result5.path) // /com.example.DataAbility:
console.log(result5.scheme) // dataability
console.log(result5.userInfo) // null
console.log(result5.port) // -1
console.log(result5.query) // null
```
Z
zengyawen 已提交
96 97 98 99 100 101 102

### constructor

constructor(uri: string)

constructor是URI的构造函数。

Z
zengyawen 已提交
103 104
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
105
**参数:**
Z
zengyawen 已提交
106

107 108 109
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| uri | string | 是 | 入参对象。 |
Z
zengyawen 已提交
110

111 112 113 114 115 116 117 118
**错误码:**

以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200002 | Invalid uri string. |

Z
zengyawen 已提交
119 120
**示例:**

J
jiangkai43 已提交
121
```ts
Z
zengyawen 已提交
122
let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
J
jiangkai43 已提交
123
new uri.URI(mm);
Z
zengyawen 已提交
124
```
J
jiangkai43 已提交
125 126
```ts
new uri.URI('https://username:password@host:8080');
Z
zengyawen 已提交
127
```
Z
zengyawen 已提交
128 129 130 131 132 133


### toString

toString(): string

Z
zengyawen 已提交
134 135
**系统能力:** SystemCapability.Utils.Lang

136
返回适用于URI中的查询字符串。
Z
zengyawen 已提交
137

Z
zengyawen 已提交
138 139 140 141 142
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| string | 返回网址的字符串序列化。 |
Z
zengyawen 已提交
143

Z
zengyawen 已提交
144 145
**示例:**

J
jiangkai43 已提交
146
```ts
Z
zengyawen 已提交
147
const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
B
bi-hu 已提交
148
let result1 = result.toString();
Z
zengyawen 已提交
149
```
Z
zengyawen 已提交
150 151


152
### equals<sup>(deprecated)</sup>
153

Z
zengyawen 已提交
154 155 156 157
equals(other: URI): boolean

判断此URI是否与其他URI对象相等。

158 159 160 161
> **说明:**
>
> 从API version 8开始支持,从API version 9开始废弃,建议使用[equalsTo<sup>9+</sup>](#equalsto9)替代。

Z
zengyawen 已提交
162 163
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
164
**参数:**
Z
zengyawen 已提交
165

Z
zengyawen 已提交
166 167 168
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | 是 | 需要比较的URI对象。 |
Z
zengyawen 已提交
169

Z
zengyawen 已提交
170 171 172 173 174 175 176 177
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回true表示相等,否则返回false。 |

**示例:**

J
jiangkai43 已提交
178
```ts
179 180
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
Z
zengyawen 已提交
181 182
uriInstance.equals(uriInstance1);
```
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
### equalsTo<sup>9+</sup>

equalsTo(other: URI): boolean

判断此URI是否与其他URI对象相等。

**系统能力:** SystemCapability.Utils.Lang

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | 是 | 需要比较的URI对象。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回true表示相等,否则返回false。 |

**示例:**

J
jiangkai43 已提交
205
```ts
206 207
const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
B
bi-hu 已提交
208
let result = uriInstance.equalsTo(uriInstance1);
209
```
Z
zengyawen 已提交
210 211 212

### checkIsAbsolute

Z
zengyawen 已提交
213 214
checkIsAbsolute(): boolean

215
判断此URI是否为绝对URI(是否定义了scheme组件)。
Z
zengyawen 已提交
216

Z
zengyawen 已提交
217 218
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
219
**返回值:**
Z
zengyawen 已提交
220

Z
zengyawen 已提交
221 222
| 类型 | 说明 |
| -------- | -------- |
223
| boolean | 如果是绝对URI返回true,否则返回false。|
Z
zengyawen 已提交
224 225 226

**示例:**

J
jiangkai43 已提交
227
```ts
Z
zengyawen 已提交
228
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
229
console.log(`${uriInstance.checkIsAbsolute()}`); // true
D
du-wenboboo 已提交
230
const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
231
console.log(`${uriInstance1.checkIsAbsolute()}`); // false
Z
zengyawen 已提交
232
```
Z
zengyawen 已提交
233 234 235 236 237 238 239 240


### normalize

normalize(): URI

规范化此URI的路径。

Z
zengyawen 已提交
241 242
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
243 244 245 246 247 248 249
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| URI | 返回一个path被规范化后的URI对象。 |

**示例:**
250

J
jiangkai43 已提交
251
```ts
Z
zengyawen 已提交
252
const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
D
du-wenboboo 已提交
253
console.log(uriInstance.path); // /path/path1/../path2/./path3
Z
zengyawen 已提交
254
let uriInstance1 = uriInstance.normalize();
D
du-wenboboo 已提交
255
console.log(uriInstance1.path); // /path/path2/path3
Z
zengyawen 已提交
256
```