iOS開發(fā)那些事-iOS網(wǎng)絡編程同步GET方法請求編程

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

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供三都網(wǎng)站建設、三都做網(wǎng)站、三都網(wǎng)站設計、三都網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、三都企業(yè)網(wǎng)站模板建站服務,10多年三都做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

為了學習這些API的使用我們MyNotes“備忘錄”應用實例,數(shù)據(jù)來源于服務器端,而不是本地的Notes.xml(或Notes.json)文件。

首先實現(xiàn)查詢業(yè)務,查詢業(yè)務請求可以在主視圖控制器MasterViewController類中實現(xiàn),其中MasterViewController.h代碼如下:

 

  1. #import <UIKit/UIKit.h> 
  2.  
  3. #import “NSString+URLEncoding.h” 
  4.  
  5. #import “NSNumber+Message.h” 
  6.  
  7.   
  8.  
  9. @interface MasterViewController : UITableViewController 
  10.  
  11.   
  12.  
  13. @property (strong, nonatomic) DetailViewController *detailViewController; 
  14.  
  15. //保存數(shù)據(jù)列表 
  16.  
  17. @property (nonatomic,strong) NSMutableArray* listData; 
  18.  
  19.   
  20.  
  21. //重新加載表視圖 
  22.  
  23. -(void)reloadView:(NSDictionary*)res; 
  24.  
  25.   
  26.  
  27. //開始請求Web Service 
  28.  
  29. -(void)startRequest; 
  30.  
  31.   
  32.  
  33. @end 

 

其中引入頭文件NSString+URLEncoding.h文件是在程序中需要對URL進行編碼處理。引入頭文件 NSNumber+Message.h文件是處理把服務器返回消息代碼轉換為用戶能看懂的消息。MasterViewController.m中的主要代 碼如下:

 

  1. - (void)viewDidLoad 
  2.  
  3.  
  4. [super viewDidLoad]; 
  5.  
  6. self.navigationItem.leftBarButtonItem = self.editButtonItem; 
  7.  
  8. self.detailViewController  = (DetailViewController *) 
  9.  
  10. [[self.splitViewController.viewControllers lastObject] topViewController]; 
  11.  
  12. [self startRequest];                                                ① 
  13.  
  14.  
  15.   
  16.  
  17. #pragma mark – Table View 
  18.  
  19. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  20.  
  21. return 1; 
  22.  
  23.  
  24.   
  25.  
  26. - (NSInteger)tableView:(UITableView *)tableView 
  27.  
  28. numberOfRowsInSection:(NSInteger)section { 
  29.  
  30. return self.listData.count; 
  31.  
  32.  
  33.   
  34.  
  35. - (UITableViewCell *)tableView:(UITableView *)tableView 
  36.  
  37. cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  38.  
  39. UITableViewCell *cell 
  40.  
  41. = [tableView dequeueReusableCellWithIdentifier:@"Cell" 
  42.  
  43. forIndexPath:indexPath]; 
  44.  
  45. NSMutableDictionary*  dict = self.listData[indexPath.row]; 
  46.  
  47. cell.textLabel.text = [dict objectForKey:@"Content"]; 
  48.  
  49. cell.detailTextLabel.text = [dict objectForKey:@"CDate"]; 
  50.  
  51. return cell; 
  52.  

其中第①行代碼[self startRequest]調用自己的方法startRequest實現(xiàn)請求Web Service。MasterViewController.m中的startRequest方法代碼如下:

 

  1. /* 
  2.  
  3. * 開始請求Web Service 
  4.  
  5. */ 
  6.  
  7. -(void)startRequest 
  8.  
  9.  
  10. NSString *strURL = [[NSString alloc] initWithFormat: 
  11.  
  12. @”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”, 
  13.  
  14. @”<你的iosbook1.com用戶郵箱>”,@”JSON”,@”query”];                           ① 
  15.  
  16. NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];             ② 
  17.  
  18. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];               ③ 
  19.  
  20. NSData *data  = [NSURLConnection sendSynchronousRequest:request 
  21.  
  22. returningResponse:nil error:nil];                       ④ 
  23.  
  24. NSLog(@”請求完成…”); 
  25.  
  26. NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data 
  27.  
  28. options:NSJSONReadingAllowFragments error:nil]; 
  29.  
  30. [self reloadView:resDict];                                              ⑤ 
  31.  

此外,我們在前文中還提到了一個分類NSString (URLEncoding),它的作用是對URL編碼和解碼,它的代碼如下:

 

  1. @interface NSString (URLEncoding) 
  2.  
  3.   
  4.  
  5. -(NSString *)URLEncodedString; 
  6.  
  7. -(NSString *)URLDecodedString; 
  8.  
  9.   
  10.  
  11. @end 
  12.  
  13.   
  14.  
  15. @implementation NSString (URLEncoding) 
  16.  
  17.   
  18.  
  19. - (NSString *)URLEncodedString 
  20.  
  21.  
  22. NSString *result = (NSString *) 
  23.  
  24. CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,① 
  25.  
  26. (CFStringRef)self, 
  27.  
  28. NULL,                           ② 
  29.  
  30. CFSTR(“+$,#[] “),                      ③ 
  31.  
  32. kCFStringEncodingUTF8)); 
  33.  
  34. return result; 
  35.  
  36.  
  37. - (NSString*)URLDecodedString 
  38.  
  39.  
  40. NSString *result = (NSString *) 
  41.  
  42. CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding 
  43.  
  44. (kCFAllocatorDefault,                                                 ③ 
  45.  
  46. (CFStringRef)self, CFSTR(“”),                                       ④ 
  47.  
  48. kCFStringEncodingUTF8)); 
  49.  
  50. return result; 
  51.  
  52.  
  53. @end 

第①行代碼CFURLCreateStringByAddingPercentEscape函數(shù)是Core Foundation框架提供的C函數(shù),可以把內(nèi)容轉換成為URL編碼。第②行參數(shù)指定了將本身為非法URL字符不進行編碼的字符集合,例如:“!* ()”等符號。第③行參數(shù)是將本身為合法URL字符需要進行編碼的字符集合。

第③行代碼CFURLCreateStringByReplacingPercentEscapesUsingEncoding函數(shù)是Core Foundation框架提供的C函數(shù),它與上面CFURLCreateStringByAddingPercentEscape函數(shù)截然相反,是進行 URL解碼的。第④行的參數(shù)指定不進行解碼的字符集。

Foundation框架也提供了基于Objective-C的方法進行URL編碼和解碼,與 CFURLCreateStringByAddingPercentEscape函數(shù)對應的NSString方法是 stringByAddingPercentEscapesUsingEncoding。與 CFURLCreateStringByReplacingPercentEscapesUsingEncoding函數(shù)對應的NSString方法是 stringByReplacingPercentEscapesUsingEncoding:,由于這些方法不能自定義是否要編碼和解碼的字符集,因此 沒有上面的函數(shù)靈活。

新聞名稱:iOS開發(fā)那些事-iOS網(wǎng)絡編程同步GET方法請求編程
地址分享:http://muchs.cn/article28/ihiccp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設網(wǎng)站收錄、微信小程序自適應網(wǎng)站、Google、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營