【#网络数据解析】首先我们准备一些准备工作新建一个singleView工程,然后如图所示
文章图片
heiyu.png
文章图片
heiyu.png
如图所示,我们新建一个名字叫URL的headFile文件,方便以后调用接口
文章图片
关于网络数据解析iOS9.0之后已经弃用NSConnection不过还是能用,不过推荐使用NSSession.
由于代理比较麻烦,所以这里只整理block
NSURLConnection解析
// #pragma mark - post同步请求
//1.创建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.创建网络请求 [post请求必须初始化为可变请求,因为稍后要进行一些内容的设置]
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
//2.5设置body
//创建一个连接字符串(这个内容在以后的开发中接口文档都有标注)
NSString *dataString = POST_BODY;
//对连接字符串进行编码【这一步千万不能忘记】
NSData *postData = https://www.it610.com/article/[dataString `dataUsingEncoding:NSUTF8StringEncoding];
//设置请求格式为POST请求[在这个地方后边POST必须全大写]
[mutableRequest setHTTPMethod:@"POST"];
//设置请求体(body)
[mutableRequest setHTTPBody:postData];
//3.连接服务器
NSData *data = [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:nil error:nil];
//4.进行json数据解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
// #pragma mark - get同步请求
// 1.创建URL
NSURL *url = [NSURL URLWithString:GET_URL];
// 2.创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
// 3.连接服务器
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// 4.解析数据
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",resultDic);
// #pragma mark - post异步请求(block)
//1.创建url
NSURL *url = [NSURL URLWithString:POST_URL];
// 2.创建网络请求
NSMutableURLRequest *mRequest= [NSMutableURLRequest requestWithURL:url];
//2.5创建body
NSString *string = POST_BODY;
NSData *data = https://www.it610.com/article/[string dataUsingEncoding:NSUTF8StringEncoding];
[mRequest setHTTPMethod:@"POST"];
[mRequest setHTTPBody:data];
//3.链接服务器
[NSURLConnection sendAsynchronousRequest:mRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError == nil) {//4.数据解析
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dict);
}
}];
// #pragma mark - get异步请求(block)
//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 = %@",dic);
//先开辟子线程解析数据
//然后在主线程里刷新UI
}
}];
NSURLSession解析 // #pragma mark - get同步请求
//1.创建url
NSURL *url = [NSURL URLWithString:GET_URL];
//2.创建session对象
NSURLSession *session = [NSURLSession sharedSession];
//3.创建task请求任务
//NSURLSession是基于任务去完成相关的事件的
//NSURLSessionTask所有的任务均放在这个里边实现
NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//4.解析相关的数据
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
}
}];
//5.核心步:启动任务【千万不能忘记】
//原因:NSURLSessionTask实例出来的任务处于挂起状态,如果不启动,不会走block中的实现内容
[task resume];
// #pragma mark - post异步请求
//1.创建url
NSURL *url = [NSURL URLWithString:POST_URL];
//2.创建请求
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
//2.5核心设置body
NSString *bodyString = POST_BODY;
NSData *postData = https://www.it610.com/article/[bodyString dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequest setHTTPMethod:@"POST"];
[mutableRequest setHTTPBody:postData];
//3.创建session对象
NSURLSession *session = [NSURLSession sharedSession];
//4.创建task
NSURLSessionTask *task = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {//5.解析
if (error == nil) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dic = %@",dic);
}}];
//6.启动任务
[task resume];