加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090227083647/http://developer.android.com:80/reference/java/net/URL.html
public final class

URL

extends Object
implements Serializable
java.lang.Object
   ↳ java.net.URL

Class Overview

An instance of class URL specifies the location of a resource on the world wide web as specified by RFC 1738.

Summary

Public Constructors
URL(String spec)
Constructs a new URL instance by parsing the specification.
URL(URL context, String spec)
Constructs a new URL by parsing the specification given by spec and using the context provided by context.
URL(URL context, String spec, URLStreamHandler handler)
Constructs a new URL by parsing the specification given by spec and using the context provided by context.
URL(String protocol, String host, String file)
Constructs a new URL instance using the arguments provided.
URL(String protocol, String host, int port, String file)
Constructs a new URL instance using the arguments provided.
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
Constructs a new URL instance using the arguments provided.
Public Methods
boolean equals(Object o)
Compares the argument to the receiver, and returns true if they represent the same URL.
String getAuthority()
Returns the authority component of this URL.
final Object getContent(Class[] types)
Returns an Object representing the resource referenced by this URL.
final Object getContent()
Returns an Object representing the resource referenced by this URL.
int getDefaultPort()
Returns the default port for this URL as defined by the URLStreamHandler.
String getFile()
Returns the file component of this URL.
String getHost()
Returns the host component of this URL.
String getPath()
Returns the path component of this URL.
int getPort()
Returns the port component of this URL.
String getProtocol()
Returns the protocol component of this URL.
String getQuery()
Returns the query component of this URL.
String getRef()
Returns the reference component of this URL.
String getUserInfo()
Returns the user info component of this URL.
int hashCode()
Returns a hash code for this URL object.
URLConnection openConnection()
Creates a connection to this URL using the appropriate ProtocolHandler.
URLConnection openConnection(Proxy proxy)
The method is the same as openConnection() except that it uses the proxy to establish a connection to this URL using appropriate ProtocolHandler.
final InputStream openStream()
Returns a stream for reading from this URL.
boolean sameFile(URL otherURL)
Returns true if the receiver and the argument refer to the same file.
synchronized static void setURLStreamHandlerFactory(URLStreamHandlerFactory streamFactory)
Sets the URL Stream (protocol) handler factory.
String toExternalForm()
Create and return the String representation of this URL.
String toString()
Returns a string containing a concise, human-readable description of the receiver.
URI toURI()
Creates a URI related with this URL
Protected Methods
void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)
Sets the properties of this URL using the provided arguments.
void set(String protocol, String host, int port, String file, String ref)
Sets the properties of this URL using the provided arguments.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public URL (String spec)

Constructs a new URL instance by parsing the specification.

Parameters
spec java.lang.String a URL specification.
Throws
MalformedURLException if the spec could not be parsed as an URL.

public URL (URL context, String spec)

Constructs a new URL by parsing the specification given by spec and using the context provided by context.

The protocol of the specification is obtained by parsing the spec string.

If the spec does not specify a protocol:

  • If the context is null, then a MalformedURLException.
  • If the context is not null, then the protocol is obtained from the context.
If the spec does specify a protocol:
  • If the context is null, or specifies a different protocol than the spec, the context is ignored.
  • If the context is not null and specifies the same protocol as the specification, the properties of the new URL are obtained from the context.
Parameters
context java.net.URL URL to use as context.
spec java.lang.String a URL specification.
Throws
MalformedURLException if the spec could not be parsed as an URL.

public URL (URL context, String spec, URLStreamHandler handler)

Constructs a new URL by parsing the specification given by spec and using the context provided by context.

If the handler argument is non-null, a security check is made to verify that user-defined protocol handlers can be specified.

The protocol of the specification is obtained by parsing the spec string.

If the spec does not specify a protocol:

  • If the context is null, then a MalformedURLException.
  • If the context is not null, then the protocol is obtained from the context.
If the spec does specify a protocol:
  • If the context is null, or specifies a different protocol than the spec, the context is ignored.
  • If the context is not null and specifies the same protocol as the specification, the properties of the new URL are obtained from the context.
Parameters
context java.net.URL URL to use as context.
spec java.lang.String a URL specification.
handler java.net.URLStreamHandler a URLStreamHandler.
Throws
MalformedURLException if the spec could not be parsed as an URL

public URL (String protocol, String host, String file)

Constructs a new URL instance using the arguments provided.

Parameters
protocol String the protocol for the URL.
host String the name of the host.
file the name of the resource.
Throws
MalformedURLException if the parameters do not represent a valid URL.

public URL (String protocol, String host, int port, String file)

Constructs a new URL instance using the arguments provided.

Parameters
protocol String the protocol for the URL.
host String the name of the host.
port int the port number.
file String the name of the resource.
Throws
MalformedURLException if the parameters do not represent a valid URL.

