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]; } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索afnetworking
, ios afnetworking
, afnetwork
, operation
, ios类库
, success
, parameters
, Self
, failure
jquery_ajax的success
,以便于您获取更多的相关知识。