异步get请求之代理方法
#import "ViewController.h"
#import "Header.h" @interface ViewController ()<NSURLSessionDataDelegate> /**
* 用于保存相关的数据
*/
@property (nonatomic, strong) NSMutableData *resultData; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} // 对数据进行加载:使用NSURLSessionDataTask和NSURLSessionTask两者没有本质区别
// 要处理下载任务的使用使用此任务NSURLSessionDownloadTask
// 要处理上传任务使用:NSURLSessionUploadTask #pragma mark - get请求(异步)
- (IBAction)getRequest:(UIButton *)sender { // 1.创建url
NSURL *url = [NSURL URLWithString:GET_URL]; // 2.创建session对象
// 参数一:模式的设置
/*
defaultSessionConfiguration 默认会话模式
ephemeralSessionConfiguration 瞬时会话模式
backgroundSessionConfigurationWithIdentifier 后台会话模式
*/
// 参数二:代理
// 参数三:线程队列
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; // 3.创建task对象
NSURLSessionDataTask *task = [session dataTaskWithURL:url]; // 4.启动任务
[task resume]; } #pragma mark - 实现协议方法
// 服务器开始响应
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { // 允许服务器响应【在这个地方只有允许服务器响应了才会接收到数据】
completionHandler(NSURLSessionResponseAllow); // 初始化data,稍后进行片段的拼接存储
self.resultData = [NSMutableData data];
} // 接收数据拼接
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data { // 反复执行,然后拼接相关的片段
[self.resultData appendData:data];
} // 数据接收完成,网络请求结束
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { // 解析
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.resultData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dic);
}
} @end
异步get请求之代理方法的更多相关文章
- 异步post请求之代理方法
#import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...
- 异步post请求之Block方法
#import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...
- 异步get请求之Block方法
#import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...
- 弃用的异步get和post方法之代理方法
#import "ViewController.h" #import "Header.h" @interface ViewController () <N ...
- ios网络学习------2 用非代理方法实现同步post请求
#pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark post登录方法 -(void) ...
- iOS 多个异步网络请求全部返回后再执行具体逻辑的方法
对于dispatch多个异步操作后的同步方法,以前只看过dispatch_group_async,看看这个方法的说明: * @discussion * Submits a block to a dis ...
- PHP异步请求之fsockopen()方法详解
正常情况下,PHP执行的都是同步请求,代码自上而下依次执行,但有些场景如发送邮件.执行耗时任务等操作时就不适用于同步请求,只能使用异步处理请求. 场景要求: 客户端调用服务器a.php接口,需要执行一 ...
- IOS9中使用NSURLConection发送异步网络请求
IOS9中使用NSURLConection发送异步网络请求 在ios9中,NSURLConection的sendSync..和sendAsync已经过时.被NSURLSession代替. 以下蓝色部分 ...
- iOS_网络请求_代理方式
#pragma mark - 网络请求代理方式(异步) - (IBAction)DelegateButtonDidClicked:(UIButton *)sender { // 1.拼接 urlStr ...
随机推荐
- smartjs - DataManager 场景示例分析 - 数据懒加载
发一张policy的参数图设置图: 场景1 - 数据的懒加载/延迟加载 在很多时候,为了提高网页的加载速度,减少不必要的开销,会将页面的数据拆分成几个部分,首先加载呈现可视区域内的数据,然后剩下来的会 ...
- Unity 学习资源
NGUI文档及视频: http://www.tasharen.com/forum/index.php?topic=6754 动态更新的解决方案: http://game.ceeger.com/foru ...
- (转)ubuntu安装opengl
$ sudo apt-get install build-essential 安装OpenGL Library接下来要把我们会用到的 Library 装上去,首先安装 OpenGL Library $ ...
- 转 iOS Core Animation 动画 入门学习(一)基础
iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...
- 高手详解SQL性能优化十条建议
1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便 ...
- Watch out for these 10 common pitfalls of experienced Java developers & architects--转
原文地址:http://zeroturnaround.com/rebellabs/watch-out-for-these-10-common-pitfalls-of-experienced-java- ...
- JS微信分享不好写?来封装一下
微信开发这块,作为开发工程师来说,一般是避免不了的,也好像发现一些朋友写微信分享都是在每个页面一大把一大把的代码. 代码冗余,即便是复制过来再改也很麻烦. 之前自己封装了一下js,今天来分享一下,希望 ...
- 快速清除文件夹svn版本控制信息
将下面内容另存为clear.bat文件,在有版本控制的目录执行即可 @echo On @Rem 清除SVN版本控制信息 @for /r . %%a in (.) do @if exist " ...
- JavaScript中的数组详解
JavaScript中的数组 一.数组的定义 数组是值的有序集合,或者说数组都是数据的有序列表. 二.创建数组 [字面量形式] 1.空数组 var arr=[]; 2.带有元素的数组 var arr= ...
- Do not to test a private method.
If you want to unit test a private method, something may be wrong. Unit tests are (generally speakin ...