IOS network data download and JSON parsing, and ios data json Parsing
Introduction to iOS network data download and JSON Parsing
In this article, I will introduce how to use NSURLConnection to download data from the network, parse the downloaded JSON data format, and display the asynchronous download of data and images.
Knowledge points involved:
1. NSURLConnection asynchronous download and Encapsulation
2. JSON format and JSON Format Parsing
3. data display and asynchronous image display using SDWebImage
Content 1. Introduction to basic network download knowledge
(1) What is a network application?
Generally, iPhone computers and cameras can run without downloading data from the network. Therefore, this type of application is a local application, but most applications on the iPhone can run through the network, for example, QQ, Xiami music, so in iOS development, you need to know how to download data from the network
(2) program structure of network applications
Different from local applications, network application data is downloaded from the network. Therefore, you need to run a program on the network to provide data or services for the application, therefore, this network application is generally called a client, and the service running on the network is called a server.
(3) Common network interface forms
IOS network application common data interface is half of the form of http url address, such as the love limit free application home page data address for http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id =
In a project, some open-source libraries are generally used to download data through this URL. For example, AFNetworking
(4) Common Data Formats
There are two common data formats in iOS development: JSON format and XML format. JSON format is usually used.
(5) general interface development process
Develop an interface in iOS that requires the interface, interface material resources, and network interfaces
The development process is generally as follows:
* ** 1. download data
* ** 2. parse JSON or XML data and create a data model.
* ** 3. Use Controls to display data and customize views when necessary, such as cell
2. Use NSURLConnection
NSString synchronous download data
// Use the URL address in HTTP // http: // protocol (ftp: //) // iappfree.candou.com host address (Domain Name and IP address )//: 8080 host port // free/applications/limited web page program file path //? Currency = RMB & page = 1 & category_id = program parameters (parameters are separated with &) NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // download data // generate the NSURL object NSError * error = nil through the address; 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 it in the project // 1. download in synchronous mode. After initWithContentsOfURL is downloaded, the system returns a result. // The interface is suspended and cannot be used. // 2. use asynchronous download (NSURLConnection asynchronous download)
NSURLCnnection synchronously downloads data
NSString * urlString = @ "http://iappfree.candou.com: 8080/free/applications/limited? Currency = RMB & page = 1 & category_id = "; // send a synchronous URL request // NSURLRequest URL request object 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 ");}
NSURLCnnection asynchronously downloads data
# Pragma mark-asynchronous download-(void) testNSURLConnectionAsyncDownloadData {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 // asynchronous: after the method is executed, start downloading and return immediately // The download process is executed in the background (multi-thread) _ connection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: urlString] delegate: self startImmediately: YES]; NSLog (@ "initWithRequest execution completed");} // proxy method: receive Server Response execution-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {NSLog (@ "receive Server response execution");} // proxy method: when receiving data, execute // Note: when the data is large, it may be executed multiple times-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {[_ data appendData: data];} // proxy method: data download completed-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSString * str = [[NSString alloc] initWithData: _ data encoding: NSUTF8StringEncoding]; NSLog (@ "str = % @", str);}-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "error = % @", error );}
3. JSON format description and formatting tool 4. Implementation of a complete page (including the creation of a model and the use of SDWebImage ):