UI2

it2024-11-11  7

// // ViewController.m // UI2_同步下载 // // 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=页数 @interface ViewController () { NSURLConnection *_urlConnection;//苹果官方提供的, 给客户端与服务器建立连接 NSMutableArray *_dataArray; //提供数据 } @end //同步下载:当向服务器,发送网络请求后, 请求数据期间, UI不能交互,只有在服务器返回数据之后才能进行后续的操作 //请求的数据量比较小,特殊要求 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self loadDataWithPage:4]; } - (void)loadDataWithPage:(NSInteger)page { //创建URL对象 NSString *urlString = [NSString stringWithFormat:@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=%ld", page]; NSURL *url = [NSURL URLWithString:urlString]; //创建数据请求对象 //cachePolicy 缓存协议是个枚举类型: //NSURLRequestUseProtocolCachePolicy 基础策略 //NSURLRequestReloadIgnoringLocalCacheData 忽略本地缓存 //NSURLRequestReturnCacheDataElseLoad 首先使用缓存,如果没有本地缓存,才从原地址下载 //NSURLRequestReturnCacheDataDontLoad 使用本地缓存,从不下载,如果本地没有缓存,则请求失败。此策略多用于离线操作 //NSURLRequestReloadIgnoringLocalAndRemoteCacheData 无视任何的缓存策略,无论是本地还是远程,总是从原地址重新下载 //NSURLRequestReloadRevalidatingCacheData 如果本地缓存是有效的则不下载。其他任何情况都从原地址重新下载 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; //状态响应 //404(请求失败) 200(请求成功) 400(客户端语法错误) 500(服务端错误) NSHTTPURLResponse *response= nil; NSError *error = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //打印状态码 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 that can be recreated. } @end

 

转载于:https://www.cnblogs.com/0515offer/p/4655042.html

相关资源:数据结构—成绩单生成器
最新回复(0)