制作iPhone的SOAP应用的详细教程

本文介绍如何在iPhone应用中使用SOAP技术与远程服务器进行通信,包括基本概念、实例操作及代码实现。通过封装SOAP请求,实现与提供SOAP服务的服务器交互,并在用户界面上展示服务器响应结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者 bluebird

帖子地址 http://www.cocoachina.com/bbs/read.php?tid-16561.html

本教程用到的提供soap接口的网址是: http://www.Nanonull.com/TimeService/ 这个页面有多个方法可以通过soap调用,页面上也有说明.如果用IE的浏览器还能看到此网页提供的wsdl文件.要做soap的webservice首先要了解一些关于webservice和soap的一些基本知识.下面几个网址可能会帮你快速入门.

SOAP教程: http://www.w3school.com.cn/soap/index.asp
使用WSDL发布WebService: http://blog.youkuaiyun.com/meiqingsong/archive/2005/04/04/336057.aspx

    为了便于理解,我先讲下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.         IBOutletUITextField *nameInput;
  4.         IBOutletUILabel *greeting;
  5.  
  6.         NSMutableData*webData;
  7.         NSMutableString*soapResults;
  8.         NSXMLParser*xmlParser;
  9.         BOOLrecordResults;
  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.                                                         @"<?xml version="1.0"encoding="utf-8"?>n"
  7.                                                         "<soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n"
  8.                                                         "<soap:Body>n"
  9.                                                         "<getOffesetUTCTimexmlns="http://www.Nanonull.com/TimeService/">n"
  10.                                                         "<hoursOffset>%@</hoursOffset>n"
  11.                                                         "</getOffesetUTCTime>n"
  12.                                                         "</soap:Body>n"
  13.                                                         "</soap:Envelope>n",nameInput.text
  14.                                                         ];
  15.         NSLog(soapMessage);
  16.         //请求发送到的路径
  17.         NSURL*url = [NSURLURLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"];
  18.         NSMutableURLRequest*theRequest = [NSMutableURLRequest requestWithURL:url];
  19.         NSString*msgLength = [NSString stringWithFormat:@"%d", [soapMessagelength]];
  20.         
  21.         //以下对请求信息添加属性前四句是必有的,第五句是soap信息。
  22.         [theRequestaddValue: @"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
  23.         [theRequestaddValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime"forHTTPHeaderField:@"SOAPAction"];
  24.         
  25.         [theRequestaddValue: msgLength forHTTPHeaderField:@"Content-Length"];
  26.         [theRequestsetHTTPMethod:@"POST"];
  27.         [theRequestsetHTTPBody: [soapMessagedataUsingEncoding: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(@"theConnectionis NULL");
  40.         }
  41.         
  42.         
  43. }

这个方法作用就是封装soap请求,并向服务器发送请求.
代码有注释.不重复讲解.soap并不难,难的是没有案例告诉我们怎么把其它平台的soap移植过来,这里我给出了代码,我相信对iphone开发人员的话应该能看懂了.我在下面会把此案例的源代码附上.如果自己做不出来再看我的代码.如果我这样讲您觉得不够细,那说明您的iphone开发还不是太深入,那么您应该用不到soap技术.可以飘过了.
下面的代码是接收信息并解析,显示到用户界面

  1. -(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response
  2. {
  3.         [webDatasetLength: 0];
  4.         NSLog(@"connection:didReceiveResponse:1");
  5. }
  6. -(void)connection:(NSURLConnection *)connectiondidReceiveData:(NSData *)data
  7. {
  8.         [webDataappendData:data];
  9.         NSLog(@"connection:didReceiveData:2");
  10.  
  11. }
  12.  
  13. //如果电脑没有连接网络,则出现 此信息(不是网络服务器不通)
  14. -(void)connection:(NSURLConnection *)connectiondidFailWithError:(NSError *)error
  15. {
  16.         NSLog(@"ERRORwith theConenction");
  17.         [connectionrelease];
  18.         [webDatarelease];
  19. }
  20. -(void)connectionDidFinishLoading:(NSURLConnection*)connection
  21. {
  22.         NSLog(@"3DONE. Received Bytes: %d", [webData length]);
  23.         NSString*theXML = [[NSString alloc] initWithBytes: [webData mutableBytes]length:[webData length] encoding:NSUTF8StringEncoding];
  24.         NSLog(theXML);
  25.         [theXMLrelease];
  26.         
  27.         //重新加載xmlParser
  28.         if(xmlParser )
  29.         {
  30.                 [xmlParserrelease];
  31.         }
  32.         
  33.         xmlParser= [[NSXMLParser alloc] initWithData: webData];
  34.         [xmlParsersetDelegate: self];
  35.         [xmlParsersetShouldResolveExternalEntities: YES];
  36.         [xmlParserparse];
  37.         
  38.         [connectionrelease];
  39.         //[webDatarelease];
  40. }


  • -(void)parser:(NSXMLParser *)parser didStartElement:(NSString*)elementName namespaceURI:(NSString *) namespaceURIqualifiedName:(NSString *)qName
  • attributes: (NSDictionary *)attributeDict
  • {
  •         NSLog(@"4parser didStarElemen: namespaceURI: attributes:");
  •  
  •                 if([elementName isEqualToString:@"getOffesetUTCTimeResult"])
  •         {
  •                 if(!soapResults)
  •                 {
  •                         soapResults= [[NSMutableString alloc] init];
  •                 }
  •                 recordResults= YES;
  •         }
  •         
  • }
  • -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString*)string
  • {
  •         NSLog(@"5parser: foundCharacters:");
  •  
  •         if(recordResults )
  •         {
  •                 [soapResultsappendString: string];
  •         }
  • }
  • -(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName
  • {
  •         NSLog(@"6parser: didEndElement:");
  •  
  •         if([elementName isEqualToString:@"getOffesetUTCTimeResult"])
  •         {
  •                 recordResults= FALSE;
  •                 greeting.text= [[[NSString init]stringWithFormat:@"第%@时区的时间是: ",nameInput.text]stringByAppendingString:soapResults];
  •                 [soapResultsrelease];
  •                 soapResults= nil;
  •                 NSLog(@"hoursOffsetresult");
  •         }
  •         
  • }
  • - (void)parserDidStartDocument:(NSXMLParser *)parser{
  •         NSLog(@"-------------------start--------------");
  • }
  • - (void)parserDidEndDocument:(NSXMLParser *)parser{
  •         NSLog(@"-------------------end--------------");
  • }

说 明下:

  1. -(void)connectionDidFinishLoading:(NSURLConnection*)connection


这个方法是存储接收到的信息

  1. -(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName

方法是显示接收的信息.
以上代码已经包括所有用到的代码.
如果一切顺利,应该有如下界面
1.打开界面

2.输入请求的时区

3.显示服务器反馈结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值