异步get请求之代理方法

时间:2022-11-09 08:44:35
 #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请求之代理方法的更多相关文章

  1. 异步post请求之代理方法

    #import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...

  2. 异步post请求之Block方法

    #import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...

  3. 异步get请求之Block方法

    #import "ViewController.h" #import "Header.h" @interface ViewController ()<NS ...

  4. 弃用的异步get和post方法之代理方法

    #import "ViewController.h" #import "Header.h" @interface ViewController () <N ...

  5. ios网络学习------2 用非代理方法实现同步post请求

    #pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark post登录方法 -(void) ...

  6. iOS 多个异步网络请求全部返回后再执行具体逻辑的方法

    对于dispatch多个异步操作后的同步方法,以前只看过dispatch_group_async,看看这个方法的说明: * @discussion * Submits a block to a dis ...

  7. PHP异步请求之fsockopen&lpar;&rpar;方法详解

    正常情况下,PHP执行的都是同步请求,代码自上而下依次执行,但有些场景如发送邮件.执行耗时任务等操作时就不适用于同步请求,只能使用异步处理请求. 场景要求: 客户端调用服务器a.php接口,需要执行一 ...

  8. IOS9中使用NSURLConection发送异步网络请求

    IOS9中使用NSURLConection发送异步网络请求 在ios9中,NSURLConection的sendSync..和sendAsync已经过时.被NSURLSession代替. 以下蓝色部分 ...

  9. iOS&lowbar;网络请求&lowbar;代理方式

    #pragma mark - 网络请求代理方式(异步) - (IBAction)DelegateButtonDidClicked:(UIButton *)sender { // 1.拼接 urlStr ...

随机推荐

  1. Could not find result map com&period;lrlz&period;weixin&period;dao&period;SysUserDAO&period;SysUser

    <select id="selectByTimeCount" resultType="java.lang.Integer" parameterType=& ...

  2. MySQL大数据量快速分页实现(转载)

    在mysql中如果是小数据量分页我们直接使用limit x,y即可,但是如果千万数据使用这样你无法正常使用分页功能了,那么大数据量要如何构造sql查询分页呢?     般刚开始学SQL语句的时候,会这 ...

  3. sql-将一个表中的数导入另一个表中

    1.如果2张表的字段一致,并且希望插入全部数据 可以用这种方法: INSERT INTO 目标表 SELECT * FROM 来源表;例: insert into insertTest select ...

  4. &semi;function&lpar;&dollar;&comma;undefined&rpar; 前面的分号是什么用处

    ;function($,undefined) 前面的分号是什么用处 ;(function($){$.extend($.fn...现般在一些 JQuery 函数前面有分号,在前面加分号可以有多种用途:1 ...

  5. Binary Tree Level Order Traversal&comma;Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  6. app 性能

    Android App优化之性能分析工具 https://www.jianshu.com/p/da2a4bfcba68 -------------- 系列文: 背景:Android App优化, 要怎 ...

  7. 【【洛谷P2678 跳石头】——&percnt;&percnt;&percnt;ShawnZhou大佬】

    {dalao传送门} 这道题如果要使用暴力搜索直接求解会严重超时.实际上,我们可以发现,这个所谓的最短跳跃距离显然不能超过一个范围,而这个范围题目上已经给了出来.也就是说,答案是有一个确定的范围限制的 ...

  8. 搭建 RabbitMQ Server 高可用集群【转】

    阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...

  9. 1333:【例2-2】Blah数集

    1333:[例2-2]Blah数集 注意是数组,答案数组中不能有重复数字 q数组是存储答案的 代码: #include<iostream> #include<cstdio> # ...

  10. The difference among ioctl&comma; unlocked&lowbar;ioctl and compat&lowbar;ioctl &lpar;RT&rpar;

    Meta-answer: All the raw stuff happening to the Linux kernel goes through lkml (the Linux kernel mai ...