iOS开发那些事-iOS网络编程同步GET方法请求编程

发布时间:2020-07-10 19:39:36 作者:tony关东升
来源:网络 阅读:357

iOS SDK为HTTP请求提供了同步和异步请求两种不同的API,而且可以使用GET或POST等请求方法。我们先了解其中最为简单的同步GET方法请求。

为了学习这些API的使用我们MyNotes“备忘录”应用实例,数据来源于服务器端,而不是本地的Notes.xml(或Notes.json)文件。

首先实现查询业务,查询业务请求可以在主视图控制器MasterViewController类中实现,其中MasterViewController.h代码如下:

 

  1. #import <UIKit/UIKit.h> 
  2.  
  3. #import “NSString+URLEncoding.h” 
  4.  
  5. #import “NSNumber+Message.h” 
  6.  
  7.   
  8.  
  9. @interface MasterViewController : UITableViewController 
  10.  
  11.   
  12.  
  13. @property (strong, nonatomic) DetailViewController *detailViewController; 
  14.  
  15. //保存数据列表 
  16.  
  17. @property (nonatomic,strong) NSMutableArray* listData; 
  18.  
  19.   
  20.  
  21. //重新加载表视图 
  22.  
  23. -(void)reloadView:(NSDictionary*)res; 
  24.  
  25.   
  26.  
  27. //开始请求Web Service 
  28.  
  29. -(void)startRequest; 
  30.  
  31.   
  32.  
  33. @end 

 

其中引入头文件NSString+URLEncoding.h文件是在程序中需要对URL进行编码处理。引入头文件 NSNumber+Message.h文件是处理把服务器返回消息代码转换为用户能看懂的消息。MasterViewController.m中的主要代 码如下:

 

  1. - (void)viewDidLoad 
  2.  
  3.  
  4. [super viewDidLoad]; 
  5.  
  6. self.navigationItem.leftBarButtonItem = self.editButtonItem; 
  7.  
  8. self.detailViewController  = (DetailViewController *) 
  9.  
  10. [[self.splitViewController.viewControllers lastObject] topViewController]; 
  11.  
  12. [self startRequest];                                                ① 
  13.  
  14.  
  15.   
  16.  
  17. #pragma mark – Table View 
  18.  
  19. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  20.  
  21. return 1
  22.  
  23.  
  24.   
  25.  
  26. - (NSInteger)tableView:(UITableView *)tableView 
  27.  
  28. numberOfRowsInSection:(NSInteger)section { 
  29.  
  30. return self.listData.count; 
  31.  
  32.  
  33.   
  34.  
  35. - (UITableViewCell *)tableView:(UITableView *)tableView 
  36.  
  37. cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  38.  
  39. UITableViewCell *cell 
  40.  
  41. = [tableView dequeueReusableCellWithIdentifier:@"Cell" 
  42.  
  43. forIndexPath:indexPath]; 
  44.  
  45. NSMutableDictionary*  dict = self.listData[indexPath.row]; 
  46.  
  47. cell.textLabel.text = [dict objectForKey:@"Content"]; 
  48.  
  49. cell.detailTextLabel.text = [dict objectForKey:@"CDate"]; 
  50.  
  51. return cell; 
  52.  

其中第①行代码[self startRequest]调用自己的方法startRequest实现请求Web Service。MasterViewController.m中的startRequest方法代码如下:

 

  1. /* 
  2.  
  3. * 开始请求Web Service 
  4.  
  5. */ 
  6.  
  7. -(void)startRequest 
  8.  
  9.  
  10. NSString *strURL = [[NSString alloc] initWithFormat: 
  11.  
  12. @”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”, 
  13.  
  14. @”<你的iosbook1.com用户邮箱>”,@”JSON”,@”query”];                           ① 
  15.  
  16. NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];             ② 
  17.  
  18. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];               ③ 
  19.  
  20. NSData *data  = [NSURLConnection sendSynchronousRequest:request 
  21.  
  22. returningResponse:nil error:nil];                       ④ 
  23.  
  24. NSLog(@”请求完成…”); 
  25.  
  26. NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data 
  27.  
  28. options:NSJSONReadingAllowFragments error:nil]; 
  29.  
  30. [self reloadView:resDict];                                              ⑤ 
  31.  

此外,我们在前文中还提到了一个分类NSString (URLEncoding),它的作用是对URL编码和解码,它的代码如下:

 

  1. @interface NSString (URLEncoding) 
  2.  
  3.   
  4.  
  5. -(NSString *)URLEncodedString; 
  6.  
  7. -(NSString *)URLDecodedString; 
  8.  
  9.   
  10.  
  11. @end 
  12.  
  13.   
  14.  
  15. @implementation NSString (URLEncoding) 
  16.  
  17.   
  18.  
  19. - (NSString *)URLEncodedString 
  20.  
  21.  
  22. NSString *result = (NSString *) 
  23.  
  24. CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,① 
  25.  
  26. (CFStringRef)self, 
  27.  
  28. NULL,                           ② 
  29.  
  30. CFSTR(“+$,#[] “),                      ③ 
  31.  
  32. kCFStringEncodingUTF8)); 
  33.  
  34. return result; 
  35.  
  36.  
  37. - (NSString*)URLDecodedString 
  38.  
  39.  
  40. NSString *result = (NSString *) 
  41.  
  42. CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding 
  43.  
  44. (kCFAllocatorDefault,                                                 ③ 
  45.  
  46. (CFStringRef)self, CFSTR(“”),                                       ④ 
  47.  
  48. kCFStringEncodingUTF8)); 
  49.  
  50. return result; 
  51.  
  52.  
  53. @end 

第①行代码CFURLCreateStringByAddingPercentEscape函数是Core Foundation框架提供的C函数,可以把内容转换成为URL编码。第②行参数指定了将本身为非法URL字符不进行编码的字符集合,例如:“!* ()”等符号。第③行参数是将本身为合法URL字符需要进行编码的字符集合。

第③行代码CFURLCreateStringByReplacingPercentEscapesUsingEncoding函数是Core Foundation框架提供的C函数,它与上面CFURLCreateStringByAddingPercentEscape函数截然相反,是进行 URL解码的。第④行的参数指定不进行解码的字符集。

Foundation框架也提供了基于Objective-C的方法进行URL编码和解码,与 CFURLCreateStringByAddingPercentEscape函数对应的NSString方法是 stringByAddingPercentEscapesUsingEncoding。与 CFURLCreateStringByReplacingPercentEscapesUsingEncoding函数对应的NSString方法是 stringByReplacingPercentEscapesUsingEncoding:,由于这些方法不能自定义是否要编码和解码的字符集,因此 没有上面的函数灵活。

推荐阅读:
  1. ios开发网络编程
  2. iOS开发那些事-iOS6苹果地图实用开发

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

get xcode4 ios开发那些事

上一篇:这一次,彻底弄懂 Promise

下一篇:zookeeper和hbase安装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》