iOS Web data download and JSON parsing

Source: Internet
Author: User

Introduction to iOS Web data download and JSON parsing

In this article, I want to show you how to use nsurlconnnection to download data from the network in iOS, how to parse the downloaded JSON data format, and how to display the asynchronous download display of data and images.

The knowledge points involved:

1.NSURLConnection Asynchronous Download and encapsulation
2.JSON format and JSON format parsing

3. Data display and use Sdwebimage to display images asynchronously

Content 1. Introduction to basic knowledge of network download

What is a Web application?

The program structure of network application

Common forms of network interfaces

Common data formats

General flow of interface development

2. Nsurlconnection use
#import "ZJHttpRequest.h"//Elimination of Performselector warnings#pragmaClang diagnostic ignored "-warc-performselector-leaks"//class Extension//Project Practice://Some instance variables are used internally and don't want to be placed in the header file.@interfaceZjhttprequest () <NSURLConnectionDataDelegate>{nsurlconnection*_connection; NSString*_url; ID_target; SEL _action;}@end@implementationzjhttprequest//function://incoming URLs, executing the action method in the target object after the download is complete-(void) Requestwithurl: (NSString *) URL target: (ID) Target action: (SEL) action{_url=URL; _target=Target; _action=Action; //initiating a URL request_data =[[Nsmutabledata alloc] init]; _connection= [[Nsurlconnection alloc] initwithrequest:[nsurlrequest requestwithurl:[nsurl Urlwithstring:url]]Delegate: Self startimmediately:yes];}-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{[_data appenddata:data];}-(void) Connectiondidfinishloading: (Nsurlconnection *) connection{//The download is complete and the method of saving is executed .    if(_target &&[_target respondstoselector:_action])    {[_target performselector:_action withobject:self]; }    }@end

3. JSON format description and formatting tools
- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Data InterfaceNSString *urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; _dataarray=[[Nsmutablearray alloc] init]; //Download_request =[[Zjhttprequest alloc] init];        [_request requestwithurl:urlstring target:self Action: @selector (dealdowloadfinish:)]; [Self Createtableview];}#pragmamark-createtableview-(void) createtableview{_tableview=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; _tableview.datasource=Self ; _tableview.Delegate=Self ; [Self.view Addsubview:_tableview];}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return_dataarray.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellid =@"Cell"; Appcell*cell =[TableView Dequeuereusablecellwithidentifier:cellid]; if(Cell = =Nil) {Cell=[[Appcell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid]; }    //Config cellAppmodel *model =_dataarray[indexpath.row]; //cell.textLabel.text = model.name;Cell.nameLabel.text =Model.name;            [Cell.iconimageview Setimagewithurl:[nsurl URLWITHSTRING:MODEL.ICONURL]; returncell;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return  the;}-(void) Dealdowloadfinish: (Zjhttprequest *) request{nsdictionary*dict =[nsjsonserialization JSONObjectWithData:request.data options:nsjsonreadingmutablecontainers Error:nil]; //NSLog (@ "dict =%@", dict);Nsarray *applist = dict[@"Applications"];  for(Nsdictionary *appdictinchapplist) {Appmodel*model =[[Appmodel alloc] init]; Model.applicationid= appdict[@"ApplicationID"]; Model.name= appdict[@"name"]; Model.iconurl= appdict[@"ICONURL"];    [_dataarray Addobject:model]; }        //Download Data Refresh display[_tableview reloaddata];}

4. A complete page point implementation (including model creation, sdwebimage use)

iOS Web data download and JSON parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.