#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);
}
}]; }

最新文章

  1. Windows一些零碎
  2. C# 压缩文件与字节互转
  3. expdp远程导出数据
  4. LLBL Gen + Entity Framework 程序设计入门
  5. HTML DOM 事件
  6. 与你相遇好幸运,CentOS 7 x86_64使用Yum安装PostgreSQL
  7. Heap:Expedition(POJ 2431)
  8. 关于java中super()和this()
  9. POJ 3169 Layout (差分约束系统)
  10. Caused by: java.lang.NoClassDefFoundError: freemarker/cache/TemplateLoader
  11. php代码审计一些笔记
  12. Unity进阶----AssetBundle_02(加载依赖关系及网络资源)(2018/10/31)
  13. 运行scrapy crawl (文件名)时显示invalid syntax和no modle &#39;win32api&#39;解决方案
  14. gcc,make,cmake
  15. Redis安装以及主从实现
  16. 机器学习之路: python k近邻分类器 KNeighborsClassifier 鸢尾花分类预测
  17. Zookeeper配置文件参数与含义
  18. Leetcode 39
  19. Oracle VM Virtualbox基础知识
  20. C#学习笔记-接口与抽象类

热门文章

  1. 利用SVN工具下载OpenCore代码
  2. IOS雕虫小技
  3. 我所研究过的 ASP.NET MVC 或者 .NET 或者 ORM 或者框架的开源项目
  4. Could not load file or assembly &#39;System.Core, Version=2.0.5.0 和autofac冲突的问题
  5. php分享三十三:常量
  6. iOS-文字自适应
  7. 在eclipse中配置python插件
  8. Mysql学习笔记(五)数学与日期时间函数
  9. 网络编程:socket--python核心编程(3),chapter 1
  10. servlet中的细节