iOS Web data download and JSON parsing

Source: Internet
Author: User

Introduction to iOS Web data download and JSON parsing

In this article, I will give you an introduction to how iOS uses nsurlconnection to download data from the Web, how to parse the downloaded JSON data format, and how to display asynchronous downloads of data and images

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 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 in iOS development, you need to know how to download data from the Web.

(2) Program Structure of network application

Network application is different from local application, network application data is downloaded from the network, so need to run a program on the network to provide data for the application, or provide services, then this network application is generally called the client, and the service running on the network is called the server

(3) Common form of network interface

iOS Web application Common Data Interface half is the HTTP form of the URL address, such as the Love Limit application first page of the 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, with relatively more JSON format

(5) General process 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.NSURLConnection use

NSString Downloading data synchronously

//use URL address in HTTP//http://Address Usage Protocol (ftp://)    //iappfree.candou.com host address (domain name and IP)//: 8080 Host port///free/applications/limited Web program file path//currency=rmb&page=1&category_id= Program parameters (Parameters & Segmentation)NSString*urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //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 download, Initwithcontentsofurl will not be returned until download is finished//causes the interface to feign death, cannot use//2. Using asynchronous Download (nsurlconnection asynchronous download)

Nsurlcnnection Downloading data synchronously

NSString *urlstring =@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id="; //send a sync URL request//nsurlrequest URL Request ObjectNsurl *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 Downloading data asynchronously

#pragmaMark-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]; //an asynchronous URL connection request was initiated//async: Starts the download after executing the method, immediately returns//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: Received the server response execution-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@"receiving server response execution");}//Proxy method: Executes when the data is received//Note: When the data is large, it may be executed multiple times-(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); }-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@"error =%@", error);}

3.JSON format description and Formatting Tools 4. Implementation of a complete page (including model creation, sdwebimage use):

iOS Web data download and JSON parsing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.