Android - 统一资源标识符 Uri

一、概念

URI(Uniform Resource Identifier)统一资源标识符,用于标识资源的字符串(如图片、网页、文件、应用等)。

1.1 与 URL 的区别

URL(统一资源定位符)是 URI(统一资源标识符)的子集,即一种特定形式的 URI,它一定包含了网络访问协议和位置(https://www.baidu.com 或 ftp://files.server.com)。因此 URI 类作为更基础的解析,而 URL 类则专注于网络操作。

 1.2 结构

https://www.baidu.com:8080/hello/word.html?id=123&name=sb#haha
scheme 协议http网页、file文件、content内容提供者、tel电话.....
host 主机www.baidu.com。
port 端口8080。
path 路径端口之后问号之前的部分,/hello/word.html。
query 参数问号之后的部分,id=123&name=sb。
fragment 片段资源内部某个部分,以#开头,haha。
//转换成URI,编译器推荐使用KTX方式
//val uri = Uri.parse("https://www.baidu.com:8080/hello/word.html?id=123&name=sb#haha")
val uri = "https://www.baidu.com:8080/hello/word.html?id=123&name=sb".toUri()
println(uri.scheme)   //打印:https
println(uri.host)     //打印:www.baidu.com
println(uri.port)     //打印:8080
println(uri.path)     //打印:/hello/word.html
println(uri.query)    //打印:id=123&name=sb
println(uri.fragment) //打印:haha
//主机+端口
println(uri.authority)    //打印:www.baidu.com:8080
//主机+端口+路径+参数
println(uri.schemeSpecificPart)    //打印://www.baidu.com:8080/hello/word.html?id=123&name=sb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值