1 / 37

EEC-492/693/793 iPhone Application Development

Learn about XML, JSON, HTTP protocol, web services, and RESTful APIs for iPhone application development. Explore examples and build apps using SimpleTwitterClient and HolidayDatesServiceClient.

stahlm
Download Presentation

EEC-492/693/793 iPhone Application Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEC-492/693/793iPhone Application Development Lecture 20 Wenbing Zhao & Nigamanth Sridhar 12/19/2019 1 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development

  2. Outline • No class this Wednesday (Thanksgiving Eve) • XML & JSON • HTTP • Web services • RESTful with XML & JSON • SOAP • Demos • Assignment: • Build the SimpleTwitterClient and HolidayDatesServiceClient apps 12/19/2019 2 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development

  3. What is XML? XML is a text-based Extensible Markup Language Similar to HTML, an XML file consists of tags, i.e., identifiers enclosed in angle brackets <…> XML tags identify data only, it does not specify how to display it (start) tag <message> <to>you@yourAddress.com</to> <from>me@myAddress.com</from> <subject>XML Is Really Cool</subject> <text> How many ways is XML cool? ...</text> </message> (end) tag EEC492/693/793 - iPhone Application Development

  4. XML Syntax Element: A unit of XML data, delimited by tags An XML element can enclose other elements Attribute: A tag can contain attributes. An attribute is A qualifier on an XML tag that provides additional information For example, in the tag <slide title="My Slide">, title is an attribute, and My Slide is its value <message to="you@yourAddress.com" from="me@myAddress.com"subject="XML Is Really Cool"> <text> How many ways is XML cool? ... </text> </message> Element EEC492/693/793 - iPhone Application Development

  5. XML Syntax Empty tag: an element with no data value <flag/> is equivalent to <flag></flag> An XML document must be well-formed, i.e. for each tag, there must be a corresponding end tag, unless it is an empty tag Comment: same as HTML comment <!-- This is a comment --> Prolog: each XML document must have a prolog preceding XML data. A prolog contains a declaration and an optional DTD <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> EEC492/693/793 - iPhone Application Development

  6. XML Syntax Document Type Definition (DTD): specifies The kinds of tags that can be included in the XML document, and The valid arrangements of those tags There are many alternatives to DTD, such as XML Schema EEC492/693/793 - iPhone Application Development

  7. Processing XML Document An XML document can be accessed either serially or in random access mode Simple API for XML (SAX) Serial access and event driven NSXMLParser available for iPhone Document Object Model (DOM) Convert XML document into a collection of objects, allows random access and modification libxml2 available for iPhone EEC492/693/793 - iPhone Application Development

  8. JavaScript Object Notation (JSON) • More lightweight than XML • Looks a lot like a property list • Arrays, dictionaries, strings, numbers • Open source json-framework wrapper for Objective-C • http://code.google.com/p/json-framework/ EEC492/693/793 - iPhone Application Development

  9. What does a JSON object look like? { “instructor” : “Josh Shaffer”, “students” : 60, “itunes-u” : true, “midterm-exam” : null, “assignments” : [ “WhatATool”, “HelloPoly”, “Presence” ] } • An object is wrapped in {} curly braces => NSDictionary • An array is wrapped in [] brackets => NSArray • You must learn and discover the format of the JSON data from a Web service EEC492/693/793 - iPhone Application Development

  10. HTTP Protocol HTTP communicates over TCP/IP An HTTP client connects to an HTTP server using TCP After establishing a connection, the client can send an HTTP request message to the server: POST /item HTTP/1.1 Host: 137.148.142.239 Content-Type: text/plain Content-Length: 200 EEC492/693/793 - iPhone Application Development

  11. HTTP Protocol The server then processes the request and sends an HTTP response back to the client The response contains a status code that indicates the status of the request: In the example above, the server returned a status code of 200. This is the standard success code for HTTP If the server could not decode the request, it could have returned something like this: 200 OK Content-Type: text/plain Content-Length: 200 400 Bad Request Content-Length: 0 EEC492/693/793 - iPhone Application Development

  12. What is a RESTful Web Service? • REpresentational State Transfer • A RESTful Web service exposes its data as Universal Resource Identifiers (URIs), and allows users to operate on them • Operations • GET, POST, PUT, DELETE • Many Web services are exposed via RESTful interfaces with XML or JSON • Twitter API • E.g., http://search.twitter.com/search.json?q=@dell” • Flickr API EEC492/693/793 - iPhone Application Development

  13. Using json-framework • Download json-framework source • Add the source files in the Classes folder to your project • #import “JSON.h” in your source file (e.g., view controller) • Add an NSMutableData property to receive response from the server • Create an NSURLRequest object • NSURL = [NSURL URLWithString:@”your_url”]; • NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url]; • Create an NSURLConnection object, init it with the request object and set self as the delegate • NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; EEC492/693/793 - iPhone Application Development

  14. Using json-framework • In the connection delegate, implement the following methods • connection:didReceiveResponse: • Prepare for receiving data • connection: didReceiveData: • Append received data in NSMutableData • connectionDidFinishLoading: • Done receiving, start parsing data • NSString *response = [[NSString alloc] initWithData:dataStream encoding:NSUTF8StringEncoding]; • NSDictionary *dict = [response JSONValue]; • … • connection:didFailWithError: EEC492/693/793 - iPhone Application Development

  15. What is SOAP? SOAP is a communication protocol designed specifically to enable applications to communicate over Internet, similar to a procedure call SOAP runs on top of HTTP and other protocols such as SMTP and FTP EEC492/693/793 - iPhone Application Development

  16. SOAP Building Blocks A SOAP message is an XML document containing the following elements: A required Envelope element that identifies the XML document as a SOAP message An optional Header element that contains header information A required Body element that contains call and response information An optional Fault element that provides information about errors that occurred while processing the message SOAP Envelope SOAPheader Header Block Body Block SOAPBody EEC492/693/793 - iPhone Application Development

  17. SOAP Syntax Rules A SOAP message MUST be encoded using XML MUST use the SOAP Envelope namespace MUST use the SOAP Encoding namespace Must NOT contain a DTD reference Must NOT contain XML Processing Instructions EEC492/693/793 - iPhone Application Development

  18. Skeleton SOAP Message <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope“ soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> … … </soap:Header> <soap:Body> ... ... <soap:Fault> ... ... </soap:Fault> </soap:Body> </soap:Envelope> EEC492/693/793 - iPhone Application Development

  19. SOAP Envelope Element It is required and it is the root element of a SOAP message It defines the XML document as a SOAP message It must specify the mandatory namespace <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope“ soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding"> … Message information goes here … </soap:Envelope> • The SOAP encodingStyle attribute is used to define the data types used in the document EEC492/693/793 - iPhone Application Development

  20. SOAP Namespace Most elements in a soap message are required to be namespace-qualified in the form ns:tag You need to define the namespace only once in a message <soap:Envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope> namespace Namespace definition (SOAP default namespace) <soapenv:Envelopexmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope> It doesn’t matter what name you use EEC492/693/793 - iPhone Application Development

  21. SOAP Header Element The SOAP Header element is optional The header element contains application specific information about the SOAP message E.g. authentication, coordination, etc. If the Header element is present, it must be the first child element of the Envelope element EEC492/693/793 - iPhone Application Development

  22. SOAP Body Element It is required It contains actual SOAP message intended for the ultimate endpoint of the message Immediate child elements of the SOAP Body element may be namespace-qualified SOAP defines the Fault element inside the Body element in the default namespace EEC492/693/793 - iPhone Application Development

  23. SOAP Fault Element The Fault element (if present) carries an error message If present, it is embedded in the Body element as a child element A Fault element can only appear once in a SOAP message EEC492/693/793 - iPhone Application Development

  24. SOAP Fault Element The SOAP Fault element has the following sub elements EEC492/693/793 - iPhone Application Development

  25. SOAP Fault Element SOAP Fault Codes EEC492/693/793 - iPhone Application Development

  26. SOAP HTTP Binding A SOAP method call is an HTTP request/response that complies with SOAP encoding rules HTTP + XML = SOAP A SOAP request could be an HTTP POST or an HTTP GET request The HTTP POST request specifies at least two HTTP headers: Content-Type and Content-Length HTTP POST SOAP Envelope SOAPheader Transactional context SOAPBody Name of Procedure Input parameter 1 Input parameter 2 EEC492/693/793 - iPhone Application Development

  27. SOAP HTTP Binding Content-Type header: defines the MIME type for the SOAP message and the character encoding (optional) used for the XML body of the request or response Syntax: Example: Content-Type: MIMEType; charset=character-encoding POST /item HTTP/1.1 Content-Type: application/soap+xml; charset=utf-8 MIME : Multipurpose Internet Mail Extensions EEC492/693/793 - iPhone Application Development

  28. SOAP HTTP Binding Content-Length header: specifies the number of bytes in the body of the request or response Syntax: Example: Content-Length: bytes POST /item HTTP/1.1 Content-Type: application/soap+xml; charset=utf-8 Content-Length: 250 EEC492/693/793 - iPhone Application Development

  29. What is WSDL? WSDL stands for Web Services Description Language WSDL is written in XML WSDL is an XML document WSDL is used to describe Web services WSDL is also used to locate Web services EEC492/693/793 - iPhone Application Development

  30. WSDL Document Structure A WSDL document describes a web service using these major elements: Service Port (e.g. http://host/svc) Port Binding (e.g. SOAP) Binding Abstract interface portType operation(s) inMesage outMessage EEC492/693/793 - iPhone Application Development

  31. WSDL Ports The <portType> element describes a web service The operations that can be performed, and The messages that are involved It can be compared to a function library (or a class) in a traditional programming language <portType name="GetQuote"> <operation name="getQuote" > <input message="tns:GetQuoteRequest"/> <output message="tns:GetQuoteResponse"/> </operation> </portType> EEC492/693/793 - iPhone Application Development

  32. WSDL Messages The <message> element defines the data elements of an operation Each message can consist of one or more parts The parts can be compared to the parameters of a function call in a traditional programming language <message name="GetQuoteRequest"> <part name="symbol" type="xsd:string"/> </message> <message name="GetQuoteResponse"> <part name="result" type="xsd:float"/> </message> EEC492/693/793 - iPhone Application Development

  33. WSDL Types The <types> element defines the data type used by the web service For maximum platform neutrality, WSDL uses XML Schema syntax to define data types <wsdl:types> <xs:schema> <xs:import namespace='http://schemas.xmlsoap.org/ws/2004/08/addressing' schemaLocation='addr.xsd' /> </xs:schema> </wsdl:types> EEC492/693/793 - iPhone Application Development

  34. Public SAP Web Services • http://www.service-repository.com • USHolidayDates • Spelling check • Weather service • Temperature covnerter • … EEC492/693/793 - iPhone Application Development

  35. wsdl2objc • wsdl2obj • Convert the published WSDL file to Objective-C files • Uses libxml2 (DOM-based parsing) • Still working-in-progress, won’t work with some Web services • Not as ease-to-use as JSON, but so much better than handling raw XML youself! • Download wsdl2obj source and build the tool svn checkout http://wsdl2objc.googlecode.com/svn/trunk/ wsdl2objc EEC492/693/793 - iPhone Application Development

  36. Using wsdl2objc • Find a SOAP web service, download its WSDL file • Run the wsdl2objc tool, specify the WSDL file and the output directory • Add all the auto-generated files to your project • Add "-lxml2" to the "Other Linker Flags" property • Project -> Edit Project Settings -> Build Tab -> Under the Linking section, there is field called Other Linker Flags • Add "/usr/include/libxml2" to "Header Search Path" variable of Build Rules • Project -> Edit Active Target -> Build Tab -> Under the Search Paths section, there is Header Search Paths (while adding the path, make it recursive) • Add CFNetwork.framework to the "Frameworks" EEC492/693/793 - iPhone Application Development

  37. Using wsdl2objc • In the source file that will consume the Web service • #import “target_web_service.h” • Create a binding • Create a request object • Getting the response • Parse the response EEC492/693/793 - iPhone Application Development

More Related