iOS SDK為HTTP請求提供了同步和異步請求兩種不同的API,而且可以使用GET或POST等請求方法。我們先了解其中最為簡單的同步GET方法請求。

成都創新互聯專業為企業提供三都網站建設、三都做網站、三都網站設計、三都網站制作等企業網站建設、網頁設計與制作、三都企業網站模板建站服務,10多年三都做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
為了學習這些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)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方法請求編程
地址分享:http://www.js-pz168.com/article28/ihiccp.html
成都網站建設公司_創新互聯,為您提供品牌網站建設、網站收錄、微信小程序、自適應網站、Google、企業建站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