Incorporating Web Services in Mobile Applications


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Abstract Most of the APIs available to developers today have been coded for robust web server integration with little thought of incorporation into light weight mobile applications. This talk will look at the pitfalls of using these APIs directly and methods of incorporating APIs, such as Amazon, eBay, Google and other API sets into mobile and lightweight applications, while maintaining a quality user experience. First we will review the challenges of incorporating these APIs including; * Retrieval of large data sets * Multiple round trip communications * Security issues of calls * Display of information For each of these challenges we will show specific examples with sample functionality, API flows, and XML blocks. Some examples will include web user authentication techniques, media retrieval lists, and interface usability issues. Once we understand the challenges of incorporating various web APIs we will then look at techniques for handling APIs properly including caching methods, large data set handling, paging, filtering, just in time techniques, information on demand and speed testing. Throughout we will look at pseudo code, and detailed examples of real life examples. With the proper techniques mobile applications can take advantage of a wide array of third party and home grown APIs without degradation of performance, memory, and overall usability.
  • The iPhone now accounts for 50 percent of mobile Web traffic from smartphones in the U.S., according to an AdMob Mobile Metrics report released this morning. Over the past six months, the iPhone has taken share from Blackberry and Windows Mobile. In August 2008, the iPhone made up only 10 percent of mobile Web traffic from smartphones. During the same time, Blackberry’s share has gone from 32 percent to 21 percent (with the Curve and the Pearl coming in stronger than the Storm), while Windows Mobile has taken an even bigger hit, declining from 30 percent to 13 percent. Palm is also down to 7 percent from 19 percent six months ago. The only other smartphone operating system that is showing gains in mobile Web usage is Android, which has captured a strong 5 percent share just three months after launch. And that is up from 3 percent in January. The gains shown by the iPhone and Android show what is possible when phones are built with fully capable browsers and support a rich array of Web apps. iPhone Makes Up 50 Percent of Smartphone Web Traffic In U.S., Android Already 5 Percent 31 Comments by Erick Schonfeld on March 24, 2009 – Techcrunch
  • Focused on: Lessons learned from specific case studies Bet practices reinforcing ideas of web services in browser based apps Will cover the business side and then technology / code
  • Is there a fee? What is the timeframe? What resources are required? Do you need to furnish equipment?
  • One note here – even though the object based XML parser available with Cocoa runs in the iphone simulator it is not included as part of the iphone SDK and thus is unavailable on the iphone
  • Caching of the processed results to serve same client or other clients – in memory or database Can help with reducing call volume to WS service providers Especially helpful with statistical information which changes on a periodic basis Must be careful with storage and refresh rules Banking and financial information perfect example of this FRED – US Federal Reserve Economic Data
  • Remember you are the consumer of the api – play nice – the owner of the api can always turn your access off “ Starting later this week we’ll be limiting those on the whitelist to 20,000 requests per hour. Yes, you read that right: twenty THOUSAND requests per hour. According to our logs, this accounts for all but the very largest consumers of our API. This is essentially a preventative measure to ensure that no one API client, even a whitelisted account or IP, can consume an inordinate amount of our resoures.” 1/21/2009 We will see more of this – our duties as coders to play nicely with the api – use the right API
  • Incorporating Web Services in Mobile Applications

    1. 1. Incorporating Web Services in Mobile Applications <ul><li>April 3, 2009 – San Francisco </li></ul>
    2. 2. Slides available at
    3. 3. Disclaimer <ul><ul><li>Web services and APIs of third parties referenced in this presentation are owned by the third parties and governed by their agreements. Before using any of the APIs or code referenced in this presentation review the terms of use by the third party providing the API. </li></ul></ul><ul><ul><li>These slides are available under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License. You can blog, photograph, and share this information with others. </li></ul></ul><ul><ul><li>Trademarks and brands are the property of their respective owners. </li></ul></ul>
    4. 4. Introduction - Convergence <ul><li>Available APIs are growing daily </li></ul><ul><ul><li>Currently 1229 (and growing) APIs registered at Programmable Web </li></ul></ul><ul><li>Shifts in Consumer Expectations </li></ul><ul><ul><li>Consumers now expect mobile devices to be connected to Internet Sites and Services </li></ul></ul><ul><li>Platform Development becoming easier and more standardized </li></ul><ul><ul><li>iPhone SDK – Greater than 50,000 registered developers </li></ul></ul><ul><ul><li>Android – 5% of smart phone web usage in just three months 1 </li></ul></ul><ul><li>Seeing extension of enterprise applications to mobile devices </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li>1 Techcrunch, March 24, 2009, Erick Schonfeld </li></ul></ul>
    5. 5. Introduction <ul><li>Many Web Services and APIs were originally developed with server to server or server to browser in mind, not mobile applications </li></ul><ul><li>Mobile platforms have their own set of challenges given: </li></ul><ul><ul><li>Bandwidth </li></ul></ul><ul><ul><li>Memory and CPU Availability </li></ul></ul><ul><ul><li>Storage Capacity </li></ul></ul><ul><ul><li>Connectivity Options and Issues </li></ul></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>User Interaction and Display </li></ul></ul>Web Service
    6. 6. Mobile Integration Challenges
    7. 7. API Developer Programs <ul><li>Is mobile access allowed? </li></ul><ul><li>Other considerations: </li></ul><ul><ul><li>Call Limitations (# per second, total per hour/day, pricing above) </li></ul></ul><ul><ul><li>Caching and Storage of Data </li></ul></ul><ul><ul><ul><li>Persistence of Data – Length of time stored </li></ul></ul></ul><ul><ul><ul><li>Freshness of Data – Length of time before refreshing </li></ul></ul></ul>
    8. 8. <ul><li>Persistence Limitations </li></ul><ul><li>Refreshing Requirements </li></ul>Caching and Storage Limits
    9. 9. <ul><li>Certification of your Mobile Application </li></ul><ul><li>Some API sets require certification on top of distributor certification </li></ul><ul><ul><li>Determine Costs / Timeframe / Effort given a mobile app </li></ul></ul><ul><ul><li>Prepare for testing – typically looking for error handling, API abuse </li></ul></ul><ul><li>Certification is a positive! </li></ul><ul><ul><li>Credibility, validation, marketing </li></ul></ul><ul><ul><li>Keeps the neighborhood safe </li></ul></ul>Application Verification
    10. 10. Mobile Techniques
    11. 11. Authentication vs. Authorization <ul><li>The Difference </li></ul><ul><ul><li>Authentication from the API provider – API Key </li></ul></ul><ul><ul><li>Authorization from the user – authToken </li></ul></ul><ul><li>Session Key </li></ul><ul><ul><li>By providing combination of API Key and authToken can then receive the session key </li></ul></ul><ul><ul><li>What is the shelf life of the session key </li></ul></ul><ul><li>Authorization will commonly affect user’s experience on mobile </li></ul><ul><ul><ul><li>eBay authentication and authorization screens </li></ul></ul></ul>
    12. 12. - Sample Flow <ul><li> </li></ul><ul><li>Get an API key </li></ul><ul><li>Request auth – auth.getMobileSession </li></ul><ul><ul><li>Inputs: </li></ul></ul><ul><ul><ul><li>username </li></ul></ul></ul><ul><ul><ul><li>authToken (md5(username + md5(password))) </li></ul></ul></ul><ul><ul><ul><li>api_key </li></ul></ul></ul><ul><ul><ul><li>api_sig </li></ul></ul></ul><ul><li>Returns session key </li></ul><ul><li>Make API call with sk (session key) </li></ul><ul><li>Note: Try to keep credentials in one area to facilitate later changes </li></ul>
    13. 13. auth.getmobilesession <ul><li>auth.getmobilesession </li></ul><ul><li>Result with new session key </li></ul>…&username=chudsonfm&authToken=4eb71…&api_sig=e93fb… <lfm status=&quot;ok”> <session> <name>chudsonfm</name> <key>142c0085138a4b72d1782215b1da770a</key> <subscriber>0</subscriber> </session> </lfm> username (Required) : The username. authToken (Required) : A 32-byte ASCII hexadecimal MD5 hash of the user's credentials. api_key (Required) : A API key. api_sig (Required) : A method signature.
    14. 14. – Call Execution <ul><li>Now we can make the call with our sk (session key) – user.getinfo </li></ul><ul><li>Response </li></ul>…&sk=142c0…&api_sig=ccbd1…cc9dd <ul><li><lfm status=&quot;ok”> </li></ul><ul><li><user> </li></ul><ul><li><id>17317105</id> </li></ul><ul><ul><ul><ul><ul><li><name>chudsonfm</name> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><realname/> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><url></url> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><image/> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><lang>en</lang> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><country/> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><age/> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><gender>n</gender> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><subscriber>0</subscriber> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><playcount>0</playcount> </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li><playlists>0</playlists> </li></ul></ul></ul></ul></ul><ul><li></user> </li></ul><ul><li></lfm> </li></ul>sk (Required) : The session key returned by auth.getMobileSession service. api_key (Required) : Your 32-character API key. api_sig (Required) : Your API method signature
    15. 15. Speed Traps <ul><li>Could be on wifi, 3G or 1G based signal and bandwidth </li></ul><ul><li>Segment functionality / calls to prevent issues </li></ul><ul><ul><li>Standard Apple test for approval is bandwidth usage when on cell network </li></ul></ul><ul><li>Polling </li></ul><ul><ul><li>Create subset of calls and functionality when on slower bandwidth </li></ul></ul><ul><ul><li>Use notifications if possible </li></ul></ul><ul><ul><li>Modify polling interval based on need </li></ul></ul><ul><li>Recovery from intermittent or lost connections </li></ul><ul><li>Test the use of APIs via public vendor API test tools on various bandwidths </li></ul>
    16. 16. Large Data Set Handling <ul><li>Decreasing returns for XML with large data sets and repetitive XML sections </li></ul><ul><li>Even more the case with mobile devices given: </li></ul><ul><ul><li>Communication challenges </li></ul></ul><ul><ul><li>Memory and processing limitations </li></ul></ul><ul><li>Minimize large data sets: </li></ul><ul><ul><li>Use Just-in-Time (JIT) methodology </li></ul></ul><ul><ul><li>Ask for only those elements that you require </li></ul></ul><ul><ul><li>Ask for only those items that you require </li></ul></ul><ul><ul><li>Store what you can locally instead of requesting the same data </li></ul></ul>
    17. 17. Response Processing <ul><li>Leverage Stream Parsing for mobile environments </li></ul><ul><ul><li>Provides low memory overhead </li></ul></ul><ul><ul><li>But, requires more heavy lifting </li></ul></ul><ul><li>iPhone SDK based on stream parsing – NSXMLParser class </li></ul><ul><li>Android has Xstream as the streaming parser </li></ul>
    18. 18. iPhone Parsing <ul><li>NSXMLParser delegate methods </li></ul><ul><ul><li>5 key methods </li></ul></ul><ul><ul><ul><li>Parser:didStartElement: </li></ul></ul></ul><ul><ul><ul><li>Parser:foundCharacters: </li></ul></ul></ul><ul><ul><ul><li>Parser:didEndElement: </li></ul></ul></ul><ul><ul><ul><li>Parser:didEndDocument: </li></ul></ul></ul><ul><ul><ul><li>Parser:parseErrorOccurred: </li></ul></ul></ul>Method Purpose parser:didStartElement: Start of element tag occurred with attributes parser:foundCharacters: Element data located which may be all or some parser:didEndElement: End of element tag located parser:didEndDocument: End of XML document occurred parser:parseErrorOccurred: Unrecoverable parsing error occurred
    19. 19. <ul><li>Typical didStartElement, foundCharacters, and didEndElement </li></ul>
    20. 20. Parsing <ul><li>In long parsing streams provide feedback when available </li></ul><ul><li>Example: Control4 My House </li></ul><ul><ul><li>Home control from your iTouch/iPhone </li></ul></ul><ul><ul><li>During initial project load describes the sections being processed </li></ul></ul><ul><ul><li>Stores those items that will not change including house devices </li></ul></ul><ul><ul><li>But when providing current data, requests from server current state </li></ul></ul><ul><ul><li>Blends stored information with real time status information to provide display </li></ul></ul>
    21. 21. Filtering <ul><ul><li>Types of Request Filtering </li></ul></ul><ul><ul><ul><li>Selection criteria for narrowing data set returned </li></ul></ul></ul><ul><ul><li>In general determine how efficient are the calls – reduce data waste </li></ul></ul><ul><ul><ul><li>How much extra data, streaming, parsing is happening to access the data you use? How much data is being dropped on the floor? </li></ul></ul></ul><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <FindItemsAdvancedRequest xmlns=&quot;urn:ebay:apis:eBLBaseComponents&quot;> <!-- Standard Input Fields --> <MessageID> string </MessageID> <!-- Call-specific Input Fields --> <BidCountMax> int </BidCountMax> <BidCountMin> int </BidCountMin> <CategoryHistogramMaxChildren> int </CategoryHistogramMaxChildren> <CategoryHistogramMaxParents> int </CategoryHistogramMaxParents> <CategoryID> string </CategoryID> <CharityID> int </CharityID> <Condition> ItemConditionCodeType </Condition> <Currency> CurrencyCodeType </Currency> … <ShippingLocation> CountryCodeType </ShippingLocation> <ShippingPostalCode> string </ShippingPostalCode> <SortOrder> SortOrderCodeType </SortOrder> <StoreName> string </StoreName> <StoreSearch> StoreSearchCodeType </StoreSearch> </FindItemsAdvancedRequest>
    22. 22. Paging <ul><li>Provide paging functionality to users </li></ul><ul><li>Not all calls have paging available </li></ul><ul><li>Inform user of current location in set, easy access to next and previous if applicable </li></ul><ul><li>Cache key information depending on </li></ul><ul><ul><li>The user flow (detail page from results page) </li></ul></ul><ul><ul><li>The aging of the information (when does it expire) </li></ul></ul>
    23. 23. Page Requests <ul><li>Specify the page that you are requesting </li></ul><ul><li><PageNumber> </li></ul><ul><li>How many items are on a page </li></ul><ul><ul><li>Set the number of entries per page returned </li></ul></ul><ul><li><MaxEntries> </li></ul><ul><li> </li></ul>
    24. 24. List Paging Example <ul><li>Must cache of course the current page </li></ul><ul><li>Remember though the total pages can change dynamically so check total pages after subsequent requests </li></ul><ul><li><TotalItems> int </TotalItems> </li></ul><ul><li><TotalPages> int </TotalPages> </li></ul>
    25. 25. Display <ul><li>Caching of information for future retrieval and navigation </li></ul><ul><ul><li>Be careful of data aging depending on what is being shown </li></ul></ul><ul><li>Execute web service parsing and display in background when possible to prevent UI blocking </li></ul><ul><ul><li>example – eBay item description and images </li></ul></ul><ul><li>When parsing large data blocks, progress indications can be provided from element name (sections of the XML) </li></ul><ul><li>Synchronous versus Asynchronous calls </li></ul><ul><li>Use JIT and Information on Demand to maximize small layouts and minimize web service calls </li></ul>
    26. 26. Caching Options <ul><li>To minimize API round trips leverage caching and storage </li></ul><ul><ul><li>Cache in memory </li></ul></ul><ul><ul><li>Cache in onboard light weight database or file </li></ul></ul><ul><ul><li>Off device persistence </li></ul></ul>
    27. 27. Expanding Your Options
    28. 28. Blending Multiple Results <ul><li>What if you have to: </li></ul><ul><ul><li>Blend results from multiple calls in the same API </li></ul></ul><ul><ul><li>Blend results from multiple API sources </li></ul></ul><ul><li>This could take the form of: </li></ul><ul><ul><li>Extra item information on top of item details call </li></ul></ul><ul><li>Note this is simplified so does not show authentication and authorization checking, encoding, or the like. </li></ul>API Service Mobile View Trigger Request 1 Mobile App Request 2 Result 1 Fulfill Fulfill Result 2 Blend Outlet
    29. 29. Blending Multiple Services <ul><li>Multiple Services </li></ul><ul><ul><li>Dependent on multiple services </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li>Weather request from additional WS based on zip code returned by latitude and longitude from a geoposition service </li></ul></ul>API Service 1 Mobile View Trigger Request 1 Mobile App Request 2 Result 1 Fulfill Result 2 Blend Outlet API Service 2 Fulfill
    30. 30. Proxy Server Solution <ul><li>Provides layer of abstraction to consolidate or manage web services </li></ul><ul><li>Proxy Server retrieves data on demand or on scheduled basis </li></ul><ul><li>Allows for single consolidated call from the mobile device </li></ul><ul><li>Processes data and returns subset or stores data for retrieval later </li></ul><ul><li>Can also reduce API calls by caching similar results across user </li></ul><ul><li>Allows for extending business logic layer </li></ul>
    31. 31. Proxy Flow <ul><li>Provides for single round trip for mobile application </li></ul><ul><li>Provides easy maintenance of web services business logic layer </li></ul><ul><li>Easily modify </li></ul><ul><ul><li>Business logic </li></ul></ul><ul><ul><li>API credentials </li></ul></ul>Proxy Server Mobile View Trigger Request Mobile App Request Result Result Outlet API Services Fulfill Fulfill Request Result
    32. 32. Proxy Landscape Web Service Web Service Proxy Server
    33. 33. Designing your WS/API Destiny
    34. 34. Creating Web Services <ul><li>Try to serve lowest common device </li></ul><ul><li>Balance flexibility with overhead in calls: </li></ul><ul><ul><li>Filtering of criteria and paging of data functionality </li></ul></ul><ul><ul><li>Split out high traffic calls versus critical requests </li></ul></ul><ul><ul><li>Easy but secure authentication and authorization for both the consumer and application </li></ul></ul><ul><li>Benefit for mobile consumers, application programmers and web service providers </li></ul><ul><ul><li>Decreased round trips </li></ul></ul><ul><ul><li>Increased efficiency of calls and applications </li></ul></ul><ul><ul><li>Better use of call volume restrictions </li></ul></ul><ul><ul><li>User experience improves </li></ul></ul>
    35. 35. Wrap Up <ul><li>Provide user preference options that translate into better tuned API calls </li></ul><ul><ul><li>- number of items to show on a page as an example </li></ul></ul><ul><ul><li>This will translate to data efficiency, fewer callers and greater use of data retrieved </li></ul></ul><ul><ul><li>Minimize the number of trips, combine in single calls when possible </li></ul></ul><ul><ul><li>Leverage caching and data storage when allowed </li></ul></ul><ul><ul><li>Balance data granularity with length of returns for parsing </li></ul></ul><ul><li>Final thought to ponder </li></ul><ul><li>The mobile device that is typically serving as a web service client could in the now or future be a web service provider… </li></ul>
    36. 36. Thank you Slides available at: <ul><li>Chuck Hudson </li></ul><ul><li>[email_address] </li></ul>aduci
    37. 37. Appendix A - Resources <ul><li>API / Web Service Online Catalogues </li></ul><ul><ul><li>Programmable Web </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><li>Web Service List </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul>
    38. 38. Appendix B – Code Resources <ul><ul><ul><li>iPhone URL Connections </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><li>iPhone Rest Wrapper </li></ul></ul></ul><ul><ul><ul><ul><li>i </li></ul></ul></ul></ul><ul><ul><ul><li>iPhone XML Parsing – Cocoa Stream Parser </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><li>SOAP with iPhone examples </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><li>Façade Server landscape – O’REILLY – Windows platform </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><li>Connecting to Web Services – Sean Sullivan </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul><ul><ul><ul><li>W3C – Mobile Web Application Best Practices </li></ul></ul></ul><ul><ul><ul><ul><li> </li></ul></ul></ul></ul>
    39. 39. Appendix C - Efficient XML Interchange (EXI) <ul><li>W3C Working Draft 19 September 2008 - </li></ul><ul><li>“EXI is a very compact representation for the Extensible Markup Language (XML) Information Set that is intended to simultaneously optimize performance and the utilization of computational resources.” </li></ul><ul><li>Components of XML Complexity </li></ul><ul><ul><ul><ul><ul><li>1. size </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>2. total number of elements </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>3. number of unique elements </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>4. tree-height (nesting) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>5. number of unique external references </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>6. total number of attributes </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>7. number of unique attributes </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>8. regularity, or &quot;self-similarity&quot; </li></ul></ul></ul></ul></ul>
    40. 40. <ul><li>Facebook </li></ul><ul><ul><li>Notice the session key expiration </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>Oauth </li></ul></ul><ul><ul><ul><li>MPOAuthMobile – compiled in project library - Karl Adam </li></ul></ul></ul><ul><ul><li> </li></ul></ul>Appendix D - Other Auth Examples