iOS之網路資料下載和JSON解析

來源:互聯網
上載者:User

標籤:

iOS之網路資料下載和JSON解析簡介   

    在本文中筆者將要和大家介紹iOS中如何利用NSURLConnection從網路上下載資料,如何解析下載下來的JSON資料格式,以及如何顯示資料和圖片的非同步下載顯示

    涉及到的知識點

1.NSURLConnection非同步下載和封裝

2.JSON格式和JSON格式解析

3.資料顯示和使用SDWebImage非同步顯示圖片

內容 

1.網路下載基礎知識介紹

什麼是網路應用?

    一般情況下, iPhone的電腦, 照相機不需要從網路上下載資料也能運行, 所以這種類型的應用是本地應用, 但是iPhone上絕大多數的應用都需要網路才能運行, 例如QQ, , 蝦米音樂, 所以在iOS開發中需要知道如何從網路上下載資料。

 

網路應用的程式結構

  網路應用不同於本地應用, 網路應用的資料是從網路上下載下來的, 所以需要在網路上運行一個程式為應用提供資料, 或者提供服務, 那麼這個網路應用一般稱為用戶端, 而網路上啟動並執行服務稱為服務端。

 

常見的網路介面形式

  iOS網路應用常見的資料介面一半都是HTTP形式的URL地址, 例如愛限免應用首頁的資料地址為http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=

  在項目中一般使用一些開源庫通過這種網址下載資料. 例如AFNetworking。

 

常見的資料格式

  iOS開發中常見的資料格式有兩種, 一種是JSON格式, 另外種是XML格式, 相對來說, JSON格式使用的比較多。

 

介面開發的一般流程

    iOS中開發一個介面, 需要介面, 介面素材資源, 和網路介面

    開發的流程一般如下所示

    1, 下載資料

    2, 解析JSON或XML資料, 建立資料模型model

    3, 使用控制項顯示資料, 必要的時候定製視圖, 例如定製cell

 

2.NSURLConnection使用

NSURLConnection同步下載

//    NSURLConnection同步下載-(void)testNSURLConnectionSynDownLoadData{    NSString *urlstring=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";//     發送同步URL請求//    NSURL *url=[NSURL URLWithString:urlstring];    NSError *error=nil;    NSURLRequest *request=[NSURLRequest requestWithURL:url];        NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];        if(error==nil)    {        NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"str:%@",str);            }    else{        NSLog(@"下載失敗");    }//}

 

NSURLConnection非同步下載

//    NSURLConnection非同步下載#pragma mark 非同步下載-(void)testNSURLConnectionAsyncDownloadData{    _data=[[NSMutableData alloc]init];    NSString *urlstring=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";    //    發起了一個URL非同步下載請求    //    非同步:執行了方法之後下載,立即返回,下載過程在後台(多線程)執行    _connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstring]] delegate:self startImmediately:YES];    }//代理方法,接受到伺服器響應執行-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"接受到伺服器響應執行");}//代理方法:接收到資料時執行//資料較大時,可能多次執行-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [_data appendData:data];}//代理方法:資料下載完成-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSString *str=[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding ];    NSLog(@"str:%@",str);        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];    NSLog(@"dic=%@",dic);//    擷取//    NSArray *arr=dic[@"applications"];    NSArray *arr=[dic valueForKey:@"applications"];    for(NSDictionary *dict in arr)    {        NSLog(@"name=%@",dict[@"name"]);    }}//可以不寫-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"error:%@",error);}

 

3.JSON格式說明和格式工具

4.一個完整介面的實現

 

iOS之網路資料下載和JSON解析

聯繫我們

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