IOS_GET _ network request

Source: Internet
Author: User

IOS_GET _ network request

Synchronous get request

# Pragma mark-synchronous get request-(IBAction) GETSynButtonDidClicked :( UIButton *) sender {// 1. The URL must contain http: // NSString * urlString = @ http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx? Date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // 2. If the website has Chinese characters that need to be converted (or can be written without Chinese characters) urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; // 3. Create a url Based on the string (Unified) NSURL * url = [NSURL URLWithString: urlString]; // 4. Create an object of the request class NSURLRequest * request = [NSURLRequest requestWithURL: url] based on the url. // 5. Start to request the network and return data NSData * receiveData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil] for data (synchronization); // 6. NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: receiveData options :( NSJSONReadingMutableContainers) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title );}}

Asynchronous get request

# Pragma mark-asynchronous get request-(IBAction) GETAsyButtonDidClicked :( UIButton *) sender {// 1. Splice urlString. The URL must contain http: // NSString * urlString = @ http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx? Date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213; // 2. Create a URL Based on the string (Uniform Resource Locator) NSURL * url = [NSURL URLWithString: urlString]; // 3. Create a request object NSURLRequest * request = [NSURLRequest requestWithURL: url] based on the url. // 4. Start to request the network and return data (synchronous) [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {// 5. NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options :( NSJSONReadingMutableContainers) error: nil]; NSArray * array = dict [@ news]; self. newsArray = [NSMutableArray array]; for (NSDictionary * smallDict in array) {NewsModal * modal = [[NewsModal alloc] init]; [modal convert: smallDict]; [self. newsArray addObject: modal];} for (NewsModal * modal in self. newsArray) {NSLog (% @, modal. title) ;}}] ;}

 

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.