• iOS网络编程同步GET方法请求编程


    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:,由于这些方法不能自定义是否要编码和解码的字符集,因此 没有上面的函数灵活。

  • 相关阅读:
    一道初中练习题,现在的我几乎差点很有可能搞不好似乎仿佛就没有做出来.
    [转]IBM ThinkPad 全新、原装机验机流程(完全版)
    在一个BBS上看到的.觉得很不错,放过来.
    验证数字的正则表达式集
    基于百度地图SDK + SQLite数据库的安卓管理系统
    C# 将系统时间转换成农历时间
    博客园
    C#正则表达式的简单用法
    单张图片轮换
    2个有焦点时文字消失或变淡效果
  • 原文地址:https://www.cnblogs.com/lovewx/p/3860138.html
Copyright © 2020-2023  润新知