Android---52---使用URl訪問網路資源,android---52---url

來源:互聯網
上載者:User

Android---52---使用URl訪問網路資源,android---52---url

URL:Uniform Resource Locator 統一資源定位器

 

通常情況而言,URl可以有協議名、主機、連接埠和資源群組成,即滿足以下格式:

protocol://host:port/resourceName

 

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

 

 

Public Constructors 構造方法:

 

 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. 


 

常用方法:

 


String getFile ():擷取此URL的資源名
String getHost(): 擷取主機名稱
String getPath ():擷取路徑
int getPort ():擷取連接埠號碼
String getProtocol ():擷取協議名稱
String getQuery():擷取此URl的查詢字串部分
URLConnection openConnection():返回一個URLConnection對象,它表示到URl所引用的遠程對象的串連
InputStream openStream():開啟與此URL的串連,並返回一個用於讀取該URL資源的輸入資料流

 

 

讀取網路資源:

 

一張圖片:
/*
http://www.crazyit.org/attachments/month_1008/20100812_7763e970f822325bfb019ELQVym8tW3A.png
*/

兩個功能:

1.以上面網址為參數建立URL對象,獲得該對象資源的InputStream,使用BitmapFactory.decodeStream()方法解析出圖片 並顯示

2.將圖片下載下來。

 

 

public class MainActivity extends Activity {ImageView show;Bitmap bitmap;Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {if (msg.what == 0x123) {// 使用ImageView顯示該圖片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 {// 定義一個URL對象URL url = new URL("http://www.crazyit.org/"+ "attachments/month_1008/20100812_7763e970f"+ "822325bfb019ELQVym8tW3A.png");// 開啟該URL對應的資源的輸入資料流InputStream is = url.openStream();// 從InputStream中解析圖片bitmap = BitmapFactory.decodeStream(is);// 發送訊息 通知UI組件顯示該圖片handler.sendEmptyMessage(0x123);is.close();//下載圖片// 再次開啟URL對應資源的輸入資料流,建立圖片對象is = url.openStream();// 開啟手機檔案對應的輸出資料流OutputStream os = openFileOutput("crazyit.png",MODE_WORLD_READABLE);byte buff[] = new byte[1024];int hasRead = 0;// 將圖片下載到本地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();}}


 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.