Introduction to iOS Web data download and JSON parsing
In this article, I will introduce how iOS can use nsurlconnection to download data from the network, 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 Network Download basics
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.
The program structure of network application
Network application is different from the local application, the network application data is downloaded from the network, so need to run a program on the network for the application to provide data, or provide services, then this network application is generally called the client, and the service running on the network is called the service side.
Common forms of network interfaces
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.
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, and relatively more in JSON format.
General flow 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
nsurlconnection Sync Download
//nsurlconnection sync Download-(void) testnsurlconnectionsyndownloaddata{NSString*urlstring=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";//send a sync URL request//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 (@"Download Failed"); }//}
Nsurlconnection Asynchronous Download
//nsurlconnection Asynchronous Download#pragmaMark Asynchronous Download-(void) testnsurlconnectionasyncdownloaddata{_data=[[Nsmutabledata alloc]init]; NSString*urlstring=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //initiated a URL asynchronous download request//Async: After executing the method download, immediately return, the download process in the background (multi-threaded) execution_connection=[[nsurlconnection alloc]initwithrequest:[nsurlrequest Requestwithurl:[nsurl URLWithString:urlstring]]Delegate: Self startimmediately:yes]; }//Proxy method, accepted to server response execution-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@"accept the server response execution");}//Proxy method: Executes when the data is received//when data is large, it 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]initwithdata:_data encoding:nsutf8stringencoding]; NSLog (@"str:%@", str); Nsdictionary*dic=[nsjsonserialization jsonobjectwithdata:_data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@"dic=%@", DIC);//Get//Nsarray *arr=dic[@ "Applications"];Nsarray *arr=[dic Valueforkey:@"Applications"]; for(Nsdictionary *dictincharr) {NSLog (@"name=%@", dict[@"name"]); }}//can not write-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@"error:%@", error);}
3.JSON format description and Formatting tools
4. Implementation of a complete interface
iOS Web data download and JSON parsing