The URL (Uniform Resource Locator) object represents the Uniform Resource Locator, which is a pointer to the Internet "resource". A resource can be a simple file or directory, or a reference to a more complex object, such as a query to a database or search engine. Typically, a URL can consist of aprotocol name, a host, a port, and a resource . The following format is satisfied:
Protocol://host:port/resourcename
For example, the following URL address:
http://www.baidu.com/index.php
The URL class provides multiple constructors for creating a URL object, and once the URL object is acquired, you can call the following common methods to approach the resource for the URL.
- String getFile (): Gets the resource name for this URL
- String gethost (): Gets the host name of this URL
- String GetPath (): Gets the path portion of this URL
- int Getport (): Gets the port number of this URL
- String Getprotocol (): Gets the protocol name for this URL
- 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 a inputstream that has read the URL resource
Routines: Reading network resources using URLs
androidmanifest.xml--main Add access network permissions
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.example.url "android:versioncode=" 1 "android:versionname=" 1.0 " > <USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission an" Droid:name= "Android.permission.INTERNET"/> <application android:allowbackup= "true" android:icon= "@d Rawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activi Ty android:name= "com.example.url.MainActivity" android:label= "@string/app_name" > < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category an Droid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </app Lication></manifest>
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:gravity= "center" tools:context= ". Mainactivity "> <imageview android:id=" @+id/imageview " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" /></linearlayout>
Mainactivity.java
Package Com.example.url;import Java.io.ioexception;import Java.io.inputstream;import Java.net.malformedurlexception;import Java.net.url;import Android.os.bundle;import Android.os.Handler;import Android.os.message;import Android.util.log;import Android.widget.imageview;import Android.annotation.SuppressLint ; Import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;public class Mainactivity extends Activity {private ImageView show;private Bitmap Bitmap; @SuppressLint ("Handlerleak") private Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {if (Msg.what = = 0x123) {//Use ImageView to display pictures Show.setimagebitmap (bitmap);}}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Show = (ImageView) This.findviewbyid (R.id.imageview); New Thread () {@Override public void run () {//TODOauto-generated method Stub Super.run (); try {//define a URL object url url = new URL ("Http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg"); LOG.E ("URL resource Name", Url.getfile ()); LOG.E ("url hostname", url.gethost ()); LOG.E ("URL path section", Url.getpath ()); LOG.E ("URL port number", "" "+url.getport ());//Open the input stream for the resource corresponding to the URL InputStream inputstream = Url.openstream ();// Parse the picture from the InputStream bitmap = Bitmapfactory.decodestream (InputStream);// The message notification UI component displays this bitmap corresponding picture handler.sendemptymessage (0x123); Inputstream.close ();} catch (Malformedurlexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();} }}.start (); } }
Note:
URL url = new URL ("Http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg");
Where http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg is the picture address, not the network address of the picture. "Right click Network Picture", "Copy Picture Address", "get the address of the network picture"
Android Network Programming (2)--url Internet resource pointers