Introduction to iOS Web data download and JSON parsing
In this article, I'm going to show you how to use nsurlconnection to download data from the Web, how to parse the downloaded data format, and how to display the asynchronous download display of data and images.
The knowledge points involved are:
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
(1) What is a Web application?
In general, the iphone computer, the camera does not need to download data from the network can also run, so this type of application is a local application, but the vast majority of applications on the iphone need a network to run, such as QQ, shrimp music, So in iOS development, you need to know how to download data from the Web.
(2) Program Structure of network application
Network application is different from local application, network application data is downloaded from the network, so need to run a program on the network to provide data for the application, or provide services, then this network application is generally called the client, and the service running on the network is called the server
(3) Common form of network interface
iOS Web application Common Data Interface half is the HTTP form of the URL address, such as the Love Limit application first page of the data address is http://iappfree.candou.com:8080/free/applications/limited? Currency=rmb&page=1&category_id=
Some open source libraries are commonly used in projects to download data from this web site. such as Afnetworking
(4) Common data formats
There are two types of data formats commonly found in iOS development, one in JSON format and the other in XML format, with relatively more JSON format
(5) General process of interface development
Developing an interface in iOS that requires interfaces, interface assets, and network interfaces
The process of development is generally as follows
1, download data
2, parsing JSON or XML data, creating a data model
3, use the control to display data, when necessary to customize the view, such as custom cell
2.NSURLConnection use
NSString Downloading data synchronously
Use the URL address/HTTP/HTTP address using Protocol (ftp://) // iappfree.candou.com host address (domain name and IP) // : 8080 host port // /free/applications/limited Web program file path // ?currency=rmb&page=1& Category_id= program parameters (Parameters & Split) nsstring *urlstring = @ "http://iappfree.candou.com:8080/free/applications/ Limited?currency=rmb&page=1&category_id= "; Download data //Generate Nsurl object via address nserror *error = nil; Nsurl *url = [Nsurl urlwithstring:urlstring]; NSString *content = [[NSString alloc] Initwithcontentsofurl:url encoding:nsutf8stringencoding error:&error]; if (error==nil) { NSLog (@ "content =%@", content); } else { NSLog (@ "Download Failed"); } How to use//1 in the project . Synchronous form Download, initwithcontentsofurl after downloading will not return // cause the interface suspended animation, can not use //2. Use asynchronous Download ( Nsurlconnection asynchronous download)
Nsurlcnnection Downloading data synchronously
NSString *urlstring = @ "http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1& Category_id= "; Send a Sync URL request //nsurlrequest URL Request object nsurl *url = [Nsurl urlwithstring:urlstring]; Nsurlrequest *request = [Nsurlrequest requestwithurl:url]; Nserror *error = nil; 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 (@ "Download Failed"); }
Nsurlcnnection Downloading data asynchronously
#pragma mark-Asynchronous download-(void) testnsurlconnectionasyncdownloaddata{nsstring *urlstring = @ "http://iappfree.candou.com : 8080/free/applications/limited?currency=rmb&page=1&category_id= "; Initialize _data = [[Nsmutabledata alloc] init]; Initiated an asynchronous URL connection request//async: After executing the method to start the download, the immediate return//download process is performed in the background (multithreading) _connection = [[Nsurlconnection alloc] Initwithrequest:[nsurlrequest Requestwithurl:[nsurl urlwithstring:urlstring]] delegate:self StartImmediately:YES]; NSLog (@ "initwithrequest execution complete"); }//Proxy method: Received server response execution-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{ NSLog (@ "Received server response Execution");} Proxy method: When the data is received execution//NOTE: When the data is larger, may be executed multiple times-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{ [_data appenddata:data];} Proxy method: Data download complete-(void) connectiondidfinishloading: (nsurlconnection *) connection{nsstring *str = [[NSString alloc] Init Withdata:_data encoding:nsutf8stringencoding]; NSLog (@ "str =%@",STR); }-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "error =%@", error);}
3.JSON format description and Formatting Tools 4. Implementation of a complete page (including model creation, sdwebimage use):
iOS Web data download and JSON parsing