viewcontroller.m//ui2_ sync Download////Created by zhangxueming on 15/7/17.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "ViewController.h"//http://iappfree.candou.com:8080/free/applications/limited? currency=rmb&page= pages @interface Viewcontroller () {nsurlconnection *_urlconnection;//provided by Apple, to the client and the server to establish a connection NSMuta Blearray *_dataarray; Provide data} @end//Synchronous Download: When sending a network request to the server, the UI cannot interact during the request data, only after the server has returned the data to perform subsequent operations//requests for a smaller amount of data, special requirements @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Additional setup after loading the view, typically from a nib. [Self loaddatawithpage:4];} -(void) Loaddatawithpage: (Nsinteger) page{//Create URL object nsstring *urlstring = [NSString stringwithformat:@] Http://iappfr Ee.candou.com:8080/free/applications/limited?currency=rmb&page=%ld ", page]; Nsurl *url = [Nsurl urlwithstring:urlstring]; Create data Request object//cachepolicy Cache protocol is an enumeration type://nsurlrequestuseprotocolcachepolicy basic policy//nsurlrequestreloadignOringlocalcachedata Ignore local cache//nsurlrequestreturncachedataelseload First use cache, if there is no local cache, download from the original address// Nsurlrequestreturncachedatadontload uses the local cache, never downloads, and if there is no cache locally, the request fails. This policy is mostly used for offline operations//nsurlrequestreloadignoringlocalandremotecachedata ignoring any cache policy, whether local or remote, always re-downloaded from the original address// Nsurlrequestreloadrevalidatingcachedata If the local cache is valid, it is not downloaded. Any other cases are re-downloaded from the original address nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:10]; Status Response//404 (request failed) 200 (request succeeded) 400 (client syntax error) 500 (service-side error) Nshttpurlresponse *response= Nil; Nserror *error = nil; NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; Print Status Code NSLog (@ "%@", response); NSLog (@ "code =%li", response.statuscode); NSLog (@ "header =%@", response.allheaderfields); ID result = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error:nil]; NSLog (@ "result=%@", result);} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
ui2_ Sync Download