Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference and relationship between URI and Uri classes in Android

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the differences and connections between URI and Uri classes in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the difference and relationship between URI and Uri classes in Android?"

1. URI and Uri

You may often see that in development, sometimes it is URI, sometimes it is Uri, what is this?

What is the difference and relationship between the two classes whose names are so similar?

1. It belongs to a different bag. URI is located in java.net.URI, which is obviously a class provided by Java. Uri, which is located in android.net.Uri, is a class provided by Android. Therefore, it can be preliminarily judged that Uri is the "extension" of URI to meet the needs of Android system.

two。 The effect is different. The URI class represents an instance of URI (this URI is not a class, but rather its original meaning: the universal resource identifier-- Uniform Resource Identifier). The Uri class is an immutable URI reference, including a URI and some fragments, with URI followed by "#". Create and transform URI references. And the Uri class is not sensitive to invalid behavior, there is no behavior defined for invalid input, and if it is not otherwise specified, it will return garbage instead of throwing an exception.

I don't understand? It doesn't matter, just know this: Uri is developed by Android and extends some functions of URI in JAVA to specifically apply to Android development, so when developing, you can only use 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 division:

If you divide it further, it looks like this.

[html] view plain copy

[scheme:] [/ / authority] [path] [? query] [# fragment]

There are the following rules:

There can be multiple path, each using / connecting, such as

Scheme://authority/path2/path3/path4?query#fragment

The query parameter can have a corresponding value or not. If the corresponding value is indicated by =, for example:

Scheme://authority/path2/path3/path4?id = 1#fragment, where there is a parameter id, whose value is 1

There can be multiple query parameters, each of which is connected with

Scheme://authority/path2/path3/path4?id = 1&name = mingming&old#fragment

Here are three parameters:

Parameter 1:id, whose value is: 1

Parameter 2:name, whose value is: mingming

Parameter 3:old, which is not assigned a value, so its value is null

In android, in addition to scheme, authority is a must, the other several path, query, fragment, each of which can be selected or not, but the order cannot be changed, for example:

Among them, "path" can not be: scheme://authority?query#fragment.

Neither "path" nor "query" can be used: scheme://authority#fragment

Neither "query" nor "fragment" can be used: scheme://authority/path

"path", "query", "fragment" do not want: scheme://authority

Wait a minute...

(3) Ultimate division

Among them, authority can be divided into the form of host:port, that is, after re-division, it is as follows:

[html] view plain copy

[scheme:] [/ / host:port] [path] [? query] [# fragment]

So this is the finest form of division, where host:port is separated by colons, host before colons and port after colons

III. Examples

After the above explanation, we must have some understanding of the structure of the Uri. Let's take a look at the identification of each part with an example.

[html] view plain copy

[scheme:] scheme-specific-part [# fragment]

[scheme:] [/ / authority] [path] [? query] [# fragment]

[scheme:] [/ / host:port] [path] [? query] [# fragment]

First list these three forms of Uri so that you can compare them.

Match the parts 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: comparing the above two Uri standard forms, it is easy to see that the first part is scheme, so the sheme of this Uri string is: http

Scheme-specific-part: it is easy to see that scheme-specific-part is the part contained between scheme and fragment, that is, the small parts [/ / authority] [path] [? query] that include the second part. The scheme-specific-part of this Uri string is: / / www.java2s.com:8080/yourpath/fileName.htm?stove=10&path=32&id=4, note that you should take / /, because except for [scheme:] and [# fragment] parts are all scheme-specific-part. Of course, it includes the front / /

Fragment: this is easier to see, because the last part separated by # is fragment, so the fragment of this Uri is: harvic

Here's how to split the scheme-specific-part.

In scheme-specific-part, the front part is authority,?. The latter part is query, and the middle part is path.

Authority: it's easy to see that the newest part of scheme-specific-part is: www.java2s.com:8080

Query: in scheme-specific-part,? The latter part is: stove=10&path=32&id=4

Path: in * * query:** in scheme-specific-part, except for authority and query, the rest are path parts: / yourpath/fileName.htm

Because authority can be further divided into host:port form, where host:port is separated by a colon, host before the colon and port after the colon, so:

Host:www.java2s.com

Port:8080

IV. Code extraction

The above example explains how 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 interfaces that extract various parts in Uri, and still take the above Uri string 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 of the Uri, in this case, http

GetSchemeSpecificPart (): get the scheme-specific-part: part of the Uri. Here is: / / www.java2s.com:8080/yourpath/fileName.htm?

GetFragment (): get the Fragment part of Uri, that is, harvic

GetAuthority (): get the Authority part of Uri, that is, www.java2s.com:8080

GetPath (): get the path part of the Uri, namely / yourpath/fileName.htm

GetQuery (): get the query part of Uri, that is, stove=10&path=32&id=4

GetHost (): gets the Host string in Authority, namely www.java2s.com

GetPost (): gets the Port string in Authority, that is, 8080

There are also two commonly used ones: getPathSegments () and getQueryParameter (String key)

List

< String>

GetPathSegments (): our getPath () above fetches the whole part of the path: the function of / yourpath/fileName.htm,getPathSegments () is to extract the strings of each part of the Path in turn and output them as an array of strings. 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 pathSegList = mUri.getPathSegments ()

For (String pathItem:pathSegList) {

Log.d ("qijian", "pathSegItem:" + pathItem)

}

The typed list is:

GetQueryParameter (String key): above we get the entire query field through getQuery (): the function of stove=10&path=32&id=4,getQueryParameter (String key) is to return its corresponding value by passing a string of Key in the path.

[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"))

Notice that I changed the string slightly to remove the value of id from query! Then see what you get when you get its value through getQueryParameter ("id")!

The results are as follows:

As you can see, in path, even if it is not allowed to assign a value to a KEY, when you use getQueryParameter () to get the corresponding value of the KEY, you get the null

Extension 1, absolute URI and relative URI

Absolute URI: a complete format that starts with a scheme component, such as http://fsjohnhuang.cnblogs.com. Indicates that resources are referenced in a way that is independent of the environment in which the identity appears.

Relative URI: an incomplete 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 environment in which the dependent identity occurs.

2. Opaque URI and layered URI

Opaque URI:scheme-specific-part components do not start with a forward slash (/), such as mailto:fsjohnhuang@xxx.com. Because opaque URI does not require decomposition, 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.

At this point, I believe you have a deeper understanding of "what is the difference and relationship between URI and Uri classes in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report