AFNetworking 网络库

来源:互联网 发布:mac设置图标大小 编辑:程序博客网 时间:2024/06/05 10:15
1、为什么要用AFNetworking

2、AFNetworking的用法

一、为什么要用

AFNetworking 在ios
开发中,一般情况下,简单的向某个web站点简单的页面提交请求并获取服务器的响应,用xcode自带的NSURLConnection是可以解决大部分问题的。但是,在绝大部分下我们所需要访问的web页面则是属于那种受到权限保护的页面,并不是有一个简单的URL可以访问的。这就涉及到了Session和Cookie的处理了,在此时使用

NSURLConnection也是能够达到要求的,只是其中处理起来的复杂度和难度就提升了。为了更好的处理向Web站点的请求,包括处理Session,Cookie等细节问题,使用

AFNetworking则是更好的选择,他可以用于发送HTTP请求,接收HTTP的响应,但是不会缓存服务器的响应,不能执行HTML页面中的JAvascript代码,同时,AFNetworking

还内置支持JSON,plist文件和XML文件的解析,使用比较方便。

 扩展:

1、Session:中文有译作时域的,就是只某个客户端在访问服务器起到停止访问这一段的时间间隔被称为时域。
2、Cookie:由服务器发送给客服端,把Cookie的key:value值储存在本地文件夹下,当下次请求的时候能够直接发送Cookie获得权限验证


 

利用AFJSONRequestOperation官方网站上给的例子

    NSString *str=[NSString stringWithFormat:@"https://alpha-api.app.net/stream/0/posts/stream/global"];         NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];         NSURLRequest *request = [NSURLRequest requestWithURL:url];         //    从URL获取json数据         AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary* JSON) {                     NSLog(@"获取到的数据为:%@",JSON);         } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id data) {             NSLog(@"发生错误!%@",error);         }];         [operation1 start];  

如何通过URL获取图片

异步获取图片,通过队列实现,而且图片会有缓存,在下次请求相同的链接时,系统会自动调用缓存,而不从网上请求数据。

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 100.0f, 100.0f, 100.0f)];      [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];      [self.view addSubview:imageView];      上面的方法是官方提供的,还有一种方法,      NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"]];          AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse*response, UIImage *image) {              self.backgroundImageView.image = image;          } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {              NSLog(@"Error %@",error);          }];                 [operation start];  

如何通过URL获取plist文件

通过url获取plist文件的内容,用的很少,这个方法在官方提供的方法里面没有

    NSString *weatherUrl = @"http://www.calinks.com.cn/buick/kls/Buickhousekeeper.plist";        NSURL *url = [NSURL URLWithString:[weatherUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];        NSURLRequest *request = [NSURLRequest requestWithURL:url];        [AFPropertyListRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];        AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request,NSHTTPURLResponse *response, id propertyList) {            NSLog(@"%@",(NSDictionary *)propertyList);                    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, idpropertyList) {            NSLog(@"%@",error);        }];              [operation start];  




0 0
原创粉丝点击