URI.

本文介绍了统一资源标识符(URI)的基本概念,包括其特性如统一性、所指资源的定义以及URI与URL、URN之间的区别。强调了URI在不同场景下的一致性和可扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Overview of URI
URI are characterized by the following definitions:
Uniform
Uniformity provides several benefits:
  it allows different types of resource identifiers to be used in the same context, even when the mechanisms used to access those resources may differ;
  
it allows uniform semantic interpretation of common syntactic conventions across different types of resource identifiers.
  
it allows introduction of new types of resource identifiers without interfering with the way that existing identifiers are used.
  
it allows the identifiers to be reused in many different contexts.
Resource
  A resource can be anything that has identity.including an electronic document, an image, a service (e.g., "today's weather report for Los Angeles"), and a collection of other resources.

URI, URL, and URN
  A URI can be further classified as a locator, a name, or both.  The
   term "Uniform Resource Locator" (URL) refers to the subset of URI
   that identify resources via a representation of their primary access
   mechanism (e.g., their network "location"), rather than identifying
   the resource by name or by some other attribute(s) of that resource.
   The term "Uniform Resource Name" (URN) refers to the subset of URI
   that are required to remain globally unique and persistent even when
   the resource ceases to exist or becomes unavailable.


ref:http://www.ietf.org/rfc/rfc2396.txt
 
 
 
 
 

转载于:https://www.cnblogs.com/listened/p/3774038.html

### Android 中 Uri.Builder 的使用方法及示例 `Uri.Builder` 是 Android 提供的一个用于构建 URI 对象的类。它允许开发者逐步设置 URI 的各个组成部分,最终生成完整的 URI 实例。以下是关于 `Uri.Builder` 的详细介绍以及其常见用法。 #### 1. 基本概念 URI(Uniform Resource Identifier)由以下几个主要部分组成[^3]: - **Scheme**: 表示协议类型,例如 `http`, `https`, 或者自定义协议。 - **Authority**: 通常指主机名或者资源所在的服务器地址。 - **Path**: 资源的具体路径。 - **Query Parameters**: 键值对形式的附加参数,用于传递额外的信息。 - **Fragment**: 页面内的锚点或者其他片段标识符。 在 Android 开发中,可以通过 `Uri.Builder` 动态创建这些组件并组合成一个有效的 URI。 --- #### 2. 使用方法 `Uri.Builder` 类提供了多种方法来设置 URI 各个部分的内容: | 方法名称 | 描述 | |------------------|----------------------------------------------------------------------| | `.scheme(String)` | 设置 URI 的 scheme 部分,例如 `"http"` 或 `"content"`. | | `.authority(String)` | 设置 authority 部分,通常是主机名或服务端地址。 | | `.appendPath(String)` | 添加一个新的路径段到当前路径后面。 | | `.encodedPath(String)` | 替换整个路径部分为指定字符串(已编码)。 | | `.appendQueryParameter(String key, String value)` | 添加键值对作为查询参数。 | | `.build()` | 构建并返回最终的 `Uri` 对象。 | --- #### 3. 示例代码 以下是一个简单的例子,展示如何使用 `Uri.Builder` 创建一个包含 Scheme、Authority、Path 和 Query 参数的 URI: ```java import android.net.Uri; public class UriBuilderExample { public static void main(String[] args) { // 初始化 Uri.Builder Uri.Builder builder = new Uri.Builder(); // 设置 Scheme 和 Authority builder.scheme("https").authority("www.example.com"); // 添加 Path builder.appendPath("api"); builder.appendPath("v1"); builder.appendPath("data"); // 添加 Query Parameter builder.appendQueryParameter("key", "value"); builder.appendQueryParameter("page", "1"); // 构建最终的 Uri Uri uri = builder.build(); // 输出结果 System.out.println(uri.toString()); // 结果: https://www.example.com/api/v1/data?key=value&page=1 } } ``` 在这个例子中,我们依次设置了 Scheme (`https`)、Authority (`www.example.com`)、Path (`/api/v1/data`) 和两个 Query 参数 (`key=value` 和 `page=1`)。 --- #### 4. 特殊情况处理 当需要访问本地资源时,可以利用 `ContentResolver.SCHEME_ANDROID_RESOURCE` 来构造 URI。例如,在引用[1]中提到的方法可用于获取存储在 `/res/raw` 文件夹中的 MP3 文件的 URI[^1]: ```java String filename = "example.mp3"; Uri rawResourceUri = Uri.parse( ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + "/" + getPackageName() + "/raw/" + filename); System.out.println(rawResourceUri.toString()); // 结果类似于: content://com.package.name/raw/example.mp3 ``` 对于更复杂的场景,比如解析来自通知回复的数据,则可参考引用[2]中的实现逻辑[^2]。通过调用 `RemoteInput.getResultsFromIntent(intent)` 获取用户输入,并将其嵌入至目标 URI 的查询参数中。 --- #### 5. 注意事项 - 如果 URI 包含特殊字符(如空格),应确保它们被正确转义。`Uri.Builder` 自动处理大多数常见的 URL 编码需求。 - 当拼接多个路径段时,推荐使用 `.appendPath(String)` 方法而非手动连接字符串,这样能避免潜在错误。 - 查询参数会自动追加到已有参数之后,因此无需担心重复添加相同键的情况。 --- ### 总结 `Uri.Builder` 是一种灵活且强大的工具,适用于动态生成各种类型的 URI 地址。无论是网络请求还是内部资源定位,都可以借助该 API 完成复杂 URI 的组装工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值