Introduction to iOS Web data download and JSON parsing
In this article, I will show you how to use nsurlconnection to download data from the network and how to parse the downloaded JSON data format, and how to display the data and images of the asynchronous download display
Application of the knowledge 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 vast majority of applications on the iphone require a network to operate, or network applications.
The program structure of network application
Running a program on the network to provide data to the application or to provide services, the network application is generally called the client, and the service running on the network is called the service side.
Common forms of network interfaces
Common data interfaces for iOS Web applications are usually URL addresses in the form of HTTP.
General flow of interface development
Download Data Nsurlconnection+url
Parsing Data Json/xml
Building a data Model
Creating views using view Display
TableView + Custom Cell
Picture Asynchronous Download Sdweblmage
Use of 2NSURLConnection
NSString Downloading data synchronously
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"); }
nsurlconnection Sync Download
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"); }
Nsurlconnection Asynchronous Download
@property (copy,nonatomic) nsmutabledata *data;//function://Incoming URL, the download completes execution after executing the target object in the action Method-(void) Requestwithurl: ( NSString *) URL target: (ID) Target action: (SEL) Action; @interface zjhttprequest () <nsurlconnectiondatadelegate>{ Nsurlconnection *_connection; NSString *_url; ID _target; SEL _action;} @end @implementation zjhttprequest//function://Incoming URL, the download completes execution after executing the target object in the action Method-(void) Requestwithurl: (NSString *) URL Target: (ID) Target action: (SEL) action{_url = URL; _target = target; _action = action; Initiate URL request _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{//Download complete, execute Save method if (_target && [_target Res Pondstoselector:_Action]) {[_target performselector:_action withobject:self]; } }
3JSON format description and Formatting tools
JSON format
{
"Count": 20,
"Data": [
"Zhangsan",
"Lisi",
"Wangwu"
]
}
[] 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
Implementation of 41 completion pages (including model creation, sdwebimage use)
iOS Web data download and JSON parsing