public URL (String protocol, String host, int port, String file, URLStreamHandler handler)

Constructs a new URL instance using the arguments provided.

If the handler argument is non-null, a security check is made to verify that user-defined protocol handlers can be specified.

Parameters
protocol the protocol for the URL.
host the name of the host.
port the port number.
file the name of the resource.
handler the stream handler that this URL uses.
Throws
MalformedURLException if the parameters do not represent an URL.

Public Methods

public boolean equals (Object o)

Compares the argument to the receiver, and returns true if they represent the same URL. Two URLs are equal if they have the same file, host, port, protocol, and reference components.

Parameters
o the object to compare with this URL.
Returns
  • true if the object is the same as this URL, false otherwise.
See Also

public String getAuthority ()

Returns the authority component of this URL.

Returns
  • the receiver's authority.

public final Object getContent (Class[] types)

Returns an Object representing the resource referenced by this URL.

Parameters
types The list of acceptable content types
Returns
  • The object of the resource pointed by this URL, or null if the content does not match a specified content type.
Throws
IOException If an error occurred obtaining the content.

public final Object getContent ()

Returns an Object representing the resource referenced by this URL.

Returns
  • The object of the resource pointed by this URL.
Throws
IOException If an error occurred obtaining the content.

public int getDefaultPort ()

Returns the default port for this URL as defined by the URLStreamHandler.

Returns
  • the default port for this URL
See Also

public String getFile ()

Returns the file component of this URL.

Returns
  • the receiver's file.

public String getHost ()

Returns the host component of this URL.

Returns
  • the receiver's host.

public String getPath ()

Returns the path component of this URL.

Returns
  • the receiver's path.

public int getPort ()

Returns the port component of this URL.

Returns
  • the receiver's port.

public String getProtocol ()

Returns the protocol component of this URL.

Returns
  • the receiver's protocol.

public String getQuery ()

Returns the query component of this URL.

Returns
  • the receiver's query.

public String getRef ()

Returns the reference component of this URL.

Returns
  • the receiver's reference component.

public String getUserInfo ()

Returns the user info component of this URL.

Returns
  • the receiver's user info.

public int hashCode ()

Returns a hash code for this URL object.

Returns
  • the hashcode for hashtable indexing

public URLConnection openConnection ()

Creates a connection to this URL using the appropriate ProtocolHandler.

Returns
  • The connection to this URL.
Throws
IOException if the connection to the URL is not possible.

public URLConnection openConnection (Proxy proxy)

The method is the same as openConnection() except that it uses the proxy to establish a connection to this URL using appropriate ProtocolHandler.

Parameters
proxy the proxy which is used to make the connection
Returns
  • The connection to this URL.
Throws
IOException thrown if an IO error occurs during connection establishment
SecurityException thrown if a security manager is installed and it denies the permission to connect to the proxy.
IllegalArgumentException thrown if the proxy is null or of an invalid type.
UnsupportedOperationException thrown if the protocol handler doesn't support this method.

public final InputStream openStream ()

Returns a stream for reading from this URL.

Returns
  • a stream on the contents of the resource.
Throws
IOException if a stream could not be created.

public boolean sameFile (URL otherURL)

Returns true if the receiver and the argument refer to the same file. All components except the reference are compared.

Parameters
otherURL URL to compare against.
Returns
  • true if the same resource, false otherwise

public static synchronized void setURLStreamHandlerFactory (URLStreamHandlerFactory streamFactory)

Sets the URL Stream (protocol) handler factory. This method can be invoked only once during an application's lifetime.

A security check is performed to verify that the current Policy allows the stream handler factory to be set.

Parameters
streamFactory URLStreamHandlerFactory The factory to use for finding stream handlers.

public String toExternalForm ()

Create and return the String representation of this URL.

Returns
  • the external representation of this URL.

public String toString ()

Returns a string containing a concise, human-readable description of the receiver.

Returns
  • a printable representation for the receiver.

public URI toURI ()

Creates a URI related with this URL

Returns
  • a URI related to this URL
Throws
URISyntaxException if this URL cannot format into URI

Protected Methods

protected void set (String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)

Sets the properties of this URL using the provided arguments. This method is used both within this class and by the URLStreamHandler code.

Parameters
protocol the new protocol.
host the new host name.
port the new port number.
authority the new authority.
userInfo the new user info.
path the new path component.
query the new query.
ref the new reference.

protected void set (String protocol, String host, int port, String file, String ref)

Sets the properties of this URL using the provided arguments. This method is used both within this class and by the URLStreamHandler code.

Parameters
protocol the new protocol.
host the new host name.
port the new port number.
file the new file component.
ref the new reference.