Android Study Notes 20. Use ContentProvider for data sharing (2). URI... tool class, androidhttp tool class
1. UriMatcher and ContentUris toolsUriMatcher
1. function overview when developing ContentProvider, the first parameter of the query (), insert (), delete (), and update () methods is the Uri parameter, this parameter is passed in when ContentResolver calls these methods. The instance in the previous blog does not actually operate on the data, so ContentProvider does not make any judgment on the Uri parameter. Therefore, to determine the actually processed Uri of the ContentProvider and determine the data operated by the Uri parameter in each method, the Android system provides the UriMatcher tool class.2. Main MethodsVoid addUri (String authority, String path, int code): This method is used to register a Uri with the UriMatcher object. The authority and path are combined into a Uri, and the code indicates the ID code int match (Uri) corresponding to the uri ): determine the ID code of the specified Uri based on the previously registered Uri. If no matching ID is found,-1 is returned for this method.3. Use of the UriMatcher tool class(1) create a UriMatcher object and register two URIs with it. uriMatcher = new UriMatcher (UriMatcher. NO_MATCH); matcher. addUri ("com. example. android. content_3 "," words ", 1); // The resources department is divided into words, that is, access to all data items
Matcher. addUri ("com. example. android. content_3 "," word/# ", 2); // access the specified data item com. example. android. content_3/words (2) determine the ID code com. example. android. content_3/wordsmatcher. match ("Uri. parse ("matcher. match ("Uri. parse ("com. example. android. content_3/word/10 "));
ContentUris
1. Function OverviewTool class used to operate Uri strings.2. Main MethodsWithAppendedId (Uri, id): Used to add the ID part of the path parseId (uri): Used to parse the contained ID value from the specified Uri3. Use of the ContentUris tool class(1) Add an ID for the Uri. The generated uri is "content: // com. example. android. content_3/word/2 "Uri uri = Uri. parse ("content: // com. example. android. content_3/word "); Uri resultUri = ContentUris. withAppendedId (uri, 2); (2) parse the contained ID value Uri = Uri from the specified uri. parse ("content: // com. example. android. content_3/word/2 "); long wordId = ContentUris. parseId (Uri); // The result is 2.
Ii. URI and Uri1. URI(Public final class) java. lang. Object extends java.net. URI (1) feature Overview: a uniform resource identifier used to identify an abstract or physical resource. At the same time, the URI class can be used not only to parse a URI string, but also to combine several parts into a uniform resource identifier. The constructor is as follows:
URI (String spec) Creates a new URI instance by parsingspec . |
URI (String scheme, String schemeSpecificPart, String fragment) Creates a new URI instance of the given unencoded component parts. |
URI (String scheme, String userInfo, String host, int port, String path, String query, String fragment) Creates a new URI instance of the given unencoded component parts. |
URI (String scheme, String host, String path, String fragment) Creates a new URI instance of the given unencoded component parts. |
URI (String scheme, String authority, String path, String query, String fragment) Creates a new URI instance of the given unencoded component parts. |
(2) URI composition (for example, URI: http: // username: password @ host: 8080/directory/file? Query # fragment)
Component |
Example value |
Also known |
Scheme (Scheme/Protocol) |
http |
Protocol |
Scheme-specific part |
//username:password@host:8080/directory/file?query#fragment |
|
Authority (Identity Authentication) |
username:password@host:8080 |
|
User Info) |
username:password |
|
Host) |
host |
|
Port (Serial Port) |
8080 |
|
Path (resource Path) |
/directory/file |
|
Query |
query |
|
Fragment (Resource Name) |
fragment |
Ref |
Sublimation NOTE 1: For example: Enter the following Uri in the browser: http://www.crazyit.org: 80/ethos. for php, the above URL can be divided into the following three parts: http: //: URL protocol, which is fixed as long as the website is accessed through the HTTP protocol; www.crazyit.org: domain name section. As long as you access the specified website, this part is always fixed; ethos. php: the website resource part. When you access different resources, this part is always changed.
(3) Absolute vs. Relative URIsURIs are either
absolute or relative
.
- Absolute:
http://android.com/robots.txt
- Relative:
robots.txt
Note: absolute URIs contains the scheme part. If the scheme part is supported by a URL, we can convert the absolute URI to a URL using the same toURL () method; URIs does not contain scheme and cannot be converted to URLs. You can use the resolve (String) method and relativize (URI) method of URI to convert each other.
URI absolute = new URI("http://android.com/"); URI relative = new URI("robots.txt"); URI resolved = new URI("http://android.com/robots.txt"); // print "http://android.com/robots.txt" System.out.println(absolute.resolve(relative)); // print "robots.txt" System.out.println(absolute.relativize(resolved));
(4) Opaque vs. Hierarchical URIsAbsolute URIs are either
opaque or hierarchical
. Relative URIs are always hierarchical.
- Hierarchical:
http://android.com/robots.txt
- Opaque:
mailto:robots@example.com
Opaque URIs also contains scheme and scheme-specific part, but note that its scheme-specific part cannot start with a slash '/' and cannot be parsed, all opaque Uris have no authority, user info, host, port, path or query. A typical opaque URI: mailto: robots@example.com
Component |
Example value |
Scheme |
mailto |
Scheme-specific part (not starting with the slash) |
robots@example.com |
Fragment |
(5) encoding and decoding URIDecoding URI: http: // user: pa55w % 3Frd @ host: 80/doc % 7 Csearch? Q = green % 20 robots # over % 206% 22
Component |
Legal Characters (valid character) |
Other Constraints (Other restrictions) |
Raw Value) |
Value (decoded Value) |
Scheme |
0-9 ,a-z ,A-Z ,+-. |
First character must be ina-z ,A-Z |
|
http |
Scheme-specific part |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+=?/[]@ |
Non-ASCII characters okay (Non-ASCII characters) |
//user:pa55w%3Frd@host:80/doc%7Csearch?q=green%20robots |
//user:pa55w?rd@host:80/doc|search?q=green robots |
Authority |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+=@[] |
Non-ASCII characters okay |
user:pa55w%3Frd@host:80 |
user:pa55w?rd@host:80 |
User Info |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+= |
Non-ASCII characters okay |
user:pa55w%3Frd |
user:pa55w?rd |
Host |
0-9 ,a-z ,A-Z ,-.[] |
Domain name, IPv4 address or [IPv6 address] |
|
Host |
Port |
0-9 |
|
|
80 |
Path |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+ =/@ |
Non-ASCII characters okay |
/doc%7Csearch |
/doc|search |
Query |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+=?/[]@ |
Non-ASCII characters okay |
q=green%20robots |
q=green robots |
Fragment |
0-9 ,a-z ,A-Z ,_-!.~'()*,;:$&+=?/[]@ |
Non-ASCII characters okay |
over%206%22 |
over 6" |
2. Uri(Public abstract class) java. lang. object names java.net. uri (1) the Uri content required by ContentProvider: // com. example. android_Content_3/words can be divided into three parts:. content: //: This part is specified by ContentProvider of Android, just as the default protocol for accessing the Internet is http. The default protocol for exposing ContentProvider and accessing ContentProvider is content ://. B .com. example. Android_Content_3: This part is the authority of COntentProvider. The system uses this part to find the ContentProvider to operate on. As long as you access the specified ContentProvider, this part is always fixed. C. words: Resource (data ). This part changes dynamically when visitors need to access different resources.
Sublimation NOTE 2: Android Uri can provide richer functions. content: // com. example. android_Content_3/words: access resources for all data items words2.content: // com. example. android_Content_3/word/2: Access the resource is word/2, that is, access the record with ID 2 in word data 3. content: // com. example. android_Content_3/word/2/word: used to access the word field of a record whose ID is 2 in word data. Note: although most of the data operated by ContentProvider comes from the database, sometimes the data can also come from other storage methods such as files, XML, or networks, the supported Uri can also be changed to the following format: content: // com. example. android_Content_3/word/detail/, which indicates the detail node under the word node.
(2) Uri embedding class
Nested Classes |
Class |
Uri. Builder |
Helper class for building or manipulating URI references. |
(3) Common MethodsStatic Uri parse (String uriString): converts a String into a Uri application: Uri uri = Uri. parse ("content: // com. example. Android_Content_3/word/2 ");
Reference: http://wear.techbrood.com/reference/java/net/URI.html