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



項目中應該能經常用的到。











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章