(Transfer) Uri Detailed Explanation - Uri Structure and Code Extraction

Foreword: Still no foreword...


Related blogs:
1. "Uri Detailed Explanation - Uri Structure and Code Extraction"
2. "Uri Detailed Explanation II - Starting APP and Notification through Custom Uri External"

 

The last few articles told you about ContentProvider, which uses Uri. Maybe many students are not familiar with what Uri is and how it came from. Today, I will tell you about Uri in detail.

1. URI and Uri
You may often see that in development, sometimes it is URI, sometimes it is Uri, what is going on?

What is the difference and connection between two classes with such similar names?

1. The packages to which they belong are different. The URI location is in java.net.URI, apparently a class provided by Java. The Uri location is in android.net.Uri, which is a class provided by Android. So it can be initially judged that Uri is an "extension" of URI to meet the needs of the Android system.
2. The role is different. The URI class represents a URI (this URI is not a class, but its original meaning: Universal Resource Identifier - Uniform Resource Identifier) ​​instance. The Uri class is an immutable URI reference consisting of a URI and some fragments, the URI is followed by a "#". Build and convert URI references. And the Uri class is not sensitive to invalid behavior. There is no corresponding behavior defined for invalid input. If not specified otherwise, it will return garbage instead of throwing an exception.
can not read it? It doesn't matter, just know this: Uri is developed by Android, which extends some functions of URI in JAVA to be specifically applicable to Android development, so when developing, you can only use the Uri provided by Android;

