AFNetworking使用



  1. 1 将AFNetWorking文件夹导入项目  
     

    3 在使用的地方  #import "AFNetworking.h"  

  1. static NSString*const BaseURLString = @"http://www.raywenderlich.com/downloads/weather_sample/";    

  1.     // 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];   
    (1)根据基本的URL构造除完整的一个URL,然后通过这个完整的URL获得一个NSURL对象,然后根据这个url获得一个NSURLRequest。 
    (2)AFJSONRequestOperation是一个完整的类,整合了从网络中获取数据并对JSON进行解析。 
    (3)当请求成功,则运行成功块。在本例中,把解析出来的天气数据从JSON变量转换为一个字典(dictionary),并将其存储在字典中。 
    (4)如果运行出问题了,则运行失败块(failure block),比如网络不可用。如果failure block被调用了,将会通过提示框显示错误信息。 
     
    6.AFNetWorking异步加载图片 
    [plain]  view plain copy 
     
    [list=1](1)#import “UIImageView+AFNetworking.h”  (2)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];   
     7.GET 和POST请求 
    (1).构建一个baseURL,以及一个参数字典,并将这两个变量传给AFHTTPClient. 
    (2).将AFJSONRequestOperation注册为HTTP的操作, 这样就可以跟之前的示例一样,可以获得解析好的JSON数据。 
    (3).做了一个GET请求,这个请求有一对block:success和failure。 
    (4).POST请求跟GET一样 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值