iOS SDK为HTTP请求提供了同步和异步请求两种不同的API,而且可以使用GET或POST等请求方法。我们先了解其中最为简单的同步GET方法请求。
为了学习这些API的使用我们还是选择第3章MyNotes“备忘录”应用实例,与第3章不同的是数据来源于服务器端,而不是本地的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)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:,由于这些方法不能自定义是否要编码和解码的字符集,因此 没有上面的函数灵活。