AFNetworking

一、添加SystemConfiguration和MobileCoreServices 框架

二、下载AFNetworking资源包 https://github.com/AFNetworking/AFNetworking

三、在工程的Supporting File群组中打开预编译头文件XXX-Prefix.pch,然后在别的import后面添加如下一行代码#import “AFNetworking”

四、AFNetworking通过网络来加载和处理结构化的数据非常明智,它支持JSON,XML,Property List

  1. static NSString*const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";  
  2.      
  3.     // 1  
  4.     NSString *weatherUrl = [NSStringstringWithFormat:@"%@weather.php?format=json",BaseURLString];  
  5.     NSURL *url = [NSURLURLWithString:weatherUrl];  
  6.     NSURLRequest *request = [NSURLRequestrequestWithURL:url];  
  7.      
  8.     // 2  
  9.     AFJSONRequestOperation *operation =  
  10.     [AFJSONRequestOperationJSONRequestOperationWithRequest:request  
  11.                                               success:^(NSURLRequest*request, NSHTTPURLResponse *response, id JSON) {  
  12.                                                  //  
  13.                                                  NSDictionary*dicWeather = (NSDictionary *)JSON;  
  14.                                                  NSLog(@"result:%@",dicWeather);  
  15.                                               }  
  16.                                               failure:^(NSURLRequest*request, NSHTTPURLResponse *response, NSError *error, id JSON) {  
  17.                                                  UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:@"Error RetrievingWeather"  
  18.                                                                                                message:[NSStringstringWithFormat:@"%@",error]  
  19.                                                                                               delegate:self  
  20.                                                                                       cancelButtonTitle:@"OK"  
  21.                                                                                       otherButtonTitles: nil];  
  22.                                                  [alertView show];  
  23.                                               }];  
  24.     // 5  
  25.     [operation start];  

五、AFNetWorking异步加载图片(不堵塞主线程)

  1. #import “UIImageView+AFNetworking.h”  
  2. UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 80, 40, 40)];  
  3.     __weak UIImageView *_imageView = imageView;  
  4.     [imageViewsetImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURLURLWithString:@"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"]]  
  5.                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]  
  6.                            success:^(NSURLRequest *request,NSHTTPURLResponse *response, UIImage *image) {  
  7.                               _imageView.image = image;  
  8.                                
  9.                               [_imageView setNeedsDisplay];  
  10.                            }  
  11.                            failure:^(NSURLRequest *request, NSHTTPURLResponse*response, NSError *error) {  
  12.                               ;  
  13.                            }];  
  14.     [self.view addSubview:imageView];  

六、GET 和POST请求

  1. AFHTTPClient *client= [[AFHTTPClient alloc] initWithBaseURL:baseURL];  
  2. [clientregisterHTTPOperationClass:[AFJSONRequestOperation class]];  
  3. [clientsetDefaultHeader:@"Accept" value:@"application/json"];  
  4. [client postPath:@"weather.php"  
  5.               parameters:parameters  
  6.                 success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  7.                      self.weather =responseObject;  
  8.                      self.title = @"HTTPPOST";  
  9.                      [self.tableViewreloadData];  
  10.                  }  
  11.                 failure:^(AFHTTPRequestOperation *operation, NSError*error) {  
  12.                      UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"  
  13.                                                                  message:[NSStringstringWithFormat:@"%@",error]  
  14.                                                                 delegate:nil  
  15.                                                        cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  16.                      [av show];  
  17.    
  18.                  }  
  19.          ];  
  20.    
  21. [client getPath:@"weather.php"  
  22.              parameters:parameters  
  23.                success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  24.                     self.weather =responseObject;  
  25.                     self.title = @"HTTP GET";  
  26.                     [self.tableViewreloadData];  
  27.                 }  
  28.                failure:^(AFHTTPRequestOperation *operation, NSError*error) {  
  29.                     UIAlertView *av =[[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"  
  30.                                                                  message:[NSStringstringWithFormat:@"%@",error]  
  31.                                                                delegate:nil  
  32.                                                       cancelButtonTitle:@"OK" otherButtonTitles:nil];  
  33.                     [av show];  
  34.    
  35.                 }  
  36.          ];  

注:

1、AFJSONOperation,AFPropertyListOperation, AFXMLOperation用来解析结构化数据。

2、UIImageView+AFNetworking用来快捷的填充image view

3、AFHTTPClient用来进行更底层的请求

4、用自定义的AFHTTPClient子类来访问一个web service。

5、AFNetworkActivityIndicatiorManager用来给用户做出网络访问的提示。

6、AFImageRequestOperation用来加载图片

AFNetworking,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。