AFNetwork特性
来源:互联网 发布:mac忘记密码怎么办 编辑:程序博客网 时间:2024/06/05 16:14
AFNetwork是一个轻量级的网络请求api类库。是以NSURLConnection,NSOperation和其他方法为基础的。
下面这个例子是用来处理json请求的:
NSURL *url = [NSURLURLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWithRequest:request success:^(NSURLRequest*request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net GlobalStream: %@", JSON);
} failure:nil];
[operation start];
使用方法:
1.下载AFNetwork,点击下载
2.将文件夹名称为AFNetworking拖入到你的工程项目即可
常见问题:
1.AFNetworking作用都有哪些?
NSURLConnection提供了+sendAsynchronousRequest:queue:completionHandler:和+sendAsynchronousRequest:queue:completionHandler:,但是AFNetworking提供了更好的功能
*AFURLConnectionOperation和它的子类继承NSOperation的,允许请求被取消,暂停/恢复和由NSOperationQueue进行管理。
*AFURLConnectionOperation也可以让你轻松得完成上传和下载,处理验证,监控上传和下载进度,控制的缓存。
*AFHTTPRequestOperation和它得子类可以基于http状态和内容列下来区分是否成功请求了
*AFNetworking可以将远程媒体数据类型(NSData)转化为可用的格式,比如如JSON,XML,图像和plist。
*AFHTTPClient提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求
*的UIImageView+ AFNetworking增加了一个方便的方法来异步加载图像。
2. AFNetworking是否支持缓存?
可以,NSURLCache及其子类提供了很多高级接口用于处理缓存
如果你想将缓存存储再磁盘,推荐使用SDURLCache
3.如何使用AFNetworking上传一个文件?
NSData *imageData =UIImagePNGRepresentation(image);
NSURLRequest *request = [clientmultipartFormRequestWithMethod:@"POST" path:@"/upload"parameters:nil constructingBodyWithBlock: ^(id formData){
[formDataappendPartWithFileData:imageData mimeType:@"image/png"name:@"avatar"];
}];
4.如何使用AFNetworking下载一个文件?
先创建一个AFURLConnectionOperation对象,然后再使用它的属性 outputStream进行处理
operation.outputStream = [NSOutputStreamoutputStreamToFileAtPath:@"download.zip"append:NO];
5.如何解决:SystemConfiguration framework not found inproject
请导入:
#import
#import
6.当应用程序退出时,如何保持持续的请求?
AFURLConnectionOperation有一个叫 setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于处理在应用程序进入后台后,进行持续的请求
[selfsetShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
}];
一些实例:
1.XML 请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperationXMLParserRequestOperationWithRequest:request success:^(NSURLRequest*request, NSHTTPURLResponse *response, NSXMLParser *XMLParser){
XMLParser.delegate =self;
[XMLParserparse];
} failure:nil];
[operation start];
2.图片请求:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f,100.0f)];
[imageView setImageWithURL:[NSURLURLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder-avatar"]];
3.图片上传处理,监测上传状态:
didiwei 17:28:57
NSURL *url = [NSURLURLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImageimageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClientmultipartFormRequestWithMethod:@"POST" path:@"/upload"parameters:nil constructingBodyWithBlock: ^(id formData){
[formDataappendPartWithFileData:imageData name:@"avatar"fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, longlong totalBytesWritten, long long totalBytesExpectedToWrite){
NSLog(@"Sent %lld of %lldbytes", totalBytesWritten,totalBytesExpectedToWrite);
}];
[operation start];
4.在线流媒体请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"http://localhost:8080/encode"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.inputStream = [NSInputStreaminputStreamWithFileAtPath:[[NSBundle mainBundle]pathForResource:@"large-image" ofType:@"tiff"]];
operation.outputStream = [NSOutputStreamoutputStreamToMemory];
[operation start];
下面这个例子是用来处理json请求的:
NSURL *url = [NSURLURLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperationJSONRequestOperationWith
} failure:nil];
[operation start];
使用方法:
1.下载AFNetwork,点击下载
2.将文件夹名称为AFNetworking拖入到你的工程项目即可
常见问题:
1.
NSURLConnection提供了+sendAsynchronousRequest:queue:completionHandler:和+sendAsynchronousRequest:queue:completionHandler:,但是AFNetworking提供了更好的功能
*AFURLConnectionOperation
*AFURLConnectionOperation
*AFHTTPRequestOperation和它得子类可以基于http状态和内容列下来区分是否成功请求了
*AFNetworking可以将远程媒体数据类型(NSData)转化为可用的格式,比如如JSON,XML,图像和plist。
*AFHTTPClient提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求
*的UIImageView+ AFNetworking增加了一个方便的方法来异步加载图像。
2. AFNetworking是否支持缓存?
可以,NSURLCache及其子类提供了很多高级接口用于处理缓存
如果你想将缓存存储再磁盘,推荐使用SDURLCache
3.如何使用AFNetworking上传一个文件?
NSData *imageData =UIImagePNGRepresentation
NSURLRequest *request = [clientmultipartFormRequestWith
}];
4.如何使用AFNetworking下载一个文件?
先创建一个AFURLConnectionOperation
operation.outputStream = [NSOutputStreamoutputStreamToFileAtPath
5.如何解决:SystemConfiguration framework not found inproject
请导入:
#import
#import
6.当应用程序退出时,如何保持持续的请求?
AFURLConnectionOperation
[selfsetShouldExecuteAsBackgr
}];
一些实例:
1.XML 请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb007
AFXMLRequestOperation *operation = [AFXMLRequestOperationXMLParserRequestOperatio
} failure:nil];
[operation start];
2.图片请求:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f,100.0f)];
[imageView setImageWithURL:[NSURLURLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder-avatar"]];
3.图片上传处理,监测上传状态:
didiwei
NSURL *url = [NSURLURLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentatio
NSMutableURLRequest *request = [httpClientmultipartFormRequestWith
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, longlong totalBytesWritten, long long totalBytesExpectedToWrit
}];
[operation start];
4.在线流媒体请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"http://localhost:8080/encode"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.inputStream = [NSInputStreaminputStreamWithFileAtPat
operation.outputStream = [NSOutputStreamoutputStreamToMemory];
[operation start];
0 0
- AFNetwork特性
- AFNETWORK
- AFNetwork
- AFNetwork
- afnetwork
- AFNetwork分析
- iOS AFNetWork
- AFNetwork 详解
- AFNetwork使用
- AFNetwork详解
- AFNetwork cookie
- AFNetwork问题
- 请求AFNetWork
- AFNetwork 2.0
- AFNetwork-ActivityIndicatorManager
- afnetwork 库引用
- AFNETWORK post请求
- IOS AFNetWork 附件上传
- JsonHelper 帮助类
- ios--对话框异常--wait_fences: failed to receive reply: 10004003
- oracle安装之后,plsql如何连数据库
- 程序员学习英语
- js dorado 返回path
- AFNetwork特性
- 星期三的一个上午
- C语言程序设计
- 嵌入式小白之工作1
- ios开发应用程序,如何限制只能竖屏展示,旋转横屏后界面无变化
- Resources文件夹
- 默认构造函数,隐式类型转换,复制构造函数
- sscanf,sscanf_s及其相关用法
- 傻瓜式Unity 3d导出发布安卓版本的详细步骤