国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 20:13:35
字體:
來源:轉載
供稿:網友

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(@”請求完成&hellip;”);  
  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:,由于這些方法不能自定義是否要編碼和解碼的字符集,因此 沒有上面的函數靈活。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新建县| 阿勒泰市| 黎川县| 蒙城县| 巴林右旗| 镇赉县| 涞水县| 隆回县| 贵州省| 神农架林区| 始兴县| 拉萨市| 铜陵市| 安徽省| 淮阳县| 大足县| 漳浦县| 资源县| 卢氏县| 晋江市| 衢州市| 金坛市| 视频| 丰镇市| 车险| 柳江县| 高平市| 商河县| 威信县| 静海县| 庄河市| 武隆县| 新乡市| 舒城县| 高密市| 昭苏县| 佛教| 莲花县| 双桥区| 湖口县| 宜川县|