标签:style http color os 文件 数据 io for
一、添加SystemConfiguration和MobileCoreServices 框架
二、下载AFNetworking资源包 https://github.com/AFNetworking/AFNetworking
三、在工程的Supporting File群组中打开预编译头文件XXX-Prefix.pch,然后在别的import后面添加如下一行代码#import “AFNetworking”
四、AFNetworking通过网络来加载和处理结构化的数据非常明智,它支持JSON,XML,Property List
- static NSString*const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";
-
- // 1
- NSString *weatherUrl = [NSStringstringWithFormat:@"%@weather.php?format=json",BaseURLString];
- NSURL *url = [NSURLURLWithString:weatherUrl];
- NSURLRequest *request = [NSURLRequestrequestWithURL:url];
-
- // 2
- AFJSONRequestOperation *operation =
- [AFJSONRequestOperationJSONRequestOperationWithRequest:request
- success:^(NSURLRequest*request, NSHTTPURLResponse *response, id JSON) {
- //
- NSDictionary*dicWeather = (NSDictionary *)JSON;
- NSLog(@"result:%@",dicWeather);
- }
- failure:^(NSURLRequest*request, NSHTTPURLResponse *response, NSError *error, id JSON) {
- UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:@"Error RetrievingWeather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles: nil];
- [alertView show];
- }];
- // 5
- [operation start];
五、AFNetWorking异步加载图片(不堵塞主线程)
- #import “UIImageView+AFNetworking.h”
- UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 80, 40, 40)];
- __weak UIImageView *_imageView = imageView;
- [imageViewsetImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURLURLWithString:@"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"]]
- placeholderImage:[UIImage imageNamed:@"placeholder.png"]
- success:^(NSURLRequest *request,NSHTTPURLResponse *response, UIImage *image) {
- _imageView.image = image;
-
- [_imageView setNeedsDisplay];
- }
- failure:^(NSURLRequest *request, NSHTTPURLResponse*response, NSError *error) {
- ;
- }];
- [self.view addSubview:imageView];
六、GET 和POST请求
- AFHTTPClient *client= [[AFHTTPClient alloc] initWithBaseURL:baseURL];
- [clientregisterHTTPOperationClass:[AFJSONRequestOperation class]];
- [clientsetDefaultHeader:@"Accept" value:@"application/json"];
- [client postPath:@"weather.php"
- parameters:parameters
- success:^(AFHTTPRequestOperation *operation, id responseObject) {
- self.weather =responseObject;
- self.title = @"HTTPPOST";
- [self.tableViewreloadData];
- }
- failure:^(AFHTTPRequestOperation *operation, NSError*error) {
- UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:nil
- cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
-
- }
- ];
-
- [client getPath:@"weather.php"
- parameters:parameters
- success:^(AFHTTPRequestOperation *operation, id responseObject) {
- self.weather =responseObject;
- self.title = @"HTTP GET";
- [self.tableViewreloadData];
- }
- failure:^(AFHTTPRequestOperation *operation, NSError*error) {
- UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
- message:[NSStringstringWithFormat:@"%@",error]
- delegate:nil
- cancelButtonTitle:@"OK" otherButtonTitles:nil];
- [av show];
-
- }
- ];
注:
1、AFJSONOperation,AFPropertyListOperation, AFXMLOperation用来解析结构化数据。
2、UIImageView+AFNetworking用来快捷的填充image view
3、AFHTTPClient用来进行更底层的请求
4、用自定义的AFHTTPClient子类来访问一个web service。
5、AFNetworkActivityIndicatiorManager用来给用户做出网络访问的提示。
6、AFImageRequestOperation用来加载图片
AFNetworking,布布扣,bubuko.com
AFNetworking
标签:style http color os 文件 数据 io for
原文地址:http://www.cnblogs.com/swallow37/p/3873211.html