制作iPhone的SOAP應用
來源:易賢網(wǎng) 閱讀:858 次 日期:2014-11-04 09:05:20
溫馨提示:易賢網(wǎng)小編為您整理了“制作iPhone的SOAP應用”,方便廣大網(wǎng)友查閱!

為 了便于理解,我先講下soap的大體原理:我們在iPhone封裝soap請求信息,發(fā)送到某個提供soap服務的服務器,如下例中我們用到的http://www.Nanonull.com/TimeService/.服 務器能接受和識別soap請求,當它接到請求,就根據(jù)客戶端的請求情況調用服務器上的某個函數(shù),并將函數(shù)返回結果封裝成soap反饋信息發(fā)送給客戶端.客 戶端接收到soap反饋信息后,進行解析處理,以用戶能理解的形式呈現(xiàn)給用戶.整個過程就這么簡單.

好了,假設現(xiàn)在你已經(jīng)有關于 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. //請求發(fā)送到的路徑 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. // 如果連接已經(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請求,并向服務器發(fā)送請求.

代碼有注釋.不重復講解.soap并不難,難的是沒有案例告訴我們怎么把其它平臺的 soap移植過來,這里我給出了代碼,我相信對iphone開發(fā)人員的話應該能看懂了.我在下面會把此案例的源代碼附上.如果自己做不出來再看我的代碼. 如果我這樣講您覺得不夠細,那說明您的iphone開發(fā)還不是太深入,那么您應該用不到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. //如果電腦沒有連接網(wǎng)絡,則出現(xiàn) 此信息(不是網(wǎng)絡服務器不通) 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技術專欄

更多信息請查看技術文章
易賢網(wǎng)手機網(wǎng)站地址:制作iPhone的SOAP應用
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權所有:易賢網(wǎng)