Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Retrofit framework in implementation of Android REST client David Ante Macan*, Zlatko Stapić, Milan Pavlović* University of Zagreb Faculty of Organization.

Similar presentations


Presentation on theme: "Using Retrofit framework in implementation of Android REST client David Ante Macan*, Zlatko Stapić, Milan Pavlović* University of Zagreb Faculty of Organization."— Presentation transcript:

1 Using Retrofit framework in implementation of Android REST client David Ante Macan*, Zlatko Stapić, Milan Pavlović* University of Zagreb Faculty of Organization and Informatics Pavlinska 2, 42000 Varaždin, Croatia http://www.foi.unizg.hr/ CASE27 – Developers conference - 02.06.2015. – Zagreb, Croatia * Students at University of Zagreb, FOI Varazdin

2 Agenda Introduction REST Architecture Android client implementation Implementation using Retrofit Conclusion References Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 2

3 Introduction Today’s mobile applications are Internet dependent 2017 -> 90% [1], [3] Web services are used for supporting mobile application functionalities  Internet dependent monetization models  App usage statistics, data synchronization, social components, score tables… Backend servers for push notifications Web service – self contained and self describing application component that can be used by other applications and can be communicated by using open protocols [6] Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 3

4 Introduction After SOA became widely used for mobile and other applications, the dominant way of exchanging the data with web services was by using SOAP [7] XML used for creating requests and responses was too complex and requests had to be build manually -> problematic due to protocols intolerance on any errors REST (Representational State Transfer) provides lightweight alternative and relies on simple URL approach REST can output the data in any format (for example in CSV, JSON or RSS). Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 4

5 REST Architecture  Architectural style for distributed hypermedia systems, as Roy Fielding originally described it in his doctoral dissertation in 2000 [8]  Contains 6 main characteristics: 1.Uniform interface  Resource based  Manipulation of resources through representations  Usage of self-descriptive messages  Hypermedia as the engine of application state 2.Statelessness 3.Caching 4.Client-server separation 5.Layering 6.Code on demand Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 5

6 REST Architecture  HTTP request semantics Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 6 Request MethodDescription GET Used for retrieving (or read) a representation of a resource. If everything works, HTTP GET response contains a representation in XML or JSON with response code of 200 (OK). POST Most often used for creation of new resources. It is actually used to create subordinate or child resource of some other (parent) resource. When server receives POST request, it creates new resource and assigns it a new ID. PUT Used for update operations. Request body should contain newly-updated representation of the original resource which is identified by a known resource URI. DELETE Used to delete URI-identified resource.

7 Android client implementation Example system  Address Manager - enables users to get geographical location (latitude and longitude) of any user defined address Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 7

8 Android client implementation Web service specification  http://tinyurl.com/case27-locations http://tinyurl.com/case27-locations  x-www-form-urlencoded request Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 8 URLMethodParamsResponse example baseGET- [{"id":1,"location":"Ivanec","lat": "46.2228422","lon":"16.1246793"}, …] basePOSTlocation {"id":1,"location":"Ivanec","lat": "46.2228422","lon":"16.1246793"} base/idPUTlocation{"id":1,"location":"Ivanec 2", "lat": "47.2228422","lon":"15.1246793"}

9 Android client implementation Native Android web-service consumption  web service communication layer  response parsing layer  manual threading Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 9

10 Android client implementation Native Android web-service consumption  Repository – consists of set of methods that are leaning on web services  Service – represents web-service communication layer. This class should be flexible to be able to execute any web service request.  Callback – interface to be implemented by any callback object containing logic for receiving web-service response.  Parser – static class containing knowledge on parsing the responses. Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 10

11 Android client implementation Native Repository Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 11

12 Android client implementation Retrofit implementation  type-safe REST client for Android and Java  turns your REST API into a Java interface  upon downloading the data is parsed into POJO which must be defined for each "resource" in the response  flexible in message format  uses compile-time annotation processor Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 12

13 Android client implementation Retrofit implementation  Define API interface  Define RetrofitHelper that will instantiate adapter and interface Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 13

14 Android client implementation Retrofit implementation  Call methods  Handle response Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 14

15 Conclusion We presented  REST application architectural style  Two implementations of web-service communication layer Main contribution  Architectural design for the two approaches Native vs Retrofit approach  Native approach is robust and hard to adapt  Retrofit is straightforward and flexible Source code  http://tinyurl.com/case27-code http://tinyurl.com/case27-code Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 15

16 References [1] D. Bosomworth, “Mobile marketing statistics 2015” Smart Insights, 2015. Available at: http://www.smartinsights.com/mobile-marketing/mobile-marketing-analytics/mobile-marketingstatistics/ [Accessed: May-2015] [2] ComScore Inc., “The U.S. Mobile App Report” 2014. Available at: http://www.comscore.com/Insights/Presentations-and-Whitepapers/2014/The-US-Mobile-App-Report [3] Statista, “Statistics and facts on Mobile Internet Usage,” Statista.com, 2015. [Online]. Available at: http://www.statista.com/topics/779/mobile-internet/ [Accessed: May-2015]. [4] Android Developers, “Google Cloud Messaging for Android | Android Developers” Google Services, 2015. [Online]. Available at: https://developer.android.com/google/gcm/index.html [Accessed: May-2015]. [5] Apple inc., “Local and Push Notifications for Developers - Apple Developer” Developer Apple.com, 2015. [Online]. Available at: https://developer.apple.com/notifications/ [Accessed: May-2015]. [6] W3Schools.com, “Introduction to Web Services” Introduction to Web Services, 2015. [Online]. Available at: http://www.w3schools.com/webservices/ws_intro.asp [Accessed: May-2015]. [7] W3Schools.com, “SOAP Introduction”, 2015. [Online]. Available at: http://www.w3schools.com/ webservices/ws_soap_intro.asp [Accessed: May-2015]. [8] R. T. Fielding, “Architectural Styles and the Design of Network-based Software Architectures” UNIVERSITY OF CALIFORNIA, Irvine, 2000. [9] A. Nene, “Web Services Architecture – When to Use SOAP vs REST” Javalobby, 2014. [Online]. Available at: http://java.dzone.com/articles/web-services-architecture [Accessed: May-2015]. [10] J. Mueller, “Understanding SOAP and REST Basics” Software Quality Matters Blog, 2013. [Online]. Available at: http://blog.smartbear.com/apis/understanding-soap-and-rest-basics/ [Accessed: May-2015]. [11] F. Todd, “RESTful Service Best Practices, Recommendations for Creating Web Services” RestAPITutorial.com, 2013. [12] CodePath.com, “Consuming APIs with Retrofit” CodePath Android Cliffnotes, 2015. [Online]. Available at: https://guides.codepath.com/android/Consuming-APIs-with-Retrofit#setup [Accessed: May-2015]. [13] StudyTrials, “Java Google Json (Gson) Introduction” StudyTrials.com. [Online]. Available at: http://www.studytrails.com/java/json/java-google-json-introduction.jsp [Accessed: May-2015]. [14] “Google Gson” Google Gson Home, 2011. [Online]. Available at: https://sites.google.com/ site/gson/Home [Accessed: May-2015] Macan, Stapić, Pavlović: Using Retrofit framework in implementation of Android REST client - CASE27, 2 nd June 2015 16


Download ppt "Using Retrofit framework in implementation of Android REST client David Ante Macan*, Zlatko Stapić, Milan Pavlović* University of Zagreb Faculty of Organization."

Similar presentations


Ads by Google