为 了便于理解,我先讲下soap的大体原理:我们在iPhone封装soap请求信息,发送到某个提供soap服务的服务器,如下例中我们用到的http://www.Nanonull.com/TimeService/.服 务器能接受和识别soap请求,当它接到请求,就根据客户端的请求情况调用服务器上的某个函数,并将函数返回结果封装成soap反馈信息发送给客户端.客 户端接收到soap反馈信息后,进行解析处理,以用户能理解的形式呈现给用户.整个过程就这么简单.
好了,假设现在你已经有关于 soap的基础知识(没有也没关系,看了例子,再理解就更好理解了),下面我们开始做soap的例子.
第一步,建一个 Hello_SOAP项目.用IB将Hello_SOAPViewController.xib做成如下图的界面
然后在Hello_SOAPViewController.h中添加如下代码
代码
1. @interface Hello_SOAPViewController : UIViewController 2. { 3. IBOutlet UITextField *nameInput; 4. IBOutlet UILabel *greeting; 5. 6. NSMutableData *webData; 7. NSMutableString *soapResults; 8. NSXMLParser *xmlParser; 9. BOOL recordResults; 10. } 11. 12. @property(nonatomic, retain) IBOutlet UITextField *nameInput; 13. @property(nonatomic, retain) IBOutlet UILabel *greeting; 14. 15. @property(nonatomic, retain) NSMutableData *webData; 16. @property(nonatomic, retain) NSMutableString *soapResults; 17. @property(nonatomic, retain) NSXMLParser *xmlParser; 18. 19. -(IBAction)buttonClick: (id) sender; 20. - (void)getOffesetUTCTimeSOAP;
然后在Hello_SOAPViewController.xib中将两个输出口和一个动作连接好,这个不用手把手吧?
在 Hello_SOAPViewController.m文件中加入以下方法 :
代码
1. - (void)getOffesetUTCTimeSOAP 2. { 3. recordResults = NO; 4.//封装soap请求消息 5. NSString *soapMessage = [NSString stringWithFormat: 6. @"n" 7. "n" 8. "n" 9. "n" 10. "%@n" 11. "n" 12. "n" 13. "n",nameInput.text 14. ]; 15. NSLog(soapMessage); 16. //请求发送到的路径 17. NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"]; 18. NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 19. NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 20. 21. // 以下对请求信息添加属性前四句是必有的,第五句是soap信息。 22. [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 23. [theRequest addValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime" forHTTPHeaderField:@"SOAPAction"]; 24. 25. [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 26. [theRequest setHTTPMethod:@"POST"]; 27. [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 28. 29. // 请求 30. NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 31. 32. // 如果连接已经建好,则初始化data 33. if( theConnection ) 34. { 35. webData = [[NSMutableData data] retain]; 36. } 37. else 38. { 39. NSLog(@"theConnection is NULL"); 40. } 41. 42. 43. }
这个方法作用就是封装soap请求,并向服务器发送请求.
代码有注释.不重复讲解.soap并不难,难的是没有案例告诉我们怎么把其它平台的 soap移植过来,这里我给出了代码,我相信对iphone开发人员的话应该能看懂了.我在下面会把此案例的源代码附上.如果自己做不出来再看我的代码. 如果我这样讲您觉得不够细,那说明您的iphone开发还不是太深入,那么您应该用不到soap技术.可以飘过了.
下面的代码是接收信息并解析, 显示到用户界面
代码
1. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 2. { 3. [webData setLength: 0]; 4. NSLog(@"connection: didReceiveResponse:1"); 5. } 6. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 7.{ 8. [webData appendData:data]; 9. NSLog(@"connection: didReceiveData:2"); 10. 11. } 12. 13. //如果电脑没有连接网络,则出现 此信息(不是网络服务器不通) 14. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 15. { 16. NSLog(@"ERROR with theConenction"); 17. [connection release]; 18. [webData release]; 19. } 20. -(void)connectionDidFinishLoading:(NSURLConnection *)connection 21. { 22. NSLog(@"3 DONE. Received Bytes: %d", [webData length]); 23. NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 24. NSLog(theXML); 25. [theXML release]; 26. 27. //重新加蒌xmlParser 28. if( xmlParser ) 29. { 30. [xmlParser release]; 31. } 32. 33. xmlParser = [[NSXMLParser alloc] initWithData: webData]; 34. [xmlParser setDelegate: self]; 35. [xmlParser setShouldResolveExternalEntities: YES]; 36. [xmlParser parse]; 37. 38. [connection release]; 39. //[webData release]; 40. }
更多信息请查看IT技术专栏