Android --- 52 --- use URl to access network resources, android --- 52 --- url
URL: Uniform Resource Locator
Generally, a URl may consist of a protocol name, host, port, and resource, which meets the following format:
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. URL(String protocol, String host, String file) Creates a new URL instance using the given arguments. URL(String protocol, String host, int port, String file) Creates a new URL instance using the given arguments. URL(String protocol, String host, int port, String file, URLStreamHandler handler) Creates a new URL instance using the given arguments.
Common Methods:
String getFile (): gets the Resource Name of this URL
String getHost (): Get the Host Name
String getPath (): Get path
Int getPort (): Get the port number
String getProtocol (): Get the protocol name
String getQuery (): Obtain the query String of this URl.
URLConnection openConnection (): returns a URLConnection object, which indicates the connection to the remote object referenced by the URl.
InputStream openStream (): Open the connection to this URL and return an input stream used to read the URL resource.
Read network resources:
One image:
/*
Http://www.crazyit.org/attachments/month_1008/20100812_7763e970f822325bfb019ELQVym8tW3A.png
*/
Two functions:
1. The preceding URL creates a URL object for the parameter to obtain the InputStream of the object resource. The image is parsed and displayed using BitmapFactory. decodeStream ().
2. Download the image.
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 to display the Image 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 = new url ("http://www.crazyit.org/" + "attachments/month_1008/20100812_7763e970f" + "822325bfb019ELQVym8tW3A.png "); // open the input stream InputStream is = URL of the resource corresponding to the url. openStream (); // parses the image bitmap = BitmapFactory from InputStream. decodeStream (is); // The UI component that sends a message notification to display the handler image. sendEmptyMessage (0x123); is. close (); // download the image // open the input stream of the resource corresponding to the URL again and create the image object is = url. openStream (); // The output stream OutputStream OS = openFileOutput ("crazyit.png", MODE_WORLD_READABLE) corresponding to the mobile phone file; byte buff [] = new byte [1024]; int hasRead = 0; // download the image to the 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 ();}}