Introduction to iOS Web data download and JSON parsing
In this article, I'm going to show you how iOS uses nsurlconnection to download data from the Web, how to parse the downloaded JSON data format, and how to display the asynchronous download display of data and images.
Related to knowledge points 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 iOS development needs to know how to download data from the network.
(2) 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 to provide data for the application, or to provide services, then this network application is generally called the client, and the service running on the network is called the service side;
(3) Common form of network interface
iOS Web application Common Data interface is the HTTP form of the URL address, such as the Love Limit app home page 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, and, relatively speaking, the JSON format uses more
(5) General process of interface development
iOS development I interface, need interface, interface material resources and network interface
The process of development is generally as follows
1. Download the data
2. Parsing JSON or XML data, creating a data model
3. Use the controls to display the data, and customize the view when necessary, such as customizing the cell.
2.NSURLConnection use
NSString Download Data synchronously ...-(void) testnsstringdownloaddata
{nsstring*urlstr=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //1.http is the transport protocol name//2.iappfree.candou.com is the domain nameNsurl*url=[Nsurl Urlwithstring:urlstr]; Nserror*error=Nil; NSString*content=[[nsstring Alloc]initwithcontentsofurl:url encoding:nsutf8stringencoding error:&ERROR]; if(Error = =Nil) {NSLog (@"content =%@", content); } Else{NSLog (@"Download Failed"); } //the above for synchronous download, can not be used;//}-(void) nsurlconnectionsyndownloaddata{NSString*urlstr=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; Nserror*error=Nil; Nsurl*url=[Nsurl Urlwithstring:urlstr]; 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); } Else{NSLog (@"Download Failed"); } }
3.JSON format description and Formatting Tools 4. A full page point implementation (including model creation, sdwebimage use)
iOS Web data download and JSON parsing