NSURLConnection的Get\Post方法
2015-07-24 12:32
267 查看
习惯了用AFNetworking来处理网络请求,这次试试苹果源生控件的处理方式~~
#import "ViewController.h" @interface ViewController () <NSURLConnectionDataDelegate> @property (nonatomic, strong) NSMutableData *data; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupView]; } - (void)setupView { [self connection]; } - (void)connection { NSString *urlStr = [NSString stringWithFormat:@"这里存放所需要的url"]; //进行转码,以免中文乱码 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; //设置请求超时时间 request.timeoutInterval = 10.0; [self sendAsyncOnBlock:request]; } //发送get请求,用block回调 - (void)sendAsyncOnBlock:(NSMutableURLRequest *)request { NSOperationQueue *queue = [NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //注意,这里回调block的线程,我是设置为主线程的。因为往往,在这里面,需要更新UI界面,而在iOS开发中, //更新UI界面必须在主线程中更新~~ // //然后,解析json if (data){ //请求成功 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; //解析数据 } else{ //请求失败 //返回的json数据为空 } }]; } //使用代理方式,发送get请求 - (void)sendAsyncOnDelegate:(NSMutableURLRequest *)request { NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start]; } //发送post请求 - (void)sendPostReq { NSString *urlStr = [NSString stringWithFormat:@"这里存放所需要的url"]; //进行转码,以免中文乱码 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlStr]; //设置为post请求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; //默认是get请求 request.HTTPMethod = @"POST"; request.timeoutInterval = 10.0; //通过请求头,告知服务器,客户端的类型 [request setValue:@"ios" forHTTPHeaderField:@"User-Agent"]; //设置请求体:(必须要设置,没有具体数据,不做示范) // NSString *param = [NSString stringWithFormat:]; // request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; //发送请求 NSOperationQueue *queue = [NSOperationQueue mainQueue]; [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { }]; } #pragma mark -- NSURLConnectionDataDelegate //当接受到服务器的响应(连通了服务器)就会调用 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { self.data = [[NSMutableData alloc] init]; } //请求错误(失败)的时候调用(请求超时\断网\没有网, 一般指客户端错误) - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { } //当接受到服务器的数据就会调用(可能会被调用多次, 每次调用只会传递部分数据) - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //在这里,当下载大文件资源时,是需要拼接数据的 [self.data appendData:data]; } //当服务器的数据接受完毕后就会调用 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { } @end
相关文章推荐
- jQuery 3.0 的变化
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第36讲:PHP错误与异常处理
- storm安装集群配置详情
- Android中ImageView centerInside与fitCenter区别
- PS快捷键大全
- 基于JS实现的倒计时程序实例
- javaGUI解决闪屏问题
- String,字符串处理
- Spring 学习笔记-- <mvc:default-servlet-handler />默认加载simpleurlhandlermapping
- crossApp初级-CAView类-5
- word-wrap和word-break
- php之类常量/魔术常量/延迟绑定
- 自动生成makefile的脚本
- Objective-C学习笔记(二十一)——函数的返回值与参数类型
- mysql缓存和日志
- 分治 hihoCoder1050 树中的最长路
- Handling Mixed (HTTPS/HTTPS) Content
- 南阳理工 927 The partial sum problem
- C++中替代sprintf的std::ostringstream输出流详解
- Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值