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]; } }}
分析一下這裡面的代碼:
首先是做一個網路連接判斷,在開源中國iOS用戶端學習——(六)網路連接檢測一文中介紹了,作者並不是用這種方法來判斷,而是使用getPath:parameters:success:failure:來判斷網路的串連,方法使用AFHTTPRequestOperation和“PATCH”請求HTTP用戶端操作隊列,使用到了block塊(iOS
4.0+特性),URL請求成功執行success塊裡操作,這裡面block塊沒有傳回值,接受兩個參數,建立請求操作和響應資料請求,URL請求失敗執行failure裡面的方法,這個block塊裡仍沒有傳回值,接受兩個參數建立請求操作和NSError對象,描述網路或解析錯誤狀況;
在 if()中的方法[Config Instance].isNetworkRunning==YES的,如果程式載入或者已經載入完畢什麼也不返回,如果程式沒有載入資料,將資料列表數量顯示為0,接下來是在switch()中,根據使用者選擇設定不同API介面(),然後就是解析顯示資料資訊,顯示在視圖中;
在AFNetwork 檔案夾中,作者自己添加了一個AFOSCClient類,該類繼承AFHTTPClient,又設計了一個sharedClient的類方法,從返回的結果可以推測出它是通過API請求返回json類型的資料,具體什麼作用還沒看出來;
[Tool getOSCNotice2:operation.responseString];是封裝在在Tool類中的解析擷取的XML的檔案
URL請求成功,還做了一個程式異常處理,防止請求資料過成功程式異常崩潰
關於@try @catch @finally異常處理的使用:
@try
{
//執行的代碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch裡面的內容
}
@catch
{
//除非try裡面執行代碼發生了異常,否則這裡的代碼不會執行
}
@finally
{
//不管什麼情況都會執行,包括try catch 裡面用了return ,可以理解為只要執行了try或者catch,就一定會執行 finally
}
如果URL請求的資料出錯,則反應網路不連通,資料不能載入,則彈出GCDiscreetNotificationView提示視圖 提示網路錯誤;
postPath:parameters:success:failure:方法在程式中使用舉例:
FriendsView.m
-(void)reload:(BOOL)noRefresh{ if (isLoadOver) { [self doneLoadingTableViewData]; return; } [[AFOSCClient sharedClient] postPath:api_friends_list parameters:[NSDictionary dictionaryWithObjectsAndKeys:segement.selectedSegmentIndex == 0 ? @"1" : @"0",@"relation", [NSString stringWithFormat:@"%d", friends.count/20],@"pageIndex", @"20",@"pageSize", [NSString stringWithFormat:@"%d", [Config Instance].getUID],@"uid",nil] success:^(AFHTTPRequestOperation *operation, id responseObject) { if (!noRefresh) { [self clear]; } [self doneLoadingTableViewData]; isLoading = NO; NSString *response = operation.responseString; [Tool getOSCNotice2:response]; @try { TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil]; TBXMLElement *root = xml.rootXMLElement; //顯示 TBXMLElement *_friends = [TBXML childElementNamed:@"friends" parentElement:root]; if (!_friends) { isLoadOver = YES; [self.tableFriends reloadData]; return; } TBXMLElement *first = [TBXML childElementNamed:@"friend" parentElement:_friends]; if (first == nil) { [self.tableFriends reloadData]; isLoadOver = YES; return; } NSMutableArray *newFriends = [[NSMutableArray alloc] initWithCapacity:20]; TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:first]; TBXMLElement *userid = [TBXML childElementNamed:@"userid" parentElement:first]; TBXMLElement *portrait = [TBXML childElementNamed:@"portrait" parentElement:first]; TBXMLElement *expertise = [TBXML childElementNamed:@"expertise" parentElement:first]; TBXMLElement *gender = [TBXML childElementNamed:@"gender" parentElement:first]; Friend *f = [[Friend alloc] initWithParameters:[TBXML textForElement:name] andUID:[[TBXML textForElement:userid] intValue] andPortrait:[TBXML textForElement:portrait] andExpertise:[TBXML textForElement:expertise] andMale:[[TBXML textForElement:gender] intValue] == 1]; if (![Tool isRepeatFriend: friends andFriend:f]) { [newFriends addObject:f]; } while (first) { first = [TBXML nextSiblingNamed:@"friend" searchFromElement:first]; if (first) { name = [TBXML childElementNamed:@"name" parentElement:first]; userid = [TBXML childElementNamed:@"userid" parentElement:first]; portrait = [TBXML childElementNamed:@"portrait" parentElement:first]; expertise = [TBXML childElementNamed:@"expertise" parentElement:first]; gender = [TBXML childElementNamed:@"gender" parentElement:first]; f = [[Friend alloc] initWithParameters:[TBXML textForElement:name] andUID:[[TBXML textForElement:userid] intValue] andPortrait:[TBXML textForElement:portrait] andExpertise:[TBXML textForElement:expertise] andMale:[[TBXML textForElement:gender] intValue] == 1]; if (![Tool isRepeatFriend:friends andFriend:f]) { [newFriends addObject:f]; } } else break; } if (newFriends.count < 20) { isLoadOver = YES; } [friends addObjectsFromArray:newFriends]; [self.tableFriends reloadData]; } @catch (NSException *exception) { [NdUncaughtExceptionHandler TakeException:exception]; } @finally { [self doneLoadingTableViewData]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"好友名單擷取出錯"); [self doneLoadingTableViewData]; isLoading = NO; if ([Config Instance].isNetworkRunning) { [Tool ToastNotification:@"錯誤 網路無串連" andView:self.view andLoading:NO andIsBottom:NO]; } }]; isLoading = YES; [self.tableFriends reloadData];}
這個方法和getPath:parameters:success:failure:不同的在於請求方式是POST請求,可以向伺服器裡提交資料;