AFNetworking是一个讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在NSURLConnection, NSOperation, 以及其他熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.
例如,他可以使用很轻松的方式从一个url来得到json数据:
NSURL*url=[NSURLURLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];
NSURLRequest*request=[NSURLRequestrequestWithURL:url];
AFJSONRequestOperation*operation=[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest*request,NSHTTPURLResponse*response,idJSON){
NSLog(@"Public Timeline: %@", JSON);
}failure:nil];
[operation start];
一、AFNetworking综述
1、CORE 核心
AFURLConnectionOperation:一个 NSOperation 实现了NSURLConnection 的代理方法.
2、HTTP Requests 请求
AFHTTPRequestOperation:AFURLConnectionOperation的子类,当request使用的协议为HTTP和HTTPS时,它压缩了用于决定request是否成功的状态码和内容类型.
AFJSONRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理jason response数据.
AFXMLRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理xml response数据.
AFPropertyListRequestOperation:AFHTTPRequestOperation的一个子类,用于下载和处理property list response数据.
3、HTTP CLIENT 公共交流模式
AFHTTPClient:捕获一个基于http协议的网络应用程序的公共交流模式.包含:
!使用基本的url相关路径来只做request
!为request自动添加设置http headers.
!使用http 基础证书或者OAuth来验证request
!为由client制作的requests管理一个NSOperationQueue
!从NSDictionary生成一个查询字符串或http bodies.
! 从request中构建多部件
! 自动的解析http response数据为相应的表现数据
!在网络可达性测试用监控和响应变化.
4、IMAGES 图片上传与下载
AFImageRequestOperation:一个AFHTTPRequestOperation的子类,用于下载和处理图片.
UIImageView+AFNetworking:添加一些方法到UIImageView中,为了从一个URL中异步加载远程图片
二、程序示例
1、XML Request XML请求
NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];
AFXMLRequestOperation*operation=[AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest*request,NSHTTPURLResponse*response,NSXMLParser*XMLParser){
XMLParser.delegate=self;
[XMLParser parse];
}failure:nil];
[operation start];
2、IMAGE Request 图片请求
UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURLURLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
3、API CLIENT Request API客户端请求
[[AFGowallaAPIClient sharedClient]getPath:@"/spots/9223"parameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){
NSLog(@"Name: %@",[responseObject valueForKeyPath:@"name"]);
NSLog(@"Address: %@",[responseObject valueForKeyPath:@"address.street_address"]);
}failure:nil];
4、FILE Upload With Progress CallBack 带进度回调的文件上传
NSURL*url=[NSURLURLWithString:@"http://api-base-url.com"];
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
NSData*imageData=UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"],0.5);
NSMutableURLRequest*request=[httpClient multipartFormRequestWithMethod:@"POST"path:@"/upload"parameters:nilconstructingBodyWithBlock:^(idformData){
[formData appendPartWithFileData:imageData name:@"avatar"fileName:@"avatar.jpg"mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation*operation=[[[AFHTTPRequestOperation alloc]initWithRequest:request]autorelease];
[operation setUploadProgressBlock:^(NSInteger bytesWritten,longlongtotalBytesWritten,longlongtotalBytesExpectedToWrite){
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
5、Streaming Request 数据流请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];
operation.outputStream = [NSOutputStream outputStreamToMemory];
[operation start];