iOS开发那些事-iOS网络编程同步GET方法请求编程
iOS SDK为HTTP请求提供了同步和异步请求两种不同的API,而且可以使用GET或POST等请求方法。我们先了解其中最为简单的同步GET方法请求。
为了学习这些API的使用我们MyNotes“备忘录”应用实例,数据来源于服务器端,而不是本地的Notes.xml(或Notes.json)文件。
首先实现查询业务,查询业务请求可以在主视图控制器MasterViewController类中实现,其中MasterViewController.h代码如下:
#import<UIKit/UIKit.h>#import“NSString+URLEncoding.h”#import“NSNumber+Message.h”@interfaceMasterViewController:UITableViewController@property(strong,nonatomic)DetailViewController*detailViewController;//保存数据列表@property(nonatomic,strong)NSMutableArray*listData;//重新加载表视图-(void)reloadView:(NSDictionary*)res;//开始请求WebService-(void)startRequest;@end
其中引入头文件NSString+URLEncoding.h文件是在程序中需要对URL进行编码处理。引入头文件 NSNumber+Message.h文件是处理把服务器返回消息代码转换为用户能看懂的消息。MasterViewController.m中的主要代 码如下:
-(void)viewDidLoad{[superviewDidLoad];self.navigationItem.leftBarButtonItem=self.editButtonItem;self.detailViewController=(DetailViewController*)[[self.splitViewController.viewControllerslastObject]topViewController];[selfstartRequest];①}#pragmamark–TableView-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return1;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{returnself.listData.count;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];NSMutableDictionary*dict=self.listData[indexPath.row];cell.textLabel.text=[dictobjectForKey:@"Content"];cell.detailTextLabel.text=[dictobjectForKey:@"CDate"];returncell;}
其中第①行代码[self startRequest]调用自己的方法startRequest实现请求Web Service。MasterViewController.m中的startRequest方法代码如下:
/**开始请求WebService*/-(void)startRequest{NSString*strURL=[[NSStringalloc]initWithFormat:@”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”,@”<你的iosbook1.com用户邮箱>”,@”JSON”,@”query”];①NSURL*url=[NSURLURLWithString:[strURLURLEncodedString]];②NSURLRequest*request=[[NSURLRequestalloc]initWithURL:url];③NSData*data=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];④NSLog(@”请求完成…”);NSDictionary*resDict=[NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingAllowFragmentserror:nil];[selfreloadView:resDict];⑤}
此外,我们在前文中还提到了一个分类NSString (URLEncoding),它的作用是对URL编码和解码,它的代码如下:
@interfaceNSString(URLEncoding)-(NSString*)URLEncodedString;-(NSString*)URLDecodedString;@end@implementationNSString(URLEncoding)-(NSString*)URLEncodedString{NSString*result=(NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,①(CFStringRef)self,NULL,②CFSTR(“+$,#[]“),③kCFStringEncodingUTF8));returnresult;}-(NSString*)URLDecodedString{NSString*result=(NSString*)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,③(CFStringRef)self,CFSTR(“”),④kCFStringEncodingUTF8));returnresult;}@end
第①行代码CFURLCreateStringByAddingPercentEscape函数是Core Foundation框架提供的C函数,可以把内容转换成为URL编码。第②行参数指定了将本身为非法URL字符不进行编码的字符集合,例如:“!* ()”等符号。第③行参数是将本身为合法URL字符需要进行编码的字符集合。
第③行代码CFURLCreateStringByReplacingPercentEscapesUsingEncoding函数是Core Foundation框架提供的C函数,它与上面CFURLCreateStringByAddingPercentEscape函数截然相反,是进行 URL解码的。第④行的参数指定不进行解码的字符集。
Foundation框架也提供了基于Objective-C的方法进行URL编码和解码,与 CFURLCreateStringByAddingPercentEscape函数对应的NSString方法是 stringByAddingPercentEscapesUsingEncoding。与 CFURLCreateStringByReplacingPercentEscapesUsingEncoding函数对应的NSString方法是 stringByReplacingPercentEscapesUsingEncoding:,由于这些方法不能自定义是否要编码和解码的字符集,因此 没有上面的函数灵活。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。