iOS網路編程同步GET方法請求編程

來源:互聯網
上載者:User

標籤:style   http   java   color   使用   os   

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

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

 

Java代碼  
  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. //儲存資料列表  
  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檔案是處理把伺服器返回訊息代碼轉換為使用者能看懂的訊息。

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

 

 

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

 

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

 

 

 

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

 

Java代碼  
  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.   
  23. NSString *result = (NSString *)  
  24.   
  25. CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,①  
  26.   
  27. (CFStringRef)self,  
  28.   
  29. NULL,                           ②  
  30.   
  31. CFSTR(“+$,#[] “),                      ③  
  32.   
  33. kCFStringEncodingUTF8));  
  34.   
  35. return result;  
  36.   
  37. }  
  38.   
  39. - (NSString*)URLDecodedString  
  40.   
  41. {  
  42.   
  43. NSString *result = (NSString *)  
  44.   
  45. CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding  
  46.   
  47. (kCFAllocatorDefault,                                                 ③  
  48.   
  49. (CFStringRef)self, CFSTR(“”),                                       ④  
  50.   
  51. kCFStringEncodingUTF8));  
  52.   
  53. return result;  
  54.   
  55. }  
  56.   
  57. @end  

 

 

 

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

 

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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.