OC 30 day
因为工作的需求,今天跳级来写写网路请求。
我们利用这个类,帮我们发送请求。
他总共有两个类方法:
NSURLRequest *request = [NSURLRequest requestWithURL:<#(nonnull NSURL *)#>]
於是,我们创建一个网路请求,网路请求里面也有一个参数,这个参数是什麽呢? URL
於是做了一个URL
NSURL *url = [NSURL URLWithString:@"https://tw.yahoo.com"];
把URL带入网路请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
第二个参数使用主队列。
[NSOperationQueue mainQueue]
第三个参数cpmpletionHandler里面有三个参数
//reponse
//data
//connectionError
完整的网路请求编码如下:
//发送请求
NSURL *url = [NSURL URLWithString:@"https://tw.yahoo.com"];
//请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//发送异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//reponse
//data
//connectionError
if(!connectionError){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
}else{
NSLog(@"连接错误 %@",connectionError);
}
}];
很顺利的拿到一大串资料了。
>>: What is the reason behind Pacbell email login issue?
今天是我到职的 2 个月又 3 天,我就先说说我这两个月都在做些什麽事情吧。 终於上周我被交付了一...
孵了很久,终於孵出一篇新文章! 这篇是我思考了很久才决定发表的,因为目前疫情刚趋缓,所以许多企业赶着...
OS模组(Python内建) 说明 : os模组是一种与作业系统相关的模组,提供数十种与作业系统沟通...
前言: 有时候我们会需要看我们给Server的request跟回传的response时, 如果需要一...
Day 22 - Django + YOLO 後台整合应用 今天的任务是将 Day 07 - RES...