iOS 網路程式設計 (1) 解析URL,提取資料

2021-06-19 15:31:19 字數 4005 閱讀 2427

通過網路獲取資料有兩種方法: 1.使用nsurl類的方法nsurlconnection 和 nsurlrequest;2.使用nsdata 的方法initcontentwithurl 或 datawithcontentsofurl。

方法一:

nsstring *googleurl = @"";

nsurl *url = [nsurl urlwithstring: googleurl];

nsurlrequest *request = [[nsurlrequest alloc] initwithurl: url];

nsurlconnection *connection = [[nsurlconnection alloc] initwithrequest delegate: self];

什麼是url?

首先,我們必須知道 url, url全稱為 uniformresourcelocator, 統一資源定位符,也即**。

**的形式一般為:

url的訪問方式: ftp (檔案傳送協議),http(超文字傳送協議),news (usenet usenet新聞)。

為什麼要使用 nsurl?

**是乙個字串,為什麼要轉換成nsurl的物件呢?因為**的字串都比較複雜,包括很多的請求引數,如此一來,在請求的過程中要解析出來每乙個部分,所以要封裝乙個nsurl。

例如:nsurl *url = [nsurl urlwithstring: @"http: // www.baidu.com/s? tn=baiduhome_pg&bs = nsurl&f = 8&rsv_bp = 1&rsv_spt = 1&wd = nsurl&inputt = 2709"];

nslog(@"scheme : %@", [url scheme]); scheme - 計畫

nslog(@"host : %@", [ url host]); host - 主機

nslog 、、、                                port - 埠

、、、                                            path

、、、                                            relativepath

、、、                                            pathcomponent

、、、                                            parameterstring - 引數字串

、、、                                            query - 疑問

、、、                                            fragment - 碎片, 不完整的部分

、、、                                            user

、、、                                            password

輸出的結果:

為什麼要使用 nsurlrequest ?

所構建的 nsurlrequest 具有乙個依賴於快取響應的特定策略,cachepolicy 取得策略,

timeoutinterval 取得超時值。

例如:// creating the connection with the request

// and start loading the data

nsurlconnection *theconnection = [[nsurlconnection alloc] initwithrequest: therequest delegate: self];

if (theconnection) else

委託的處理方法

-(void) connection : (nsurlconnection *) connection didreceivedata: (nsdata *) data

-(void) connection: (nsurlconnection *) connection didfailwitherror: (nserror *)error

-(void) connectiondidfinishloading: (nsurlconnection *) connection

方法2:

第二部分 在獲取了資料以後把資料解析(使用了第三方庫)

// 使用第三方庫

touchjson

// 獲取

api    

nsurl *url = [nsurl urlwithstring:@"

www.weather.com.cn/data/sk/101010100.html

"];    

// 定義乙個

nserror

物件,用於捕獲錯誤資訊

nserror *error;    

nsstring *jsonstring = [nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];     

// 將解析後的資料放到字典中,編碼格式為

utf8    

nsdictionary *rootdic = [[cjsondeserializer deserializer] deserialize:[jsonstringdatausingencoding: nsutf8stringencoding] error: &error];    

// 返回的json

檔案有兩層,取第二層的內容到字典中

nsdictionary *weatherinfo = [rootdic objectforkey:@"weatherinfo"];    

// 取值輸出

nslog(@"today is %@%@%@ .the condition of weather is %@%@",[weatherinfo objectforkey:@"date_y"], [weatherinfo objectforkey:@"week"], [weatherinfo objectforkey:@"city"], [weatherinfo objectforkey:@"weather1"], [weatherinfo objectforkey:@"temp1"]);

IOS 網路解析

網路解析同步非同步 get同步 ibaction get tb id sender 1.建立url nsurl url nsurl urlwithstring 2.建立乙個請求物件 nsmutableurlrequest requst nsmutableurlrequest requestwithu...

Java網路程式設計 URL

public string getprotocol 獲取該url的協議名 public string gethost 獲取該url的主機名 public string getport 獲取該url的的埠號 public string getpath 獲取該url的檔案路徑 public string...

ios網路程式設計

一 確認網路環境3g wifi 1.新增源 檔案和framework 開發web等網路應用程式的時候,需要確認網路環境,連線情況等資訊。如果沒有處理它們,是不會通過 使用reachability 只須將該例程中的 reachability.h 和 reachability.m 拷貝到你的工程中。如下...