网编基础(1)

网编代码笔记:


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    //01 创建URL地址

    NSURL *url = [NSURLURLWithString:@"http://api.douban.com/v2/movie/top250"];

//    NSURL *url = [NSURL URLWithString:@"http://api.douban.com/v2/movie/top250?count=1"];

    

    //02 创建请求request(设置请求头,请求体,系统默认设置了GET请求,如果想要自己去设置,使用子类就可以)

//    NSURLRequest *request = [NSURLRequest requestWithURL:url];这种也可以!

    NSURLRequest *request = [[NSURLRequestalloc] initWithURL:url];

    

    //03 创建会话对象

//    NSURLSession *session = [[NSURLSession alloc] init];//不能这样创建!

    NSURLSession *session = [NSURLSessionsharedSession];

    

    //04 创建会话的数据任务

    NSURLSessionDataTask *data = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData *_Nullable data,NSURLResponse * _Nullable response,NSError * _Nullable error) {

        

        /*

        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

        NSLog(@"%@", httpResponse);

        //获取状态码

        NSInteger statusCode = httpResponse.statusCode;

        NSLog(@"打印状态码:%li", statusCode);//200代表成功,404代表找不到网页

        //获取响应头信息

        NSDictionary *dic = httpResponse.allHeaderFields;

        //添加对象

        [dic objectForKey:@"Content-Type"];

        NSLog(@"这个美丽的字典:%@", dic);

         */

        

        //打印一下当前任务执行所在的线程

//        NSLog(@"当前所在的线程:%@", [NSThread currentThread]);不在主线程,开启了新的线程执行

        

        //解析数据

        id result = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:NULL];

        NSLog(@"打印解析的数据结果:%@", result);

        

        

    }];

    

    //05 发起网络请求

    [data resume];

    

    

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值