Android - Uri

一、什么是URI

URI(Uniform Resource Identifier,统一资源标识符)以字符串来表示某种资源的统一资源标识。
From Wiki:

统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串。 该种标识允许用户对网络中(一般指万维网)的资源通过特定的协议进行交互操作。URI由包括确定语法和相关协议的方案所定义。

我看:

从Uniform 来看,url并非只是安卓的东西,是在整个计算机体系中通用的
Resource Identifier,资源标识,希望通过它来寻找指定的资源

定义在RFC 2396 - http://www.faqs.org/rfcs/rfc2396.html

RFC指 征求修正意见书(英语:Request For Comments,缩写为RFC),是由互联网工程任务组(IETF)发布的一系列备忘录。文件收集了有关互联网相关信息,以及UNIX和互联网社区的软件文件,以编号排定。目前RFC文件是由互联网协会(ISOC)赞助发布。

以下是一些我们常见Uri

   ftp://ftp.is.co.za/rfc/rfc1808.txt
      -- ftp scheme for File Transfer Protocol services

 gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles
      -- gopher scheme for Gopher and Gopher+ Protocol services

   http://www.math.uio.no/faq/compression-faq/part1.html
      -- http scheme for Hypertext Transfer Protocol services

   mailto:mduerst@ifi.unizh.ch
      -- mailto scheme for electronic mail addresses

   news:comp.infosystems.www.servers.unix
      -- news scheme for USENET news groups and articles

   telnet://melvyl.ucop.edu/
      -- telnet scheme for interactive services via the TELNET Protocol

二、Uri结构

(1)、基本形式:
[scheme:]scheme-specific-part[#fragment]
这里分为三部分:
scheme、scheme-specific-part、fragment

(2)、进一步划分:
[scheme:][//authority][path][?query][#fragment]

(3)、终极划分
其中authority,又可以分为host:port的形式,即再次划分后是这样的:
[scheme:][//host:port][path][?query][#fragment]

详情见:http://blog.csdn.net/harvic880925/article/details/44679239

三、安卓中的URI

简介

  • java.net.URI – A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC 2396.

  • android.net.Uri – Immutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a ‘#’. Builds and parses URI references which conform to RFC 2396.

简单来说

java.net.URI,显然是Java提供的一个统一资源标识符的实现类。
android.net.Uri,是由Android提供的一个统一资源标识符的抽象类。Uri与java.net.URI并无关系。

 T
 he "four main components" of a hierarchical URI consist of
    <scheme>://<authority><path>?<query>#fragment

private final String scheme; // can be null
private final Part authority;
private final PathPart path;
private final Part query;
private final Part fragment;

  if (scheme.equalsIgnoreCase("tel") || scheme.equalsIgnoreCase("sip")
                    || scheme.equalsIgnoreCase("sms") || scheme.equalsIgnoreCase("smsto")
                    || scheme.equalsIgnoreCase("mailto")) {

Uri的分类

  • Hierarchical
    层级式 like “http://google.com
    特征,以”/”进行层级分割
  • Opaque
    非层级式 like “mailto:nobody@google.com”.
    特征,没有”/”进行层级分割
  • Relative
    不包含明确的scheme
  • Absolute
    包含明确的scheme

Uri类图

Uri类图

常用Uri的生成方式

1.使用字符串进行生成

 /**
     * Creates a Uri which parses the given encoded URI string.
     *
     * @param uriString an RFC 2396-compliant, encoded URI
     * @throws NullPointerException if uriString is null
     * @return Uri for this given uri string
     */
    public static Uri parse(String uriString) {
        return new StringUri(uriString);
    }

2.从文件生成

    /**
     * Creates a Uri from a file. The URI has the form
     * "file://<absolute path>". Encodes path characters with the exception of
     * '/'.
     *
     * <p>Example: "file:///tmp/android.txt"
     *
     * @throws NullPointerException if file is null
     * @return a Uri for the given file
     */
    public static Uri fromFile(File file) {
        if (file == null) {
            throw new NullPointerException("file");
        }

        PathPart path = PathPart.fromDecoded(file.getAbsolutePath());
        return new HierarchicalUri(
                "file", Part.EMPTY, path, Part.NULL, Part.NULL);
    }

Uri的使用

URI定义文档:http://www.faqs.org/rfcs/rfc2396.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值