AFNetworking的使用

一 下載: 網址 -- github

二 環境:
  需要引入的庫 - CoreLocation.framework
  SystemConfiguration.framework
  MobileCoreServices.framework
  Security.framework

  需要在 ARC 的環境下 - 非 ARC 的工程中 - 請添加 -fobjc-arc 

三 結構:
1 : AFHTTPClient  --   提供了一個方便的網絡交互接口,包括默認頭,身份驗證,是否連接到網絡,批量處理操作,查詢字符串參數序列化,已經多種表單請求

2 : AFHTTPRequestOperation -- 
和它得子類可以基於http狀態和內容列下來區分是否成功請求了

3 : AFURLConnectionOperation -- 
和它的子類繼承NSOperation的,允許請求被取消,暫停/恢復和由NSOperationQueue進行管理。

4 : AFURLConnectionOperation -- 
可以讓你輕鬆得完成上傳和下載,處理驗證,監控上傳和下載進度,控制的緩存。

四 使用 :

用法場景 1 : 請求api數據

方法1 --

創建一個 AFHTTPClient 實例

AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
// 這裏一定要有一個 baseURL - 不然會拋出一個異常 -- 

創建一個 NSURLRequest 實例

NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
    NSURLRequest * request = [client requestWithMethod:@"POST"
                                              path:@"users/show"
                                        parameters:params];

    @param  method 網絡請求方式,比如 GET,POST,PUT,DELETE , 不能爲 nil -- 
    @param  path   和 baseURL 組合成爲一個 url  -- 通俗的說,就是我們調用的接口 -- 比如,我想調用http://api.jiepang.com/users/show,這個接口 -- 那麼 baseURL 爲 http://api.jiepang.com -- path 爲users/show 
    @param  parameters 網絡請求參數 -- http://api.jiepang.com/v1/users/show?id=123 -- id=123 就是這個參數


創建一個 AFHTTPRequestOperation 實例進行網絡鏈接

AFHTTPRequestOperation * operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"success obj == %@",responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"faild , error == %@ ", error);
    }];

    [operation start];

    這樣 - 一次簡單的調用就OK了 - 

    當然也可以,用封裝好的其它方法 -- 
    - (void)getPath:(NSString *)path
     parameters:(NSDictionary *)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)postPath:(NSString *)path
      parameters:(NSDictionary *)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)putPath:(NSString *)path
     parameters:(NSDictionary *)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)deletePath:(NSString *)path
        parameters:(NSDictionary *)parameters
           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

- (void)patchPath:(NSString *)path
parameters:(NSDictionary *)parameters
 success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
 failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;


省去了組裝 Resquest 的時間  --
比如 - 

方法二 --

    [client getPath:path
     parameters:params
        success:^(AFHTTPRequestOperation *operation, id responseObject) {

                   NSString * obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

                   NSLog(@"obj == %@",obj);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"faild -- ");
        }];

    這和上面的效果是一樣的 -- 

    我們看到,前面返回的數據全部爲 NSdata - 爲我們添加了麻煩 -- 而且我們大部分的 api 返回數據都爲 json -- 
    同樣 - 我們可以用 AFHTTPRequestOperation 的子類 AFJSONRequestOperation 來替代 -- 

    方法三 -- 

       NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};
    
    
AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];
    
    NSURLRequest * request = [client requestWithMethod:@"POST"
                                                  path:@"users/show"
                                            parameters:params];
    
    
    AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        
        NSLog(@"json == %@",JSON);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        
        NSLog(@"faild -- ");
    }];
    
    [operation start];



    使用場景 2 : 異步加載圖片 

    AFImageRequestOperation 是繼承自 AFHTTPRequestOperation -- 所以 - 方法大同小異 -- 

    NSString * url = @"http://c.hiphotos.baidu.com/album/w=2048/sign=1d7ca85bac345982c58ae29238cc30ad/f2deb48f8c5494ee7abe33362cf5e0fe99257e04.jpg";
    // 這是一個大美女
    
    創建 request
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
                                              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                          timeoutInterval:30];
    
    AFImageRequestOperation * operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
                                                                               imageProcessingBlock:^UIImage *(UIImage *image) {
                                                                                   
                                                                                   UIImage * tempImage = [UIImage imageWithCGImage:
                                                                                                         CGImageCreateWithImageInRect(image.CGImage,ake(0, 0, image.size.width, image.size.height/2.0))];
                                                                                   
                                                                                   return tempImage;
                                                                                   
                                                                               } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                                                   
                                                                                   NSLog(@"reload image success ");
                                                                                   
                                                                                   _imageView.image = image;
                                                                               } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                                                   
                                                                                   NSLog(@"reload image faild , error == %@ ",error);
                                                                                   
                                                                               }];
    
    [operation start];

    這個方法裏有三個block ,success 和 failure 不說 -- processimageBlock -- 是在 圖片加載完後,對 圖片 處理的 block ,可以爲 nil ,在 success 之前調用  --
發佈了3 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章