UI进阶之数据解析(网络请求NSURLSession)
GET POST异步block方法
POST: - (IBAction)post:(id)sender { NSURLSession *session = [NSURLSession sharedSession]; NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSString *dataString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; NSData *potaData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:potaData]; NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error == nil) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"dic == %@", dic); } }]; [task resume]; } GET: //使用block回调方式 //使用系统提供的全局NSURLSession对象 ,是一个单例 NSURLSession *session = [NSURLSession sharedSession]; //创建地址字符串(不要放空格) NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; //统一资源定位符 NSURL *url = [NSURL URLWithString:urlString]; //NSURLSession 是基于任务的,所以所有的东西都要放到任务里面, NSURLSessionTask就是NSURLSession的任务执行对象 NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error == nil) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"dic == %@", dic); } }]; //NSURLSession 的所有任务默认是挂起的,所以一定要调用resume方法,让任务开始 //[task resume];
GET POST异步代理方法
//NSURLSessionDataDelegate NSURLSession获取网络数据的代理协议 @interface ViewController ()<NSURLSessionDataDelegate> //保存结果的data @property (nonatomic, strong) NSMutableData *resultData; @end POST: - (IBAction)post:(id)sender { NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSString *dataString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; NSData *potaData = [dataString dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:potaData]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; [dataTask resume]; } GET: - (IBAction)get:(id)sender { //NSURLSession代理的异步操作 //NSURLSession 代理人属性是只读的 //第一个参数: 会话模式 //第二个参数: 代理人 //第三个参数: 代理方法在哪个线程中进行 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration ] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"]; //NSURLSessionTask 的子类对象 NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url]; [dataTask resume]; }
//代理实现的方法
//服务器开始响应
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveResponse:(nonnull NSURLResponse *)response completionHandler:(nonnull void (^)(NSURLSessionResponseDisposition))completionHandler
{
// NSURLSession 的代理协议里面 必须设置允许继续请求 才会继续的响应服务器 获取到数据
completionHandler(NSURLSessionResponseAllow);
self.resultData = [NSMutableData data];
}
//接收数据
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull 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 == %@", dic);
}
}