小谈AFNetworking的项目使用

一、需要引入的文件:

1.AFNetworking源码文件:


2.所需Library:


二、简单的使用流程:

//具体的后台服务地址
static NSString *const BaseURLString = @"www.xx.com/";

/*****************用法一*********************/

NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:BaseURLString]];

//用于存放需要传递给服务端的参数

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:1];
    
[parameters setObject:参数值 forKey:参数名];
    
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
    
NSString *path = @"具体的服务端处理接口";//例如restful的"xx/yy/zz"


/*发送请求。可以使用getPath来获取数据,也可以用postPath来更新服务器数据,
这里只列举了post的用法,get与post的写法类似。*/
[client postPath:path 
      parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
         
             //交互成功:responseObject包含了从服务器回传的信息

         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             //交互失败:error记录了具体的错误信息
             UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"获取数据失败"
                                                          message:error
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"          
                                                otherButtonTitles:nil];
             [av show];
                                
         }
];




/*****************用法二(operation的方式)*********************/



NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURLString,@"具体的服务端处理接口"]];
    
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:baseURL 
                                                            cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                        timeoutInterval:20.0f];

//传递的参数
NSString *postString = @"keywords=嘟囔";
    
NSStringEncoding enc = NSUTF8StringEncoding;
NSData *postData = [postString dataUsingEncoding: enc allowLossyConversion: YES];


//设置Method    
[request setHTTPMethod: @"POST"];


//装载内容
[request setHTTPBody:postData];
    
AFJSONRequestOperation *operation 
= [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
{
     
    //交互成功:JSON包含了从服务器回传的信息  
        
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
{
    
    //交互失败:error记录了具体的错误信息
    NSLog(@"ERROR: %@", error);
        
}];

    
[operation start];

这两种方式,都可以写在自己的网络数据交互类里面,然后也封装一个block方法,将从服务器端成功获取到的数据传递给具体的controller用于显示。


三、UIImageView+AFNetworking
这个东东很好用。可以异步的加载网络图片。
具体的用法也很简单:

#import "UIImageView+AFNetworking.h"
[需要异步加载图片的imageView setImageWithURL:[NSURL URLWithString:图片存放的可访问的网络地址] placeholderImage:[UIImage imageNamed:placeholder图片]];


有什么好的使用建议,大家可以共同讨论。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值