iOS新人学习AFNetworking(三)

源代码下载:http://download.csdn.net/detail/haogaoming123/8555077
FNetworking 是我们常用的轻量级网络请求,文章的前提是你已经对  AFNetworking 有了基本的掌握。
不太了解的,请链接  iOS 使用AFNetworking。

一 : 唠唠叨叨。

AFNetworking 是建立在 NSURLConnection 和 NSOperation 等类库的基础之上的,取消的网络的请求的操作也就变得很简单。
但  AFNetworking 又没有直接的给出我们取消的方式,这就需要我们添加。

二  :该说实现了 。

如上所言,我们要取消网络请求,无非有两种思想 --

1 - 拿到 NSOperation 进行取消 ;
2 - 通过 AFHTTPClient 集成方法取消;

我们通过继承  AFHTTPClient 并写成单例的网络请求 ,  AFHTTPClient 简单的就能拿到 - 
那 operation 该怎么处理呢 --

三 : 寻找 operaion 

我们可以通过外部直接用 operation 请求 -- 

AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101110101.html"]];
      
      NSURLRequest * request = [client requestWithMethod:@"POST"
                                                                           path:nil
                                                            parameters:nil];
      
AFHTTPRequestOperation * operation = [client HTTPRequestOperationWith Request:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"success obj == %@",responseObject);

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


这样就能拿到 operation 进行其他的操作 ---

但是,我们真正的开发中,并不真正这样操作,因为并没有添加到 队列中对 operation 进行管理 ,而且 这样的请求  AFNetworking 并不推荐 -- 

开发中,我们通过通过继承  AFHTTPClient , 做一些操作 --   并 使用  - (void)getPath:(NSString *)path
        parameters:(NSDictionary *)parameters
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

的封装 --- 

好 -- 我们看一下 这个方法的实现 ---- 

- (void)getPath:(NSString *)path
        parameters:(NSDictionary *)parameters
            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
      AFHTTPRequestOperation *operation = [self HTTPRequestOperationWith Request:request success:success failure:failure];
      [self enqueueHTTPRequestOperat ion:operation];
}

我们看到这个方法里是有 operation 的,我们为何不直接返回,拿到这个 operation 呢 ?

写个类似的方法 --- 

- (AFHTTPRequestOperation *)getServer:(NSString *)path
                                         parameters:(NSDictionary *)parameters
                                             success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                             failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
      AFHTTPRequestOperation *operation = [self HTTPRequestOperationWith Request:request success:success failure:failure];
      [self enqueueHTTPRequestOperat ion:operation];
      
      return operation;
}


这样,外部我们就能直接拿到这个 operation 了 --------

这样,外部调用,就成了   --- 

AFURLConnectionOperation  * operation = [[ZFHTTPClient shared] getFromServer:@"sales"
                                                   params:nil
                                                  success:^(AFHTTPRequestOperation *operation, id JSON) {
                                                       
                                                        NSLog(@"success");
                                                  } failure:^(NSInteger errorCode, NSString *errorMessage, NSError *error) {
                                                       
                                                        NSLog(@" error -- %@", [[ZFHTTPClient shared] operationQueue].operations);
                                                  }];



四 : 是时候取消一个网络请求了 --- 

这部分大家看注释哈 ,写的很清楚 -- 

    
    

      // 1 ------
      // operation 直接取消
      [operation cancel];
      
      // 2 ------
      // operation 暂停与重新启用
      [operation pause];
      [operation resume];
      
      // 3 ------
      // AFURLConnectionOperation  自带的 cancelConnection 方法,该方法在 AFURLConnectionOperation .m 中,需要用 perform 调用 --
      // 原理是通过 connection 的 cancle   实现 --
      // 注意的是 -- 由于 operation 加到 队列中时,我们并不知道 operation 何时调用。   cancel 的时候,operation 或许还没有 start ,所以可能会失败 -
      // 不推荐此方法 --
      
      // 比如 ,我在 add operation 后,直接 调用 cancelConnection 就是失败的 -- 因为此时 operation 还没有 start -- 所以要 延迟 1 秒执行 --
      [operation performSelector:@selector(cancelConnection) withObject:nil
                              afterDelay:1];
       
      
      // 4 ------
      // 通过 AFHTTPClient 取消所有的 operation --
      [[[ZFHTTPClient shared] operationQueue] cancelAllOperations];
      
      
      // 5 -------
      // 通过 ZFHTTPClient 取消 operation --
      // @param method 填请求的 method   -- 比如 GET , POST 等等
      // @param path 后面填要取消的 url , 注意 ,这里是详细的 url ,包括 baseurl 的 ---
      // 名字是取消所有的 operation,实际使用中只是取消单个链接 -
      [[ZFHTTPClient shared] cancelAllHTTPOperationsW ithMethod:@"GET"
                                                                                    path:[kBaseURL stringByAppendingString:@"sales"]];

你可能感兴趣的:(iOS新人学习AFNetworking(三))