IOS AFNetworking基本学习

AFNetworking 是较为出名的一个网络http库,很多项目中使用。在github上下载了最新的代码框架。

https://github.com/AFNetworking/AFNetworking/ 网上不少例子是老版本的,不过用法差不多。


我的开发环境:Xcode6 

贴图代码框架



编写了一个class 作为独立请求http

UIViewController中2个方法关联到2个按钮 这个比较简单就不贴ui了


#import "ViewController.h"



@interface ViewController ()

@end

@implementation ViewController

- (IBAction)baseHTTPGetData:(id)sender {
    HTTPDataFun *http = [[HTTPDataFun alloc] init];
    [http baseHTTPGetData:@"http://www.baidu.com"];
}

- (IBAction)afHTTPGetData:(id)sender {
    HTTPDataFun *http = [[HTTPDataFun alloc] init];
    [http afHTTPGetData:@"http://www.csdn.com"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


新建一个Cocoa对象 ,HTTPDataFun


#import "HTTPDataFun.h"
#import "AFNetworking.h"

@implementation HTTPDataFun

-(void)baseHTTPGetData:(NSString *)url { //这个方法是ios里面基本的http请求,异步方式。
    
    NSURL *httpurl = [[NSURL alloc] initWithString:url];
    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:httpurl];
    [request setTimeoutInterval:20];
    [request setHTTPMethod:@"GET"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    //异步方式
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *error){
         if (error) {
             NSLog(@"find a http request error %@",error);
         } else {
             NSInteger code = [(NSHTTPURLResponse *)response statusCode];
             if (code == 200) {
                NSString *alldata = [[NSString alloc]initWithData:data
                                                         encoding:NSUTF8StringEncoding];
                 NSLog(@"all data is %@",alldata);
             }
         }
     }];
    
}

-(void)afHTTPGetData:(NSString *)url { //这个方法用AFNetworking 框架
    NSLog(@"afnetworking request.");
    NSURL *httpurl = [[NSURL alloc] initWithString:url];
    NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:httpurl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
    AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq];
    [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){
        NSHTTPURLResponse *response = [op response];
        NSInteger code = [response statusCode];
        if (code == 200) {
            NSString *alldata = [[NSString alloc]initWithData:[op responseData] encoding:NSUTF8StringEncoding];
            NSLog(@"data is %@",alldata);
            NSString *str =  [op responseString];
            NSLog(@"data is %@",str);
        }
    
    } failure:^(AFHTTPRequestOperation *op, NSError *error) {
        if (error) {
            NSLog(@"req error %@",error);
        }
    
    }];
    [reqOper start];
    
    
    
    
}

@end


代码非常简单,贴出来算是个备份。AFNetworking 还包括和ui 的j接口扩展。也比较好用



项目中应该能经常用的到。











评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值