IOS AFNetworking基本学习

来源:互联网 发布:软件利息的会计 编辑:程序博客网 时间:2024/05/27 20:15

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接口扩展。也比较好用



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











0 0
原创粉丝点击