网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

开源中国iOS客户端学习——(八)网络通信AFNetworking类库-创新互联

AFNetworking是一个轻量级的iOS网络通信类库,继ASI类库不在更新之后开发者们有一套不错选择;

站在用户的角度思考问题,与客户深入沟通,找到庆元网站设计与庆元网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、主机域名网站空间、企业邮箱。业务覆盖庆元地区。

AFNetworking类库×××和使用教程: https://github.com/AFNetworking/AFNetworking

如果想深入研究有官方文档介绍:http://afnetworking.github.com/AFNetworking/

开源中国iOS客户端学习——(八)网络通信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接口(下图),然后就是解析显示数据信息,显示在视图中;

开源中国iOS客户端学习——(八)网络通信AFNetworking类库  开源中国iOS客户端学习——(八)网络通信AFNetworking类库

在AFNetwork 文件夹中,作者自己添加了一个AFOSCClient类,该类继承AFHTTPClient,又设计了一个sharedClient的类方法,从返回的结果可以推测出它是通过API请求返回json类型的数据,具体什么作用还没看出来;

开源中国iOS客户端学习——(八)网络通信AFNetworking类库

[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请求,可以向服务器里提交数据;



另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:开源中国iOS客户端学习——(八)网络通信AFNetworking类库-创新互联
浏览地址:http://bjjierui.cn/article/dppdpo.html