AFNetworking介绍
来源:互联网 发布:阿里云改ssh端口 编辑:程序博客网 时间:2024/05/20 03:43
AFNetworking是一个讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在NSURLConnection, NSOperation, 以及其他熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.例如,他可以使用很轻松的方式从一个url来得到json数据:
1NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];2NSURLRequest *request = [NSURLRequest requestWithURL:url];3AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {4NSLog(@"Public Timeline: %@", JSON);5} failure:nil];6[operation start];如何开始使用
- 下载 AFNetworking 然后尝试使用里面的例子
- 阅读 “Getting Started” guide, FAQ, 或者 other articles in the wiki
- 查看 完整的文档
- 查看 NSScreencast episode about AFNetworking 来得到一个快速的参考,以快速的应用到你的app中
- 有问题? Stack Overflow 是最好的地方来找到答案
综述
CORE:
AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.
HTTP Requests:
AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.
AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jason response数据.
AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xml response数据.
AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property list response数据.
HTTP CLIENT:
AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.包含:
- 使用基本的url相关路径来只做request
- 为request自动添加设置http headers.
- 使用http 基础证书或者OAuth来验证request
- 为由client制作的requests管理一个NSOperationQueue
- 从NSDictionary生成一个查询字符串或http bodies.
- 从request中构建多部件
- 自动的解析http response数据为相应的表现数据
- 在网络可达性测试用监控和响应变化.
IMAGES
AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.
UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片
例子程序
XML REQUEST
1NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];2AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {3XMLParser.delegate = self;
4[XMLParser parse];5} failure:nil];6[operation start];IMAGE REQUEST
1UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];2[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];API CLIENT REQUEST
1// AFGowallaAPIClient is a subclass of AFHTTPClient, which defines the base URL and default HTTP headers for NSURLRequests it creates
2[[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {3NSLog(@"Name: %@", [responseObject valueForKeyPath:@"name"]);4NSLog(@"Address: %@", [responseObject valueForKeyPath:@"address.street_address"]);5} failure:nil];FILE UPLOAD WITH PROGRESS CALLBACK
01NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];02AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];03NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);04NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {05[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];06}];0708AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];09[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {10NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);11}];12[operation start];STREAMING REQUEST
1NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];23AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];4operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];5operation.outputStream = [NSOutputStream outputStreamToMemory];6[operation start];0 0
- AFNetworking介绍
- AFNetworking介绍
- AFNetworking 2.0 介绍
- 深入介绍AFNetworking原理
- <iOS>AFNetworking使用介绍
- Afnetworking的基本使用介绍
- AFNetworking的介绍与理解
- AFNetworking的介绍与使用
- AFNetworking的介绍与使用
- 【技术】【使用】AFNetworking的介绍和应用
- AFNetworking详细解析之概况介绍
- AFNetworking
- AFNetworking
- AFNetworking
- AFNetworking
- AFNetworking
- AFNetworking
- AFNetworking
- objective c之中的类和实例,方法等基本语法(一)
- 利用Linux命令按行去重以及按重复度排序
- FZOJ 1157 Crazy Tea Party
- CakePHP高级安装
- java从头学day01[准备工作]
- AFNetworking介绍
- 《HTML5与CSS3基础教程(第8版)》
- UITableViewCell
- Windows下与Linux下编写socket程序的区别
- 错题集2
- dd与cp的区别
- 进程是线程吗?
- 单据管理窗口,上下两个datagridview查询时没有符合条件的记录时,绑定数据源的方法
- Kernel 编译配置机制
原创粉丝点击热门IT博客热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红酒专卖店 进口红酒礼盒 shiraz2012红酒价格 吉卡斯红酒 红酒礼盒装 esporao红酒 duvernay红酒 romisa红酒价格 indomita红酒 好喝的红酒 红酒批发城 daos红酒 medinet红酒 amplus红酒 结婚买什么红酒 哪里的红酒好喝 villerose红酒价格 delisen红酒 patritti红酒 礼盒装红酒 maugena红酒 opusone红酒 ampleur红酒 arinena红酒 波亚克红酒 红酒小瓶装 进口红酒与国产红酒 velli 红酒 红酒都有什么 爱莎达红酒 波士多红酒 马斯林红酒 morkel红酒 红酒礼盒设计 红酒木瓜哪有买 没有红酒器怎么开红酒 真正的红酒 便宜好喝的红酒 好喝便宜的红酒 红酒和干红 红酒和白酒