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) ;}}] ;}