in front of network programming (1) We learned that if you want to access the resources of other hosts, then you need to know the URL of the resource. So if we know the URL, how do we go toaccess the resources on its host? This articlemainly about the Java language Programming Android network programming of the most basic parent class urlconnectiion, the class is the use of URLs to achieve host resource access, each subclass of its characteristics are further optimized and support the new network protocol, to achieve from the full to the local, Learn from extensive to granular systems. from the JAVAAPI, you can seeJava.lang.Object java.net.URLConnection
URLConnection is a superclass of a network connection class that represents a communication link between an application and a URL.
Its immediate sub-class is:httpurlconnectionjarurlconnection
HttpURLConnection is a urlconnection that supports HTTP-specific features. This class is our most commonly used network resource access class.
There are also jarconnection are classes that you can use to connect to a Java document or jar file.
Instances of this class can be used to read and write resources referenced by this URL.
1) Use the OpenConnection method on the URL to create the connection object.
<strong>urlconnection conn = url.openconnection ();</strong>
Note: Constructs a URL connection to the specified URL and does not create a connection to the object referenced by the URL. That is to say, only the logical pipe is connected, but there is no read-write support.
2) Set parameters and general request properties.
For example, after we connect to the remote host, we need to set some read restrictions and so on. For example, allow resources to be read, do not use caching, and so on.
setallowuserinteraction (Boolean allowuserinteraction)
Allows user interaction to be allowed, such as to start the Validation dialog box after accessing the background
setdoinput (boolean doinput)
set the input stream, which is a URL connection that can be used for input and/or output. The doinput
< Code style= "Background-color:inherit" > Setoutinput (boolean dooutput)
Sets the output stream, which is a URL connection that can be used for input and/or output. set the dooutput flag to Truetoindicate that the application is writing data to a URL connection. default is Flase
setusecaches (Boolean usecaches)
Set whether to use the cache
Setconnecttimeout (int timeout)
Sets the connection time-out period, in milliseconds, for a specified time-out value that will be used when opening a communication link to a resource referenced by this urlconnection. If the timeout expires before the connection is established, a java.net.SocketTimeoutException is raised. A time-out of zero indicates an infinite timeout.
Setreadtimeout (int timeout)
Sets the read time-out period, in milliseconds.setcontenthandlerfactory (contenthandlerfactory FAC)
Sets the contenthandlerfactory of the application . An application can call the method at most once. contenthandlerfactory instances are used to construct content handlers based on content types.
Use the following methods to set the request properties:
Setrequestproperty (String key,string value)
Sets the request properties. (Very common method)
3) Use
connect
method to establish an actual connection to a remote object.
<span style= "FONT-SIZE:14PX;" ><span style= "Font-family:simhei;" ><span style= "Font-weight:normal;" >conn.connect ();</span></span></span>
Note: The URLConnection object goes through two stages: Create the object first, and then establish the connection. Step (1) is only the connection phase. The above parameters and attributes can be set between the (1) and (3) steps. 4) operation of the corresponding resources.
The operation can take place after the connection is made, at which point the remote object becomes available. The header fields and contents of the remote object become accessible.
The header field can be understood as the configuration information file for the client request message and the service-side response message, which describes and restricts the content.
The content is the interaction information body of the client and the service side.
Common 6 Header Field access methods:
public int Getcontentlength ()Returns the value of the Content-length header field. Return Description: The content length of the resource referenced by the URL of this connection, or 1 if the content length is unknown.Public String getContentType ()Returns the value of the Content-type header field. Returns a description: The content type of the resource referenced by the URL, or null if the type is unknown.Public String getcontentencoding ()Returns the value of the Content-encoding header field. Returns a description: The content encoding of the resource referenced by the URL, or null if the encoding is unknown.Public Long getexpiration ()Returns the value of the Expires header field. Returns a description: The expiration date of the resource referenced by this URL, or 0 if it is unknown. The value is the number of milliseconds from Greenwich Mean Time January 1, 1970Public Long GetDate ()Returns the value of the Date header field. Returns a description: the date that the URL refers to the resource sent, or 0 if it is unknown. The value returned is the number of milliseconds from Greenwich Mean Time January 1, 1970.Public Long getlastmodified ()Returns the value of the Last-modified header field. The result is the number of milliseconds from Greenwich Mean Time January 1, 1970. Returns a description: The last modified date of the resource referenced by this urlconnection, or 0 if it is unknown. public string Getheaderfield (string name) (this method belongs to the Balm method, which gets the corresponding header field value, in the form of a key value) that returns the value of the specified header field. If you call a connection multiple times with a different value to set the header field, only the last set value is returned. The name of the name-header field for the parameter. Returns a description: The value of the specified header field, or null if there is no such field in the header.Public map<string,list<string>> Getheaderfields ()Returns a non-modifiable Map of the header field. The Map key is a String that represents the name of the Response-header field. Each MAP value is a list of non-modifiable strings that represent the corresponding field values. Return Description: Header field Mapapi There are more ways to get the header field, and if necessary, you can download the API reference at the beginning of the blog post. Common ways to access content:Public Object getcontent () throws IOExceptionUsed to get the content of this URL connection. This method first determines the object's content type by calling the getContentType method. If this is the specific content type that the application encountered for the first time, a content handler for that type is created: If the application has established a content handler factory instance using the Setcontenthandlerfactory method, The Creatcontenthandler method of this instance is called, the content type is one of the parameters, and the result is a content handler for that content type. If no content handler factory has been established, or if the factory's Creatcontenthandler method returns NULL, the application loads the class with the following name: Sun.net.www.content.<contenttype> which, <contentType> takes the Content-type string, all the slash characters with a period ('. ') Replace, and all other non-alphanumeric characters are replaced with an underscore ' _ '. Alphanumeric character traits classes refers to 26 uppercase ASCII letters ' a ' to ' z ', 26 lowercase ASCII letters ' a ' to ' Z ' and 10 ASCII digits ' 0 ' to ' 9 '. If the specified class does not exist or is not a subclass of ContentHandler, a unknownserviceexception is thrown. Return Description: Gets the object. You should use the instanceof operator to determine the specific type of object returned.Public Object getcontent (class[] classes) throws IOExceptionGets the contents of this URL connection. Parameter: class[]-indicates the class array of the requested type returns a description: Gets the first object that matches the type specified in the class array. If the requested type is not supported, NULL is returned. You should use the instanceof operator to determine the specific type of object returned.Public InputStream getInputStream () throws IOExceptionReturns the input stream read from this open connection. When reading the returned input stream, Sockettimeoutexception is thrown if the read-in time-out is reached before the data is available for reading. Return Description: The input stream that was read into from this open connection.Public OutputStream Getoutputstream () throws IOExceptionReturns the output stream written to this connection. Return Description: Writes the output stream to this connection.
This class is more important, although the later development of Android uses its HTTP subclass, but for the deepening of the understanding of the sub-class is highly divided, this class is necessary to master.
Network Programming (2)--Connecting the parent class URLConnection