博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络初体验
阅读量:7211 次
发布时间:2019-06-29

本文共 3827 字,大约阅读时间需要 12 分钟。

hot3.png

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

转载于:https://my.oschina.net/6104zhang/blog/743453

你可能感兴趣的文章
变量命名神器Codelf
查看>>
iBase4J部署总结
查看>>
IASetIndexBuffer Offset
查看>>
TeamLab安装及使用
查看>>
很安逸的离线API文档查询工具Dash和Zeal
查看>>
ICAP: 互换客户端地址协议
查看>>
Nginx-rtmp 直播媒体实时流实现
查看>>
C++ const 理解
查看>>
Linux进程管理 (7)实时调度
查看>>
基于鲁棒图进行概念架构设计
查看>>
Permission denied: exec of '/var/www/html/bugzilla/index.cgi' failed
查看>>
LESS CSS 框架简介与使用
查看>>
2014.09线上课堂报名帖:敏捷个人手机应用使用
查看>>
C# 重启exe
查看>>
Web 服务器 之 简易WWW服务器的架设
查看>>
一种电子病历系统软件框架思想
查看>>
轻松破解NewzCrawler时间限制
查看>>
gDebugger 3.1.1 原版+破解
查看>>
C++ 对象的内存布局(上)
查看>>
在Outlook中用VBA导出HTML格式邮件
查看>>