教你制作iPhone的SOAP應(yīng)用
為 了便于理解,我先講下soap的大體原理:我們在iPhone封裝soap請求信息,發(fā)送到某個提供soap服務(wù)的服務(wù)器,如下例中我們用到的/TimeService/.服 務(wù)器能接受和識別soap請求,當(dāng)它接到請求,就根據(jù)客戶端的請求情況調(diào)用服務(wù)器上的某個函數(shù),并將函數(shù)返回結(jié)果封裝成soap反饋信息發(fā)送給客戶端.客 戶端接收到soap反饋信息后,進行解析處理,以用戶能理解的形式呈現(xiàn)給用戶.整個過程就這么簡單.
好了,假設(shè)現(xiàn)在你已經(jīng)有關(guān)于 soap的基礎(chǔ)知識(沒有也沒關(guān)系,看了例子,再理解就更好理解了),下面我們開始做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. //請求發(fā)送到的`路徑 17. NSURL *url = [NSURL URLWithString:@"/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: @"/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. // 如果連接已經(jīng)建好,則初始化data 33. if( theConnection ) 34. { 35. webData = [[NSMutableData data] retain]; 36. } 37. else 38. { 39. NSLog(@"theConnection is NULL"); 40. } 41. 42. 43. }
這個方法作用就是封裝soap請求,并向服務(wù)器發(fā)送請求.
代碼有注釋.不重復(fù)講解.soap并不難,難的是沒有案例告訴我們怎么把其它平臺的 soap移植過來,這里我給出了代碼,我相信對iphone開發(fā)人員的話應(yīng)該能看懂了.我在下面會把此案例的源代碼附上.如果自己做不出來再看我的代碼. 如果我這樣講您覺得不夠細,那說明您的iphone開發(fā)還不是太深入,那么您應(yīng)該用不到soap技術(shù).可以飄過了.
下面的代碼是接收信息并解析, 顯示到用戶界面
代碼
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. //如果電腦沒有連接網(wǎng)絡(luò),則出現(xiàn) 此信息(不是網(wǎng)絡(luò)服務(wù)器不通) 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. }
【教你制作iPhone的SOAP應(yīng)用】相關(guān)文章:
1.SOAP語法
2.SOAP簡介