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

简介:

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


     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208682 ,如需转载请自行联系原作者




相关文章
|
6天前
|
监控 网络协议 Linux
网络学习
网络学习
122 67
|
3天前
|
网络协议 C语言
C语言 网络编程(十)TCP通信创建流程---客户端
在TCP通信中,客户端需通过一系列步骤与服务器建立连接并进行数据传输。首先使用 `socket()` 函数创建一个流式套接字,然后通过 `connect()` 函数连接服务器。连接成功后,可以使用 `send()` 和 `recv()` 函数进行数据发送和接收。最后展示了一个完整的客户端示例代码,实现了与服务器的通信过程。
|
6天前
|
网络协议 安全 网络安全
网络基础知识学习
【9月更文挑战第1天】
25 0
|
10天前
|
前端开发 算法 网络协议
如何学习计算机基础知识,打好前端和网络安全的基础
如何学习计算机基础知识,打好前端和网络安全的基础
23 4
|
6天前
|
安全 Linux 网络安全
网络安全学习
【9月更文挑战第1天】
18 0
|
7天前
|
网络协议 C# 开发者
WPF与Socket编程的完美邂逅:打造流畅网络通信体验——从客户端到服务器端,手把手教你实现基于Socket的实时数据交换
【8月更文挑战第31天】网络通信在现代应用中至关重要,Socket编程作为其实现基础,即便在主要用于桌面应用的Windows Presentation Foundation(WPF)中也发挥着重要作用。本文通过最佳实践,详细介绍如何在WPF应用中利用Socket实现网络通信,包括创建WPF项目、设计用户界面、实现Socket通信逻辑及搭建简单服务器端的全过程。具体步骤涵盖从UI设计到前后端交互的各个环节,并附有详尽示例代码,助力WPF开发者掌握这一关键技术,拓展应用程序的功能与实用性。
20 0
|
1天前
|
存储 安全 网络安全
云计算与网络安全:云服务、网络安全、信息安全等技术领域的探讨
【9月更文挑战第5天】云计算作为一种新兴的计算模式,已经在全球范围内得到了广泛的应用。然而,随着云计算的快速发展,网络安全问题也日益凸显。本文将从云服务、网络安全、信息安全等方面对云计算与网络安全进行探讨。
26 15
|
2天前
|
安全 算法 网络安全
网络安全与信息安全:漏洞、加密与安全意识的三重奏
【9月更文挑战第4天】在数字时代的交响乐中,网络安全与信息安全是不可或缺的乐章。本文将深入探讨网络安全的脆弱性,揭示那些隐藏在光鲜表面下的潜在风险。我们将一同穿梭于加密技术的迷宫,解锁保护数据的神秘钥匙。更重要的是,本文将点亮一盏灯,照亮培养个人和组织安全意识的道路。通过深入浅出的分析与生动的案例,我们将共同见证网络安全的复杂性、加密技术的力量以及安全意识的重要性。让我们携手,为这场数字交响乐谱写一曲无懈可击的安全篇章。
|
1天前
|
安全 网络安全 数据安全/隐私保护
网络安全与信息安全:关于网络安全漏洞、加密技术、安全意识等方面的知识分享
【9月更文挑战第5天】在数字化时代,网络安全和信息安全已成为全球关注的焦点。本文将探讨网络安全漏洞、加密技术和安全意识等方面的内容,以帮助读者更好地了解网络安全的重要性,并提高自己的网络安全防护能力。我们将通过分析网络安全漏洞的原因和影响,介绍加密技术的基本原理和应用,以及强调安全意识在防范网络攻击中的关键作用。最后,我们将提供一些实用的建议,帮助读者保护自己的网络安全。
下一篇
DDNS