為 了便於理解,我先講下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. }