Android HTTP程式設計

2021-09-29 08:47:52 字數 3596 閱讀 1304

例如:http://www:*****.com/china/index.html

(1)協議名:http://

(2)全球資訊網伺服器:www:

(3)裝有網頁伺服器的網域名稱或站點伺服器的名稱:*****.com

(4)訪問在伺服器上的路徑:/china/

(5)訪問的內容:index.html

①url是網際網路上的「資源」的唯一位址標識,它由協議名、主機、埠和資源組成。我們通過url類來獲取這些相關的屬性。

public string getprotocol()

獲取url的協議名                                  

public string gethost() 

獲取url的主機名

public string getport()

獲取url的埠號

public string getfile()

獲取url的檔名

public string getpath()

獲取url的路徑

public string getauthority()

獲取url的許可權資訊

public string getquery()

獲取url的查詢字串部分

public final object getcontent()

獲取url的內容 

②通過url類獲取網頁內容的步驟

(1)先例項化乙個url物件   

url url = new url("") 

(2)利用public final inputstream openstream()開啟url的連線,再利用inputstreamreader()接收

inputstreamreader insr = new inputstreamreader(url.openstream();

(3)再構造乙個buffererreader()物件獲取資源(如要對網頁資源進行解析,則需要使用uri類)

bufferreader br = new bufferreader(insr);

string str  = br.readline();

二、urlconnection,利用該類我們可以傳遞http的訊息頭,對它可以上傳引數。可以完成一些刪除、查詢等功能的應用

不過實現這些操作常用它的子類httpurlconnection類。

①urlconnection是抽象類,是實現應用程式和url之間通訊連線的所有類的超類,該類的物件可以對url所指定的資源進行讀寫操作

public abstract void connect()

建立到此url引用的資源的通訊連線                   

public object getconnect()

獲取url連線的內容

public inputstream getinputstream()

返回開啟連線的輸入流

public outputstream getoutputstream()

返回開啟鏈結的輸出流

public string getconnectionencoding

返回content-encoding頭字段的值

public int getcontentlength()

返回content-length頭字段的值

public string getcontenttype()

返回content-type頭字段的值

public long getexpiration()

返回expres頭字段的值

public long getdate()

返回date頭字段的值

三、httpurlconnection、利用該類獲取網頁內容、檔案、向網頁傳送請求引數以及傳送xml資料

通過httpurlconnection類獲取網頁內容的步驟

(1)建立乙個url物件

url url = new url("") ;

(2)得到httpurlconnection物件

(3)設定超時連線

con.setconnecttimeout(6000);

(4)對響應碼進行判斷

if(con.getresponsecode() != 200) throw new runtimeexception("請求失敗");

(5)得到網路返回的收入流

inputstream is = con.getinputstream();

string result = readdate(is,"gbk");

con.disconnection();

注:設定超時連線的好處,如果網路不好,android 系統會在超時預設時間後悔**資源,在步驟(5)中獲取網頁內容還有考            慮該網頁的具體編碼格式。

②獲取網頁檔案的步驟

如獲取網頁內容的形式類似,在定位的時候要指定其檔案的url位址,接受時要根據檔案的型別將其解析。

③向網頁傳送請求引數的步驟

(1)將請求引數儲存到byte陣列中

string para  = new string("username = admin&password = admin");

byte[ ] data  = para.getbytes();

(2)建立url連線物件

url url = new url("") ;

(3)獲得httpurlconnection物件

(4)設定允許輸出

con.setdoput(true);

(5)設定不使用快取

con,setuercaches(false);

(6)設定使用post方式傳送

con.setrequestmethod("post")

(7)設定維持長度連線

con.setrequestproperty("connection","keep - alive");

(8)設定檔案字符集

con.setrequestproperty("charset","utf - 8");

(9)設定檔案長度

con.setrequestproperty("content - length",string.valueof(data.length));

(10)設定檔案型別

(11)最後乙個流的方式輸出

con.getoutoutstrem().write(data);

④向intent傳送xml資料

(1)將生成的xml問價寫入byte 陣列中同時設定utf-8,其他步驟與上面③傳遞引數一致

android http 請求方式

android用 httpclient向伺服器發起get或者post請求 首先client引數設定 可選設定,設定超時 先將引數放入list,再對引數進行url編碼 listparams new linkedlist params.add new basicnamevaluepair param1 ...

Android Http連線之GET POST請求

在android sdk中提供了apache httpclient org.apache.http.模組。在這個模組中涉及到兩個重要的類 httpget和httppost。建立步驟 1 建立httpget 或httppost 物件,將要請求的url通過構造方法傳入httpget 或httppost ...

Android Http連線之GET POST請求

在android sdk中提供了apache httpclient org.apache.http.模組。在這個模組中涉及到兩個重要的類 httpget和httppost。建立步驟 1 建立httpget 或httppost 物件,將要請求的url通過構造方法傳入httpget 或httppost ...