Url:uniform Resource Locator Unified Resource Locator
In general, URLs can have protocol names, hosts, ports, and resources that meet the following formats:
Protocol://host:port/resourcename
For example:
http://www.baidu.com/index.php
Public constructors Construction Method:
URL (String spec) creates a new URL instance by parsing the String spec. URL (url context, String spec) creates a new URL to the specified resource spec. URL (url context, String spec, URLStreamHandler handler) creates a new URL to the specified resource spec. The URL (string protocol, string host, string file) Creates a new URL instance using the given arguments. The URL (string protocol, string host, int port, string file) Creates a new URL instance using the given arguments.
Common methods:
String getFile (): Gets the resource name for this URL
String gethost (): Get host Name
String GetPath (): Get Path
int Getport (): Get port number
String Getprotocol (): Gets the protocol name
String Getquery (): Gets the query string portion of this URL
URLConnection openconnection (): Returns a URLConnection object that represents the connection to the remote object referenced by the URL
InputStream OpenStream (): Opens a connection to this URL and returns an input stream for reading the URL resource
Read network resources:
A picture:
/*
Http://www.crazyit.org/attachments/month_1008/20100812_7763e970f822325bfb019ELQVym8tW3A.png
*/
Two functions:
1. Create a URL object with the above URL, get the InputStream of the object resource, parse out the picture using the Bitmapfactory.decodestream () method, and display
2. Download the picture.
public class Mainactivity extends Activity {ImageView show; Bitmap Bitmap; Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {if (Msg.what = = 0x123) {//Use ImageView Show the picture Show.setimagebitmap (bitmap);};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); show = (ImageView) Findviewbyid (r.id.show); new Thread () {public void run () {try {//define a URL object url url = new URL ("http://www.crazyit.org/" + "attachments/month_1008/20100812_7763e970f" + "822325bfb019elqvym8tw3a.png" )///open the input stream for the resource corresponding to the URL InputStream is = Url.openstream ();//parse the picture from InputStream bitmap = Bitmapfactory.decodestream (is);// Send Message Notification UI component displays the picture Handler.sendemptymessage (0x123); Is.close ()//download picture//Open the input stream of the URL corresponding resource again, create picture object is = Url.openstream ();/ Open the phone file corresponding to the output stream outputstream OS = openfileoutput ("Crazyit.png", mode_world_readable); byte buff[] = new Byte[1024];int Hasread = 0;//Download the picture to Local while ((Hasread = Is.read (buff)) > 0) {os.write (bUff, 0, hasread);} Is.close (); Os.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}};}. Start ();}}
Android------Use URLs to access network resources