2. Uri structure
(1), basic form:
[html] view plain copy
[scheme:]scheme-specific-part[#fragment]
This is divided into three parts:
scheme, scheme-specific-part, fragment
(2), further Divide:
If it is further divided, it looks like this

[html] view plain copy
[scheme:][//authority][path][?query][#fragment]
There are the following rules:
There can be multiple paths, each connected with /, such as
scheme:// The authority/path1/path2/path3?query#fragment
query parameter can have the corresponding value or not. If the corresponding value is represented by =, for example:
scheme://authority/path1/path2/path3?id = 1#fragment, there is a parameter id, its value is 1
There can be multiple query parameters, each with & connect
scheme://authority/path1/path2/path3?id = 1&name = mingming&old#fragment
There are three parameters here :
parameter 1: id, its value is: 1
parameter 2: name, its value is: mingming
parameter 3: old, there is no assignment to it, so its value is null
in android, except scheme and authority are required , several other paths, queries, fragments, each of them can be optional or not, but the order cannot be changed, for example:
where "path" is optional: scheme://authority?query#fragment
where "path" and "query" is not required: scheme://authority#fragment
where "query" and "Fragment" but not at all: scheme://authority/path
"path", "query", "fragment" do not need: scheme://authority
, etc...
(3), the ultimate division
of authority, which can be divided into the form of host:port, that is, after dividing it again, it looks like this:

[html] view plain copy
[scheme:][//host:port][path][?query][#fragment]
So this is the most finely divided form, where host:port is separated by a colon, and before the colon is host , the port after the colon;
3. The example
After the above explanation, I think everyone has an understanding of the structure of the Uri. Let's take an example to see the identification method of each part.

[html] view plain copy
[scheme:]scheme-specific-part[#fragment]
[scheme:][//authority][path][?query][#fragment]
[scheme:][//host:port] [path][?query][#fragment]
List these three Uri forms first, so that everyone can compare;
match each part against the following Uri string:
[java] view plain copy
http://www.java2s .com:8080/yourpath/fileName.htm?stove=10&path=32&id=4#harvic
scheme: Matching the above two Uri standard forms, it is easy to see that the part before: is the scheme, so the Uri string is The scheme is: http
scheme-specific-part: It is easy to see that scheme-specific-part is the part contained between scheme and fragment, that is, [//authority][path][?query] which includes the second part. Several small parts, the scheme-specific-part of this Uri string is: //www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id=4, pay attention to bring //, because Except for [scheme:] and [#fragment], all parts are scheme-specific-parts, including the first // of course;
fragment: This is easier to see, because the part separated by # at the end is fragment, so The fragment of this Uri is: harvic
The following is the splitting of the scheme-specific-part;
in the scheme-specific-part, the front-end part is the authority, ? The latter part is the query, and the middle part is the path
authority: it is easy to see that the latest part of the scheme-specific-part is: www.java2s.com:8080
query: in the scheme-specific-part, ? The latter part is: stove=10&path=32&id=4
path: in **query:** in the scheme-specific-part, except for the authority and query, the rest are part of the path: /yourpath/fileName.htm
and because of the authority One step can be divided into host:port form, where host:port is separated by a colon, before the colon is the host, after the colon is the port, so:
host:www.java2s.com
port:8080
Fourth, code extraction
We explain the above through examples It is a way to identify more parts of Uri with the naked eye, but how to extract it in the code. Let's take a look at the interface for extracting each part in Uri, still take the Uri string above as an example:

[java] view plain copy
http://www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id=4#harvic
getScheme() : Get the scheme string part in Uri, here ie, http
getSchemeSpecificPart(): Get the scheme-specific-part: part in Uri, here is: //www.java2s.com:8080/yourpath/fileName.htm?
getFragment(): Get the Fragment part in Uri, ie harvic
getAuthority (): Get the Authority part in Uri, namely www.java2s.com:8080
getPath(): Get the path part in Uri, namely /yourpath/fileName.htm
getQuery(): Get the query part in Uri, namely store=10&path= 32&id=4
getHost(): Get the Host string in the Authority, namely www.java2s.com
getPost(): Get the Port string in the Authority, that is, 8080
There are two other commonly used ones: getPathSegments(), getQueryParameter(String key)
List<String> getPathSegments(): Above our getPath() is to get the entire path part: /yourpath/fileName.htm, the function of getPathSegments() is to extract the strings of each part of the Path in turn, and use the string array form output. Take the Uri above as an example:
[java] view plain copy
String mUriStr = "http://www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id=4#harvic";
Uri mUri = Uri .parse(mUriStr);
List<String> pathSegList = mUri.getPathSegments();
for (String pathItem:pathSegList){
Log.d("qijian","pathSegItem:"+pathItem);
}
The typed list is:

getQueryParameter(String key): In the above, we get the entire query field through getQuery(): stove=10&path=32&id=4, the function of getQueryParameter(String key) is to pass the string of a key in the path and return its corresponding value .
[java] view plain copy
String mUriStr = "http://www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id#harvic";
mUri = Uri.parse(mUriStr);
Log.d( tag,"getQueryParameter(\"stove\"):"+mUri.getQueryParameter("stove"));
Log.d(tag,"getQueryParameter(\"id\"):"+mUri.getQueryParameter("id") );
Note that I changed the string slightly and removed the value of id in the query! ! ! ! ! Then see what you get by getting its value via getQueryParameter("id") !
The result is as follows:

It can be seen that in the path, even if it is not allowed to assign a value to a KEY, when using getQueryParameter() to obtain the value corresponding to the KEY, the obtained value is null;

V. Extension
1. Absolute URI and relative URI
Absolute URI: The complete format starting with the scheme component, such as http://fsjohnhuang.cnblogs.com. Indicates that the resource is referenced in a way that is independent of the context in which the identity appears.
Relative URI: a non-complete format that does not start with a scheme component, such as fsjohnhuang.cnblogs.com. Indicates that the resource is referenced in a way that is dependent on the context in which the dependent identifier appears.

2. Opaque URI and Hierarchical URI
Opaque URI: The scheme-specific-part component does not start with a forward slash (/), such as mailto:[email protected]. Since opaque URIs do not need to be decomposed, there is no validation of scheme-specific-part components.
Hierarchical URI: scheme-specific-part components start with a forward slash (/), such as http://fsjohnhuang.com.


For more knowledge about this extension and normalization, resolution, and relativization, see: "Java Magic Hall: URI, URL (including URL Protocol Handler) and URN"

 

The following source code demonstrates the usage and results of each extraction function;

If this article helps you, remember to pay attention

Source code download address: http://download.csdn.net/detail/harvic880925/8539679

Please respect the copyright of the original creator, please indicate the source for reprinting: http://blog.csdn.net/harvic880925/article/details/44679239 Thank you

If you like my articles, then you will like my WeChat public account more, and I will regularly push the latest articles from bloggers and collect dry goods to share with you (once a week)

 


Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/harvic880925/article/details/44679239

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613053&siteId=291194637
Recommended