iOS SDK為HTTP請求提供了同步和異步請求兩種不同的API,而且可以使用GET或POST等請求方法。我們先了解其中最為簡單的同步GET方法請求。
首先實現查詢業務,查詢業務請求可以在主視圖控制器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;
-
-
-
-
- -(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方法代碼如下:
-
- -(void)startRequest
-
- {
-
- NSString *strURL = [[NSString alloc] initWithFormat:
-
- @”http:
-
- @”<你的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:,由于這些方法不能自定義是否要編碼和解碼的字符集,因此 沒有上面的函數靈活。