Introduction to iOS Web data download and JSON parsing
In this article I will give you an introduction to how iOS uses URLConnection to download data from the Web, how to parse the downloaded JSON data format, and how to display a download display of data and images
The knowledge points involved:
1.URLConnection Asynchronous Download and encapsulation
2.JSON format and JSON format parsing
3. Data display and use Sdwebimage to display images asynchronously
Content 1. Network Download Basics What is a Web application?
Need to connect to the network of applications, such as:, QQ, foot kee, etc.;
Computers, cameras, calendars and so on do not need to connect to the network called local applications.
The program structure of network application
Need to have client and server side
The service side is for the client service, the content of the service such as providing resources to the client, saving the client data.
Client, or user-side, is the program that corresponds to the server and provides local services to the customer
Common forms of network interfaces
iOS Web application Common Data Interface half is the HTTP form of the URL address;
Other network data interfaces: Http,ftp,https
Common data formats
There are two kinds of data formats commonly used in iOS development, one is JSON format, the other is XML format, relatively speaking, the JSON format uses more than 75%,xml accounted for 20%, the remainder accounted for 5%
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.URLConnection use
NSString Sync Download
NSString *urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; NSLog (@"%@", urlstring);//Download Data//generating Nsurl objects from addressesNserror *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 the project//1. Synchronous form Download, initwithcontentsofurl after downloading will not return to cause the interface suspended animation, can not use//2. Using asynchronous Download (nsurlconnection asynchronous download)
nsurlconnection Sync Download
NSString *urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //send a sync URL request//nsurlrequest URL RequestNsurl *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"); }
Nsurlconnection Asynchronous Download
-(void) testtestnsurlconnectionasyncdownloaddata{NSString*urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //Initialize_data =[[Nsmutabledata alloc]init];//initiates an asynchronous URL connection request//Async: Executes the method after the download begins, returns immediately, the download process is performed in the background (multithreaded)_connection = [[Nsurlconnection alloc]initwithrequest:[nsurlrequest Requestwithurl:[nsurl URLWithString:urlString]]Delegate: Self startimmediately:yes]; NSLog (@"initwithrequest Execution Complete"); }//Proxy method: Receives response execution from the server-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@"response received from the server");}//Proxy method: Executes when the data is received//Note: The data may be executed more than once when it is large-(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{[_data appenddata:data];}//Proxy method: The data download is complete-(void) Connectiondidfinishloading: (Nsurlconnection *) connection{NSString*str=[[NSString Alloc]initwithdata:_data encoding:nsutf8stringencoding]; NSLog (@"str =%@", str); //the simplest real name.//parsing JSON//function: JSON data converted to nsdictionary or NsarrayNsdictionary *dic =[nsjsonserialization jsonobjectwithdata:_data options:nsjsonreadingmutablecontainers Error:nil]; Nsarray*applist = dic[@"Applications"]; for(Nsdictionary *appdicinchapplist) {NSLog (@"name =%@", appdic[@"name"]); }}//Proxy method: Data download Failed-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@"error =%@", error);}
3.JSON format description and Formatting tools
{ "List" : [ { "Isxuanzuo":0, "Name":"Wang Feng 2014 "Peak Storm coming" Super Tour Chongqing station", "Pricestr":"180,280,380,480,680,980,1280", "ProjectID":66611, "ShowTime":"2014.11.01", "Sitestatus":8, "Summary":""pre-paid notice" May 12, 2014 12:08 Open first pay! ", "Venid":258, "Venname":"Chongqing Olympic Sports Center", "CityName":"Chongqing City", "Pricename":"180-1280 USD" }, { "Isxuanzuo":0, "Name":"Karen Mok's 20-year concert tour Zhengzhou station", "Pricestr":"480,680,880,1280,1680,2014", "ProjectID":66313, "ShowTime":"2014.07.26", "Sitestatus":8, "Summary":""Client Exclusive" 480 yuan a few rare ticket clients exclusively sold! ", "Venid":942, "Venname":"Zhengzhou International Convention and Exhibition Center", "CityName":"Zhengzhou", "Pricename":"480-2014 USD" }, { "Isxuanzuo":0, "Name":"Wang 2014music-manii "full-fire" world tour-Beijing station", "Pricestr":"280,380,480,580,880,1280,1680", "ProjectID":66052, "ShowTime":"2014.06.14", "Sitestatus":8, "Summary":"Tank on stage firepower, create music campaign! A new interpretation of classic works, the experience of extraordinary music charm! ", "Venid":392, "Venname":"National Stadium", "CityName":"Beijing", "Pricename":"280-1680 USD" } ]}
[] represents the array, corresponding to the Nsarray
That represents the data that is tied
{} represents the dictionary, corresponding to the Nsdictionary
: represents a key-value pair
"xxx" means string, corresponding to NSString
corresponding NSNumber
JSON format formatting tool
Jason
Json Editor
Online: http://www.kjson.com/
4. Implementation of a complete page (including model creation, use of sdwebimage)
:
My Code: Download
iOS Web data download and JSON parsing