弃用的异步get和post方法之Block方法
2024-08-25 05:05:30
#import "ViewController.h"
#import "Header.h" @interface ViewController () <NSURLConnectionDataDelegate> @property (nonatomic, strong) NSMutableArray *dataArray; @end @implementation ViewController // 懒加载
- (NSMutableArray *)dataArray { if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} #pragma mark - get异步请求
- (IBAction)getAsynchronousRequset:(UIButton *)sender { // 1.创建url
NSURL *url = [NSURL URLWithString:GET_URL]; // 2.创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3.链接服务器
// 方法一:Block方法实现
// 第一个参数:请求对象
// 第二个参数:线程队列
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// response是携带的接口信息
// data请求下来的数据,接下来会会使用到的
// connectionError错误信息
if (connectionError == nil) { // 4.解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@", dic); // 先开辟子线程解析数据,然后在主线程里刷新UI
} }]; } #pragma mark - post异步请求
- (IBAction)postAsynchronousRequset:(UIButton *)sender { // 1.创建url
NSURL *url = [NSURL URLWithString:POST_URL]; // 2.创建请求
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url]; // 2.5.设置body
// 创建一个连接字符串(这个内容在以后的开发中接口文档都有标注)
NSString *dataStr = POST_BODY; // 对连接字符串进行编码【这一步千万不能忘记】
NSData *postData = [dataStr dataUsingEncoding:NSUTF8StringEncoding]; // 设置请求格式为post请求【在这里POST必须大写】
[mutableRequest setHTTPMethod:@"POST"]; // 设置请求体(body)
[mutableRequest setHTTPBody:postData]; // 3.链接服务器
// 方法一:Block方法
[NSURLConnection sendAsynchronousRequest:mutableRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (connectionError == nil) {
// 4.解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dic);
}
}]; }
最新文章
- Windows一些零碎
- C# 压缩文件与字节互转
- expdp远程导出数据
- LLBL Gen + Entity Framework 程序设计入门
- HTML DOM 事件
- 与你相遇好幸运,CentOS 7 x86_64使用Yum安装PostgreSQL
- Heap:Expedition(POJ 2431)
- 关于java中super()和this()
- POJ 3169 Layout (差分约束系统)
- Caused by: java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader
- php代码审计一些笔记
- Unity进阶----AssetBundle_02(加载依赖关系及网络资源)(2018/10/31)
- 运行scrapy crawl (文件名)时显示invalid syntax和no modle &#39;win32api&#39;解决方案
- gcc,make,cmake
- Redis安装以及主从实现
- 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测
- Zookeeper配置文件参数与含义
- Leetcode 39
- Oracle VM Virtualbox基础知识
- C#学习笔记-接口与抽象类
热门文章
- 利用SVN工具下载OpenCore代码
- IOS雕虫小技
- 我所研究过的 ASP.NET MVC 或者 .NET 或者 ORM 或者框架的开源项目
- Could not load file or assembly &#39;System.Core, Version=2.0.5.0 和autofac冲突的问题
- php分享三十三:常量
- iOS-文字自适应
- 在eclipse中配置python插件
- Mysql学习笔记(五)数学与日期时间函数
- 网络编程:socket--python核心编程(3),chapter 1
- servlet中的细节