温馨提示×

温馨提示×

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

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

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

发布时间:2020-07-10 19:39:36 阅读:363 作者:tony关东升 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

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

#import <UIKit/UIKit.h>  #import “NSString+URLEncoding.h”  #import “NSNumber+Message.h”     @interface MasterViewController : UITableViewController     @property (strong, nonatomic) DetailViewController *detailViewController;  //保存数据列表  @property (nonatomic,strong) NSMutableArray* listData;     //重新加载表视图  -(void)reloadView:(NSDictionary*)res;     //开始请求Web Service  -(void)startRequest;     @end 

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

- (void)viewDidLoad  {  [super viewDidLoad];  self.navigationItem.leftBarButtonItem = self.editButtonItem;  self.detailViewController  = (DetailViewController *)  [[self.splitViewController.viewControllers lastObject] topViewController];  [self startRequest];                                                ①  }     #pragma mark – Table View  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  return 1;  }     - (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section {  return self.listData.count;  }     - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {  UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];  NSMutableDictionary*  dict = self.listData[indexPath.row];  cell.textLabel.text = [dict objectForKey:@"Content"];  cell.detailTextLabel.text = [dict objectForKey:@"CDate"];  return cell;  } 

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

/*  * 开始请求Web Service  */  -(void)startRequest  {  NSString *strURL = [[NSString alloc] initWithFormat:  @”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”,  @”<你的iosbook1.com用户邮箱>”,@”JSON”,@”query”];                           ①  NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];             ②  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];               ③  NSData *data  = [NSURLConnection sendSynchronousRequest:request  returningResponse:nil error:nil];                       ④  NSLog(@”请求完成…”);  NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data  options:NSJSONReadingAllowFragments error:nil];  [self reloadView:resDict];                                              ⑤  } 

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

@interface NSString (URLEncoding)     -(NSString *)URLEncodedString;  -(NSString *)URLDecodedString;     @end     @implementation NSString (URLEncoding)     - (NSString *)URLEncodedString  {  NSString *result = (NSString *)  CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,①  (CFStringRef)self,  NULL,                           ②  CFSTR(“+$,#[] “),                      ③  kCFStringEncodingUTF8));  return result;  }  - (NSString*)URLDecodedString  {  NSString *result = (NSString *)  CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding  (kCFAllocatorDefault,                                                 ③  (CFStringRef)selfCFSTR(“”),                                       ④  kCFStringEncodingUTF8));  return result;  }  @end 

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

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

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×