AFNetworking是一個輕量級的iOS網路通訊類庫,繼ASI類庫不在更新之後開發人員們有一套不錯選擇;
AFNetworking類庫源碼下載和使用教程: https://github.com/AFNetworking/AFNetworking
如果想深入研究有官方文檔介紹:http://afnetworking.github.com/AFNetworking/
在開源中國iOS用戶端中關於AFNetworking類庫的使用只用到了兩個執行個體方法
(1)getPath:parameters:success:failure:
(2)postPath:parameters:success:failure:
他們用法基本相同,只是請求資料方式不同,一種是Get請求和Post請求。Get是向伺服器發索取資料的一種請求,也就相當於查詢資訊功能,不會修改類容,Post是向伺服器提交資料的一種請求,影響資料內容;兩種方法定義:
- (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 HTTPRequestOperationWithRequest:request success:success failure:failure]; [self enqueueHTTPRequestOperation:operation]; }
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; [self enqueueHTTPRequestOperation:operation]; }
getPath:parameters:success:failure:方法在程式中使用舉例:
NewsView.m
- (void)reload:(BOOL)noRefresh { //如果有網路連接 if ([Config Instance].isNetworkRunning) { if (isLoading || isLoadOver) { return; } if (!noRefresh) { allCount = 0; } int pageIndex = allCount/20; NSString *url; switch (self.catalog) { case 1: url = [NSString stringWithFormat:@"%@?catalog=%d&pageIndex=%d&pageSize=%d", api_news_list, 1, pageIndex, 20]; break; case 2: url = [NSString stringWithFormat:@"%@?type=latest&pageIndex=%d&pageSize=%d", api_blog_list, pageIndex, 20]; break; case 3: url = [NSString stringWithFormat:@"%@?type=recommend&pageIndex=%d&pageSize=%d", api_blog_list, pageIndex, 20]; break; } [[AFOSCClient sharedClient]getPath:url parameters:Nil success:^(AFHTTPRequestOperation *operation, id responseObject) { [Tool getOSCNotice2:operation.responseString]; isLoading = NO; if (!noRefresh) { [self clear]; } @try { NSMutableArray *newNews = self.catalog <= 1 ? [Tool readStrNewsArray:operation.responseString andOld: news]: [Tool readStrUserBlogsArray:operation.responseString andOld: news]; int count = [Tool isListOver2:operation.responseString]; allCount += count; if (count < 20) { isLoadOver = YES; } [news addObjectsFromArray:newNews]; [self.tableNews reloadData]; [self doneLoadingTableViewData]; //如果是第一頁 則緩衝下來 if (news.count <= 20) { [Tool saveCache:5 andID:self.catalog andString:operation.responseString]; } } @catch (NSException *exception) { [NdUncaughtExceptionHandler TakeException:exception]; } @finally { [self doneLoadingTableViewData]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"新聞列表擷取出錯"); //如果是重新整理 [self doneLoadingTableViewData]; if ([Config Instance].isNetworkRunning == NO) { return; } isLoading = NO; if ([Config Instance].isNetworkRunning) { [Tool ToastNotification:@"錯誤 網路無串連" andView:self.view andLoading:NO andIsBottom:NO]; } }]; isLoading = YES; [self.tableNews reloadData]; } //如果沒有網路連接 else { NSString *value = [Tool getCache:5 andID:self.catalog]; if (value) { NSMutableArray *newNews = [Tool readStrNewsArray:value andOld:news]; [self.tableNews reloadData]; isLoadOver = YES; [news addObjectsFromArray:newNews]; [self.tableNews reloadData]; [self doneLoadingTableViewData]; } } }