as3corelib Tutorial:How to Use URI in Flex

penguin-256x256 IMO, the major usage of this class is parsing a URL to retrieve the information such as host, port, file name, file extension, query, etc. It’s really a convenient way instead of using regular expression to parse the target information.

According to the documentation of as3corelib:

This class implements functions and utilities for working with URI’s (Universal Resource Identifiers). For technical description of the URI syntax, please see RFC 3986 at or do a web search for “RFC 3986″.

The most important aspect of URI’s to understand is that URI’s and URL’s are not strings. URI’s are complex data structures that encapsulate many pieces of information. The string version of a URI is the serialized representation of that data structure. This string serialization is used to provide a human readable representation and a means to transport the data over the network where it can then be parsed back into its’ component parts.

URI’s fall into one of three categories:

<scheme>:<scheme-specific-part>#<fragment> (non-hierarchical)
<scheme>:<authority><path>?<query>#<fragment> (hierarchical)=""></authority><path>?<query>#<fragment>>
<path>?<query>#<fragment> (relative hierarchical)

The query and fragment parts are optional.

This class supports both non-hierarchical and hierarchical URI’s

Search-256x256 Demo | DownloadDownload Full Project

This class is intended to be used “as-is” for the vast majority of common URI’s. However, if your application requires a custom URI syntax (e.g. custom query syntax or special handling of non-hierarchical URI’s), this class can be fully sub-classed. If you intended to subclass URI, please see the source code for complete documentation on protected members and protected functions.

For example:

var uri:URI = new URI(Application.application.url);
userService.url = “http://”+uri.authority+ (uri.port==””?””:”:”+uri.port) + ”/FileManage/servlet”;

Here, the URI class is used to retrieve the host value and port value of the server which reserves the requested flex application.

The documentation of this class is attached at the end of this article.

Screenshot of this demo:


Following is the source code of URIExample.xml

<?xml version="1.0" encoding="utf-8"?>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value=""/>
	<uri value="/rfc/rfc2396.txt"/>
	<uri value="rfc/background.png"/>
	<uri value="../rfc/rfc2396.txt"/>
	<uri value="file:///F:/opensource/as3corelib-.92.1/as3corelib-.92.1/docs/index.html"/>
	<uri value="F:/opensource/as3corelib-.92.1/as3corelib-.92.1/docs/index.html"/>
	<uri value="F:\\opensource\\as3corelib-.92.1\\as3corelib-.92.1\\docs\\index.html"/>
	<uri value="ldap://[2001:db8::7]/c=GB?objectClass?one"/>
	<uri value=""/>
	<uri value="news:comp.infosystems.www.servers.unix"/>
	<uri value="tel:+1-816-555-1212"/>
	<uri value="telnet://"/>
	<uri value="urn:oasis:names:specification:docbook:dtd:xml:4.1.2"/>

And the source code of URIDemo.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute">

			private var uri:URI;

			private function selectURI(event:MouseEvent):void
				uri = new URI(event.currentTarget.text);

	<mx:XML id="uris" source="URIExample.xml"/>

		title="URI Demo"
			<mx:Repeater id="uriList" dataProvider="{uris.uri}">

			<mx:FormItem label="is valid">
				<mx:Label text="{uri.isValid()}"/>

			<mx:FormItem label="to display string">
				<mx:Label text="{uri.toDisplayString()}"/>

			<mx:FormItem label="to string">
				<mx:Label text="{uri.toString()}"/>

			<mx:FormItem label="schema">
				<mx:Label text="{uri.scheme}"/>

			<mx:FormItem label="username">
				<mx:Label text="{uri.username}"/>

			<mx:FormItem label="password">
				<mx:Label text="{uri.password}"/>

			<mx:FormItem label="authority">
				<mx:Label text="{uri.authority}"/>

			<mx:FormItem label="default port">
				<mx:Label text="{uri.getDefaultPort()}"/>

			<mx:FormItem label="port">
				<mx:Label text="{uri.port}"/>

			<mx:FormItem label="path">
				<mx:Label text="{uri.path}"/>

			<mx:FormItem label="file name">
				<mx:Label text="{uri.getFilename(true)}"/>

			<mx:FormItem label="extension">
				<mx:Label text="{uri.getExtension(true)}"/>

			<mx:FormItem label="query">
				<mx:Label text="{uri.query}"/>

			<mx:FormItem label="queryRaw">
				<mx:Label text="{uri.queryRaw}"/>

			<mx:FormItem label="fragment">
				<mx:Label text="{uri.fragment}"/>

			<mx:FormItem label="nonHierarchical">
				<mx:Label text="{uri.nonHierarchical}"/>

			<mx:FormItem label="is absolute uri">
				<mx:Label text="{uri.isAbsolute()}"/>

			<mx:FormItem label="is relative uri">
				<mx:Label text="{uri.isRelative()}"/>

			<mx:FormItem label="is directory">
				<mx:Label text="{uri.isDirectory()}"/>

			<mx:FormItem label="is hierarchical">
				<mx:Label text="{uri.isHierarchical()}"/>
				it seems isOfFileType return the opposite
				result. maybe it's a bug of this method.
			<mx:FormItem label="is of file type(html)">
				<mx:Label text="{uri.getExtension(true)==''?false:!uri.isOfFileType('html')}"/>

				<mx:FormItem label="custom uri">
					<mx:TextInput id="customURI"/>

					<mx:Button  id="parseButton"
						click="uri = new URI(customURI.text)"/>
Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “as3corelib Tutorial:How to Use URI in Flex”

  1. Steve says:

    Hi There

    Great tutorial. I was wondering if you could point me in the right direction as this tutorial seems to be what i am after – but might be a bit much for the simple feature i am trying to acheive.

    I have a external .swf file which will get loaded by either swfloader or navigateToURL via a flex app. the .swf file is not part of the flex framework, and it has a URI datainput field with a clickable button.

    What i am trying to do is basically move that URI datainput field from the external .swf file and put it in the flex app. When the clickable button is pressed i need a variable with the URI value sent to the .swf, so that when the .swf fires it also looks for and loads the URI variable data.

    Any help guys would be really appreciated, this thing is pulling my hair out.



  2. [...] 原文: Tags: AS3, as3Corelib, Flash, Flex You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

Leave a Reply