iOS Web data download and JSON parsing
Introduction
In this article, I'm going to show you how to use nsurlconnection to download data from the Web and how to parse the downloaded JSON data format, and how to display the data and the asynchronous download display of the image.
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?
Network applications are some applications that can be used normally in a network environment.
The program structure of network application
c/S structure
Client clients: display the data and communicate with the user .
Server servers: Provide data to clients and provide services .
General flow of interface development
1. You first need to know what kind of interface you are doing, and then you can see what material and network interface you need.
2. Use nsurlconnection to download data asynchronously.
3. The data model is established by analyzing the data.
4. Define a Uitabbarviewcell to display the interface.
Use of 2.NSURLconnection
Here we are encapsulating the method of this asynchronous download
@interfaceZxhttprequest () <NSURLConnectionDataDelegate>{nsstring*_url; SEL _action; Nsurlconnection*_connection; ID_target;}@implementationzxhttprequest-(void) Requestwithurl: (NSString *) URL target: (ID) Target action: (SEL) Action; {_url=URL; _action=Action; _target=Target; _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{[_target performselector:_action withobject:self];}
NSString Downloading data synchronously
We write it in this method, the data interface is the love limit of data interface, synchronous download we are not available, because if you want to download the data is relatively large, it is easy to get stuck in this method can not come out, our asynchronous download is running in the background download, But the synchronous download is the data after the download and then return to the method of jumping out. This is their difference and is also a fatal flaw in simultaneous downloads.
-(void) testnsstringdownloaddata{NSString*urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; 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"); } }
3.JSON format description and Formatting tools
The most important thing in JSON format is to find out the nested relationships in its data, it is very simple to parse the method, the focus is not on the method, in the nested relationship, we through a Jason's parsing software can clearly see it inside nested data structure, recommend to everyone.
This is the method of parsing:
Nsdictionary *dic=[nsjsonserialization jsonobjectwithdata: (NSData *) options: (nsjsonreadingoptions) Error: (NSError * __autoreleasing *)
4. Implementation of a complete interface (including model creation)
:
iOS Web data download and JSON parsing