WABHTTP
//远程资源地址
NSURL *url = [NSURL URLWithString:@"https://api.heweather.com/x3/weather?cityid=CN101010100&key=e1f85b8fd1ad40b2aa5b0e236f465180"];
//创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
//发送请求
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//json序列化解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
NSArray *arr = dict[@"HeWeather data service 3.0"];
for (id obj in arr) {
NSLog(@"%@",obj[@"suggestion"][@"comf"][@"brf"]);
}
//资源下载的url
NSString *fileStr = @"http://d.hiphotos.baidu.com/image/h%3D200/sign=6008b360f336afc3110c38658318eb85/a1ec08fa513d26973aa9f6fd51fbb2fb4316d81c.jpg";
NSURL *fileUrl = [NSURL URLWithString:fileStr];
NSURLRequest *downLoadRequest = [NSURLRequest requestWithURL:fileUrl];
//数据下载
NSURLSessionDownloadTask *downTask = [[NSURLSession sharedSession] downloadTaskWithRequest:downLoadRequest completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//下载到的文件存储的位置
NSLog(@"%@",location);
//response.suggestedFilename下载到文件的名字
NSString *filePath= [@"/Users/glx/Desktop" stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"%@",filePath);
//把下载的图片移动到桌面
[[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:nil];
}];
[downTask resume];//启动任务
基本
//远程资源的地址
NSURL *url = [NSURL URLWithString:@"https://api.heweather.com/x3/citylist?search=allchina&key=e1f85b8fd1ad40b2aa5b0e236f465180"];
//创建请求对象
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
//发送请求
// NSURLResponse *response = nil;
//创建一个数据传输任务
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//data服务器返回的数据、response响应、error错误
//解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
// NSLog(@"%@",dict);
_array = dict[@"city_info"];
[tableView reloadData];
}];
[task resume];//执行任务
//NSURLSession代理
#import "ViewController.h"
@interface ViewController ()<NSURLSessionDelegate,NSURLSessionDataDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建一个session对象 NSURLSessionConfiguration:session的配置类(defaultSessionConfiguration:默认 session 配置,使用硬盘来存储缓存数据)backgroundSessionConfiguration:后台session配置,与默认配置类似,不同的是会在后台开启另一个线程来处理网络数据
//delegate:代理对象
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.heweather.com/x3/citylist?search=allchina&key=e1f85b8fd1ad40b2aa5b0e236f465180"]];
//创建任务
NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
NSLog(@"%@",task);
[task resume];
}
//进入后台
- ( void )URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
}
//任务执行完成
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error{
NSLog(@" task did complete ");
}
//执行任务服务器返回的数据
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data{
NSLog(@"%@",data);
}
//执行完任务 接受到的响应
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{
NSLog(@"hello....");
completionHandler(NSURLSessionResponseAllow);
}
@end