Making the iphone Soap app

Source: Internet
Author: User
Tags soap

For the sake of understanding, let me start with the general principle of soap: We encapsulate the SOAP request information on the iphone and send it to a server that provides SOAP services, the http://www we use in the following example. nanonull.com/timeservice/. The server can accept and recognize a SOAP request, and when it receives a request, it invokes a function on the server based on the client's request, and encapsulates the result of the function return to the client with a SOAP feedback message. Guest When the client receives the SOAP feedback, it is parsed and presented to the user in a form that the user can understand. The whole process is so simple.

OK, now that you have the basics of soap (no, it doesn't matter, look at the examples and understand better), let's start with a soap example.

The first step is to build a Hello_soap project. Using the IB to make the Hello_soapviewcontroller.xib an interface of the following diagram

Then add the following code in the Hello_soapviewcontroller.h

Code

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. @property (nonatomic, retain) Iboutlet Uitextfield *nameinput; @property (nonatomic, retain) Iboutlet Uilabel *greeting; 14.15. @property (nonatomic, retain) Nsmutabledata *webdata; @property (nonatomic, retain) nsmutablestring *soapresults; @property (nonatomic, retain) Nsxmlparser *xmlparser; 18.19. -(Ibaction) ButtonClick: (ID) sender; Getoffesetutctimesoap-(void);

Then in the hello_soapviewcontroller.xib will be two exports and one action link good, this does not have the hand?

Add the following methods to the HELLO_SOAPVIEWCONTROLLER.M file:

Code

1.-(void) Getoffesetutctimesoap 2. {3. recordresults = NO; 4.//encapsulates SOAP request message 5. NSString *soapmessage = [NSString stringwithformat:6. @ "n" 7. "N" 8. "N" 9. "N" 10. "%@n" 11. "N" 12. "N" 13. "N", Nameinput.text 14. ]; NSLog (SoapMessage); 16.//Request sent to the path 17. Nsurl *url = [Nsurl urlwithstring:@ "Http://www.nanonull.com/TimeService/TimeService.asmx"]; Nsmutableurlrequest *therequest = [Nsmutableurlrequest Requestwithurl:url]; NSString *msglength = [NSString stringwithformat:@ "%d", [soapmessage length]]; 20.21. The following adds attributes to the request information, the first four sentences are mandatory, and the fifth sentence is soap information. [Therequest AddValue: @ "text/xml; charset=utf-8" forhttpheaderfield:@ "Content-type"]; [Therequest AddValue: @ "http://www.Nanonull.com/TimeService/getOffesetUTCTime" forhttpheaderfield:@ "SOAPAction"]; 24.25. [Therequest addvalue:msglength forhttpheaderfield:@ "Content-length"]; [Therequest sethttpmethod:@ "POST"]; [Therequest sethttpbody: [SoapMessage datausingencoding:nsutf8stringencoding]]; 28.29. Request 30. Nsurlconnection *theconnection = [[Nsurlconnection alloc] initwithrequest:therequest delegate:self]; 31.32. If the connection is already built, initialize data 33. if (theconnection) 34. {WebData = [[Nsmutabledata data] retain]; 36.} 37. Else 38. {NSLog (@ "theconnection is NULL"); 40.} 41. 42.43. }

The purpose of this method is to encapsulate the SOAP request and send the request to the server.

The code has comments. Do not repeat. Soap is not difficult, the hard part is that no case tells us how to transplant the soap from another platform, I give the code here, and I'm sure the iphone developer should be able to read it. I'll enclose the source code for this case below. If I can't do it, I'll look at my code. . If I say that you don't think it's fine enough, your iphone development isn't going too far, so you shouldn't be using SOAP technology.

The following code is to receive information and parse it, and display it to the user interface

Code

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.//If the computer is not connected to the network, this information (not the network server) will appear 14. -(void) connection: (Nsurlconnection *) connection didfailwitherror: (nserror *) error 15. {NSLog (@ "ERROR with Theconenction"); [Connection release]; [WebData release]; 19.} 20. -(void) connectiondidfinishloading: (nsurlconnection *) connection 21. {NSLog (@ "3 done.) Received Bytes:%d ", [WebData length]); NSString *thexml = [[NSString alloc] initwithbytes: [webdata mutablebytes] length:[webdata length] Encoding:nsutf8str Ingencoding]; NSLog (Thexml); [Thexml release]; 26.27. Re-add Lou Xmlparser 28. if (xmlparser) 29. [Xmlparser release]; 31.} 32. Xmlparser = [[NsxmlpaRser alloc] initwithdata:webdata]; [Xmlparser setdelegate:self]; [Xmlparser Setshouldresolveexternalentities:yes]; [Xmlparser parse]; 37.38. [Connection release]; //[webdata release];

.}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.