iOS基础知识:Objective-C 之 网络请求
2015-09-08 18:27
761 查看
iOS基础知识—————基础不牢,地动山摇
Objective-C 之 网络请求
URL
1、全称:Uniform Resource Locator 统一资源定位符
2、URL对象建立:
3、URLRequest 对象建立:
4、URLConnection的异步请求
5、NSConnectionDataDelegate代理方法
HTML
1、全称:Hyper Text Markup Language 超文本语言
2、用Hpple解析网页步骤:
3、关于searchWithXPathQuery里面的谓词
URLSession
1、用Session请求网络资源,可类比URLConnection
2、Session download data
3、Session data delegate
代理方法如下,相比connection的delegate,将connection的error和finished合并于一起了:
4、AFNetWorking
Objective-C 之 网络请求
URL
1、全称:Uniform Resource Locator 统一资源定位符
2、URL对象建立:
//创建URL从网络服务器 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; //创建URL从本地文件 NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/desktop/text.txt"];
3、URLRequest 对象建立:
//默认的Request NSURLRequest *req = [NSURLRequest requestWithURL:url]; //设定缓存策略,及网络请求超时时间 NSURLRequest *req1 = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
4、URLConnection的异步请求
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError) { NSLog(@"error:%@",connectionError); }else{ NSLog(@"response:%@",response); NSLog(@"datalength:%lu",data.length); } }];
5、NSConnectionDataDelegate代理方法
NSURLConnection *connect = [NSURLConnection connectionWithRequest:req delegate:self]; //四个代理方法 //网络开始响应后,代理自动调用 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"123"); } //在接收数据过程中代理会不停调用,直到其结束 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"1111"); } //数据接收完成后调用 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"finished"); } //数据接收失败时调用,并将错误信息返回至error - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"error"); }
HTML
1、全称:Hyper Text Markup Language 超文本语言
2、用Hpple解析网页步骤:
//creat url and get the data NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSData *data = [NSData dataWithContentsOfURL:url]; //get the Hpple to doc TFHpple *doc = [TFHpple hppleWithHTMLData:data encoding:@"utf-8"]; //search the hppleElement by specified predicate NSArray *eleArray = [doc searchWithXPathQuery:@"//a[@href='http://www.hao123.com']"]; //get the information from the element array for (TFHppleElement *element in eleArray) { NSLog(@"content:%@",element.content); }
3、关于searchWithXPathQuery里面的谓词
//用得比较多的形式,寻找根结点以下,任何有class属性,并且其class属性为"abc"的div结点 @"//div[@class='abc']"
URLSession
1、用Session请求网络资源,可类比URLConnection
//config a defaultSession configuration NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; //creat a session NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; //creat a dataTask and start by [dataTask resume] NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"error:%@",error); }else{ NSLog(@"response:%@",response); NSLog(@"datalength:%lu",data.length); } }]; [dataTask resume];
2、Session download data
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; //return download location NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:req completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"error:%@",error); }else{ NSLog(@"response:%@",response); NSLog(@"location:%@",location); } }]; [downloadTask resume];
3、Session data delegate
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; //creat session with delegate NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:req]; [dataTask resume];
代理方法如下,相比connection的delegate,将connection的error和finished合并于一起了:
//task start - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{ NSLog(@"respons:%@",response); //start by responseAllow completionHandler(NSURLSessionResponseAllow); } //task data downloading - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{ NSLog(@"length:%lu",data.length); } //complete or error - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{ if (error) { NSLog(@"error:%@",error); }else{ NSLog(@"download Finished"); } }
4、AFNetWorking
//creat operation manager and set Response Serializer as HTTPResponseSerializer(default is JSON Serializer) AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //creat operation , request data in (id)data AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:req success:^(AFHTTPRequestOperation *operation, id data) { NSLog(@"response:%@",operation.response); NSLog(@"class:%@",[data class]); } failure:^(AFHTTPRequestOperation * operation, NSError * error) { if (error) { NSLog(@"error:%@",error); }else NSLog(@"finished"); }]; [operation start];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 非常不错的脚本入侵基础知识总结第1/2页
- AS3 Flex基础知识100条
- Ruby基础知识之方法、代码段
- Lua教程(二):基础知识、类型与值介绍
- javascript基础知识大集锦(一) 推荐收藏
- C++基础入门教程(一):基础知识大杂烩
- ECMAScript 基础知识
- PHP基础知识介绍
- javascript中的107个基础知识收集整理 推荐
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- javascript真的不难-回顾一下基础知识
- JavaScript 基础知识 被自己遗忘的