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

来源:互联网 发布:web前端面试题 知乎 编辑:程序博客网 时间:2024/06/05 16:38

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 *)tableViewnumberOfRowsInSection:(NSInteger)section {return self.listData.count;} - (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(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:requestreturningResponse:nil error:nil];                       ④NSLog(@”请求完成…”);NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:dataoptions: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)self, CFSTR(“”),                                       ④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:,由于这些方法不能自定义是否要编码和解码的字符集,因此没有上面的函数灵活。

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


原创粉丝点击