手把手教你做iOS的soap應用 webservice
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
引自:http://www.cocoachina.com/bbs/read.php?tid=16561
用到的提供soap介面的網址是:http://www.Nanonull.com/TimeService/
soap教程:http://www.w3school.com.cn/soap/index.asp
使用WSDL釋出WebService:http://blog.csdn.net/meiqingsong/archive/2005/04/04/336057.aspx
為了便於理解,我先講下soap的大體原理:我們在iphone封裝soap請求資訊,傳送到某個提供soap服務的伺服器,如下例中我們用到的
好了,假設現在你已經有關於soap的基礎知識(沒有也沒關係,看了例子,再理解就更好理解了),下面我們開始做soap的例子.
第一步,建一個Hello_SOAP專案.
然後在Hello_SOAPViewController.h中新增如下程式碼
@interface Hello_SOAPViewController : UIViewController
{
IBOutlet UITextField *nameInput;
IBOutlet UILabel *greeting;
NSMutableData *webData;
NSMutableString *soapResults;
NSXMLParser *xmlParser;
BOOL recordResults;
}
@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableString *soapResults;
@property(nonatomic, retain) NSXMLParser *xmlParser;
-(IBAction)buttonClick: (id) sender;
- (void)getOffesetUTCTimeSOAP;
然後在Hello_SOAPViewController.xib中將兩個輸出口和一個動作連線好,這個不用手把手吧?
在Hello_SOAPViewController.m檔案中加入以下方法 :
- (void)getOffesetUTCTimeSOAP
{
recordResults = NO;
//封裝soap請求訊息
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns: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"
"<soap:Body>\n"
"<getOffesetUTCTime xmlns=\"http://www.Nanonull.com/TimeService/\">\n"
"<hoursOffset>%@</hoursOffset>\n"
"</getOffesetUTCTime>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",nameInput.text
];
NSLog(soapMessage);
//請求傳送到的路徑
NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
//以下對請求資訊新增屬性前四句是必有的,第五句是soap資訊。
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//請求
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
//如果連線已經建好,則初始化data
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
}
這個方法作用就是封裝soap請求,並向伺服器傳送請求.
程式碼有註釋.不重複講解.soap並不難,難的是沒有案例告訴我們怎麼把其它平臺的soap移植過來,這裡我給出了程式碼,我相信對iphone開發人員的話應該能看懂了.我在下面會把此案例的原始碼附上.如果自己做不出來再看我的程式碼.如果我這樣講您覺得不夠細,那說明您的iphone開發還不是太深入,那麼您應該用不到soap技術.可以飄過了.
下面的程式碼是接收資訊並解析,顯示到使用者介面
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
NSLog(@"connection: didReceiveResponse:1");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"connection: didReceiveData:2");
}
//如果電腦沒有連線網路,則出現此資訊(不是網路伺服器不通)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"3 DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(theXML);
[theXML release];
//重新載入xmlParser
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[connection release];
//[webData release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
NSLog(@"4 parser didStarElemen: namespaceURI: attributes:");
if( [elementName isEqualToString:@"getOffesetUTCTimeResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = YES;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"5 parser: foundCharacters:");
if( recordResults )
{
[soapResults appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"6 parser: didEndElement:");
if( [elementName isEqualToString:@"getOffesetUTCTimeResult"])
{
recordResults = FALSE;
greeting.text = [[[NSString init]stringWithFormat:@"第%@時區的時間是: ",nameInput.text] stringByAppendingString:soapResults];
[soapResults release];
soapResults = nil;
NSLog(@"hoursOffset result");
}
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"-------------------start--------------");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"-------------------end--------------");
}
說明下:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
這個方法是儲存接收到的資訊
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>