zoukankan      html  css  js  c++  java
  • AFNetWorking能做什么

    AFNetwork是一个轻量级的网络请求api类库。是以NSURLConnection, NSOperation和其它方法为基础的。
    
    
    以下这个样例是用来处理json请求的: 
    NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
        NSLog(@"App.net Global Stream: %@", 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 = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData mimeType:@"image/png" name:@"avatar"]; }]; 4.怎样使用AFNetworking下载一个文件? 先创建一个 AFURLConnectionOperation对象,然后再使用它的属性 outputStream进行处理 operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO]; 5.怎样解决:SystemConfiguration framework not found in project 请导入: #import <SystemConfiguration/SystemConfiguration.h> #import <MobileCoreServices/MobileCoreServices.h> 6.当应用程序退出时。怎样保持持续的请求? AFURLConnectionOperation有一个叫 setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于处理在应用程序进入后台后,进行持续的请求 [self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ }]; 一些实例: 1.XML 请求 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"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.图片请求: UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; 3.图片上传处理,监測上传状态: didiwei 17:28:57 NSURL *url = [NSURL URLWithString:@"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:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation start]; 4.在线流媒体请求 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]]; operation.outputStream = [NSOutputStream outputStreamToMemory]; [operation start];


  • 相关阅读:
    Java 给Word指定字符串添加批注
    Java 打印Word文档
    Java 在PDF中添加页面跳转按钮
    C# 添加、修改、删除Excel图表数据标签
    C# 添加、读取、删除Excel文档属性
    Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
    【Luogu5348】密码解锁(莫比乌斯反演,数论)
    AtCoder Grand Contest 015
    AtCoder Grand Contest 014
    Codeforces Round #556 (Div. 1)
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7365715.html
Copyright © 2011-2022 走看看