android開發之獲得網路資源

2021-08-31 19:43:49 字數 1019 閱讀 7786

android獲取網路資源

1.生成url物件

2.建立連線

3.獲取資料

例one:

/* 定義獲取檔案內容的url */

url myurl = new url( "");

/* 開啟url鏈結 */

urlconnection ucon = myurl.openconnection();

/* 使用inputstreams,從urlconnection讀取資料*/

inputstream is = ucon.getinputstream();

bufferedinputstream bis = new bufferedinputstream(is);

例two:

url iconurl = new url(""+ gwh.geticonurl());

urlconnection conn = iconurl.openconnection();

conn.connect();

inputstream is = conn.getinputstream();

bufferedinputstream bis = new bufferedinputstream(is);

1.字串:

/* 用bytearraybuffer做快取 */

bytearraybuffer baf = new bytearraybuffer(50);

int current = 0;

while((current = bis.read()) != -1)

/* 將快取的內容轉化為string, 用utf-8編碼 */

mystring = encodingutils.getstring(baf.tobytearray(), "utf-8");

//mystring = new string(baf.tobytearray());

2.:bitmap bm = null;

bm = bitmapfactory.decodestream(bis);

Android網路服務開發之Socket程式設計

socket是基於埠的通訊。一般情況下,乙個應用可以包含多個埠,但乙個埠只能分配給乙個應用程式。同乙個埠可以傳送資料,也可以接收資料。使用socket的步驟 1.首先描述乙個遠端的伺服器位址。可以使用inetaddress類來構造,也可以直接使用伺服器的網域名稱。2.建立乙個socket物件,建構函...

Android開發之獲取網路IP位址方法總結

1.1wifi下獲取本地區域網ip位址 wifi下獲取本地網路ip位址 區域網位址 public static string getlocalipaddress context context return 1.2.流動網路獲取網路ip位址 獲取有限網ip public static string ...

Android開發之簡訊

1.sms主要結構 id 簡訊序號,如100 thread id 對話的序號,如100,與同乙個手機號互發的簡訊,其序號是相同的 person 發件人,如果發件人在通訊錄中則為具體姓名,陌生人為null date 日期,long型,如1346988516,可以對日期顯示格式進行設定 protocol...