制作iphone的soap應(yīng)用
來源:易賢網(wǎng) 閱讀:777 次 日期:2015-02-03 14:32:25
溫馨提示:易賢網(wǎng)小編為您整理了“制作iphone的soap應(yīng)用”,方便廣大網(wǎng)友查閱!

為 了便于理解,我先講下soap的大體原理:我們?cè)趇phone封裝soap請(qǐng)求信息,發(fā)送到某個(gè)提供soap服務(wù)的服務(wù)器,如下例中我們用到的.服 務(wù)器能接受和識(shí)別soap請(qǐng)求,當(dāng)它接到請(qǐng)求,就根據(jù)客戶端的請(qǐng)求情況調(diào)用服務(wù)器上的某個(gè)函數(shù),并將函數(shù)返回結(jié)果封裝成soap反饋信息發(fā)送給客戶端.客 戶端接收到soap反饋信息后,進(jìn)行解析處理,以用戶能理解的形式呈現(xiàn)給用戶.整個(gè)過程就這么簡(jiǎn)單.

好了,假設(shè)現(xiàn)在你已經(jīng)有關(guān)于 soap的基礎(chǔ)知識(shí)(沒有也沒關(guān)系,看了例子,再理解就更好理解了),下面我們開始做soap的例子.

第一步,建一個(gè) hello_soap項(xiàng)目.用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中將兩個(gè)輸出口和一個(gè)動(dòng)作連接好,這個(gè)不用手把手吧?

在 hello_soapviewcontroller.m文件中加入以下方法 :

代碼如下:

1. - (void)getoffesetutctimesoap 2. { 3. recordresults = no; 4.//封裝soap請(qǐng)求消息 5. nsstring *soapmessage = [nsstring stringwithformat: 6. @n 7. n 8. n 9. n 10. 11. n 12. n 13. n,nameinput.text 14. ]; 15. nslog(soapmessage); 16. //請(qǐng)求發(fā)送到的路徑 17. nsurl *url = [nsurl urlwithstring:@]; 18. nsmutableurlrequest *therequest = [nsmutableurlrequest requestwithurl:url]; 19. nsstring *msglength = [nsstring stringwithformat:@%d, [soapmessage length]]; 20. 21. // 以下對(duì)請(qǐng)求信息添加屬性前四句是必有的,第五句是soap信息。 22. [therequest addvalue: @text/xml; charset=utf-8 forhttpheaderfield:@content-type]; 23. [therequest addvalue: @ forhttpheaderfield:@soapaction]; 24. 25. [therequest addvalue: msglength forhttpheaderfield:@content-length]; 26. [therequest sethttpmethod:@post]; 27. [therequest sethttpbody: [soapmessage datausingencoding:nsutf8stringencoding]]; 28. 29. // 請(qǐng)求 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. }

這個(gè)方法作用就是封裝soap請(qǐng)求,并向服務(wù)器發(fā)送請(qǐng)求.

代碼有注釋.不重復(fù)講解.soap并不難,難的是沒有案例告訴我們?cè)趺窗哑渌脚_(tái)的 soap移植過來,這里我給出了代碼,我相信對(duì)iphone開發(fā)人員的話應(yīng)該能看懂了.我在下面會(huì)把此案例的源代碼附上.如果自己做不出來再看我的代碼. 如果我這樣講您覺得不夠細(xì),那說明您的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. }

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看技術(shù)文章
易賢網(wǎng)手機(jī)網(wǎng)站地址:制作iphone的soap應(yīng)用
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)