Android 網路編程(2)——URL互連網資源指標

來源:互聯網
上載者:User

標籤:url

URL(Uniform Resource Locator)對象代表統一資源定位器,它是指向互連網"資源"的指標。資源可以是簡單的檔案或目錄,也可以是對更複雜的對象的引用,例如對資料庫或搜尋引擎的查詢。通常情況而言,URL可以由協議名、主機、連接埠和資源群組成。即滿足如下格式:

protocol://host:port/resourceName

例如如下的URL地址:

http://www.baidu.com/index.php

URL類提供了多個構造器用於建立URL對象,一旦擷取了URL對象之後,可以調用如下常用方法來方法URL對應的資源。

  • String getFile():擷取此URL的資源名
  • String getHost():擷取此URL的主機名稱
  • String getPath():擷取此URL的路徑部分
  • int getPort():擷取此URL的連接埠號碼
  • String getProtocol():擷取此URL的協議名稱
  • String getQuery():擷取此URL的查詢字串部分
  • URLConnection openConnection():返回一個URLConnection對象,它表示到URL所引用的遠程對象的串連
  • InputStream openStream():開啟與此URL的串連,並返回一個擁有讀取該URL資源的InputStream
常式:使用URL讀取網路資源 AndroidManifest.xml——主要添加訪問網路許可權
<?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="19" />    <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.url.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></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){//使用ImageView顯示圖片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() {        // TODO Auto-generated method stub        super.run();                try {        //定義一個URL對象URL url = new URL("http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg");Log.e("URL資源名", url.getFile());Log.e("URL主機名稱", url.getHost());Log.e("URL路徑部分", url.getPath());Log.e("URL連接埠號碼", ""+url.getPort());//開啟該URL對應的資源的輸入資料流InputStream inputStream = url.openStream();//從InputStream中解析出圖片bitmap = BitmapFactory.decodeStream(inputStream);//發訊息通知UI組件顯示此bitmap對應的圖片handler.sendEmptyMessage(0x123);inputStream.close();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}        }        }.start();    }    }

注意
URL url = new URL("http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg");
其中http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg是圖片地址,而不是顯示此圖片的網路地址。【右擊網狀圖片】->【複製圖片地址】->【得到網狀圖片的地址】

Android 網路編程(2)——URL互連網資源指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.