iOS SDK為HTTP請求提供了同步和異步請求兩種不同的API,而且可以使用GET或POST等請求方法。我們先了解其中最為簡單的同步GET方法請求。
為了學習這些API的使用MyNotes“備忘錄”應用實例,數據來源於服務器端,而不是本地的Notes.xml(或Notes.json)文件。
首先實現查詢業務,查詢業務請求可以在主視圖控制器MasterViewController類中實現,其中MasterViewController.h代碼如下:
[cpp]
#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
#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中的主要代碼如下:
[cpp] view plaincopyprint?- (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]; ⑤
}
- (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編碼和解碼,它的代碼如下:
[cpp]
@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
@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:,由於這些方法不能自定義是否要編碼和解碼的字符集,因此沒有上面的函數靈活。