SlideShare a Scribd company logo
1 of 8
Download to read offline
CS425 - Web and Mobile Software Engineering
Dr. Lilia Sfaxi
1 mai 2017
Web Services
Android Lab 05


WEB SERVICES DR. LILIA SFAXI !1
MedTech
I. Retrofit
Retrofit is a REST client for Android and Java, enabling the grouping and organisation of all
1
the APIs in a simple and clean way.
Annotations are used to control the parameters or the API, defined in interfaces, called from
classes.
All the http APIs are represented as Interfaces, as follows:
1. APIs Declaration
Each method must have an http annotation that provides the method and the URL. Five
annotations are provided: GET, POST, PUT, DELETE and HEAD. Here is an example:
It is possible to specify the parameters of the requesting the URL:
For a dynamic replacement of blocks and parameters in the method, use {}, then reference
the block with the annotation @PATH, as follows:
It is also possible to use the query parameters as follows:
Retrofit : http://square.github.io/retrofit/
1
WEB SERVICES DR. LILIA SFAXI !2
The resulting URL will be as follows:
group/id_val/users ?sort=sort_val
To insert several parameters in the request, use a Map:
Go to the Retrofit website for more examples.
2. Data Format
By default, Retrofit can only deserialise HTTP bodies as ResponseBody objects of the library
OkHttp, and accepts only request of type RequestBody for the @Body.
But several converters exist to support other types, such as Gson. Gson is a Java library used
to convert Java objects into their JSON representation, and vice-versa.
To support Retrofit, you should first add it to the Gradle dependencies of your project. In
Retrofit 2.x, OkHttp is included by default with retrofit, you don’t need to explicitly import it.
Activity 1: Create a project called Lab5. Add the following
dependencies, by inserting the following line in the file
build.gradle (Module:app) in the dependencies part:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Don’t forget to synchronise your project for the dependencies to be taken into account.
III. Exercise : Access to a Web Service
1. Objective
The main objective of this part is to create a simple application that accesses data coming
from an external web service.
WEB SERVICES DR. LILIA SFAXI !3
2. Generation of POJO classes from JSON data
The web service that we call is a public weather service. To see the current weather in Tunis,
use the following URL, for example:
http://api.openweathermap.org/data/2.5/weather?
q=Tunis&APPID=17db59488cadcad345211c36304a9266
The obtained response is the following:
WEB SERVICES DR. LILIA SFAXI !4
To obtain a valid APPID, you have to sign up to the site: http://openweathermap.org, an
APPID is generated for you, you will find it in your profile. You can then just replace the
APPID of the previous request with yours.
Once a valid response obtained, as a JSON document, it is possible to generate POJO classes
that you are going to use to handle the data of the web service easily.
To do that:
1. Go to: http://www.jsonschema2pojo.org/
2. Paste the JSON document you just obtained in the left area.
3. In the right area, write the name of the package (for example com.weather), the
name of the main class (for example Model), use JSON as a source type and Gson
as an annotation style, like the following:
WEB SERVICES DR. LILIA SFAXI !5
4. Click on Zip to download the project containing the generated classes.
5. Copy the generated classes in a new POJO package of your source directory:
3. Layout Creation
Start by creating a layout with 4 Textfields, called respectively txt_city, txt_status,
txt_humidity and txt_press.
Also add in the Manifest file, a permission for internet access.
<uses-permission android:name=« android.permission.INTERNET"/>
4. RestInterface
WEB SERVICES DR. LILIA SFAXI !6
1. Create an interface classed RestInterface in your project. This interface will map a
given URL to a method call, thanks to annotations of Retrofit API.
2. To obtain the weather of Tunis, the code of RestInterface will be as follows:
Notice that the URL starts with /weather. In fact, the main path will be defined in the main
class, when the service is called.
5. Retrofit Builder
In the same RestInterface, add a Retrofit Builder to build your request:
The url used here is a static String attribute representing the main URL of the service:
http://api.openweathermap.org/data/2.5/
6. Build the Activity
To call the service, follow these steps:
1. Create the appropriate textviews
2. Create an object RestInterface :
3. Call the service:
4. Schedule the call to happen asynchronously and provide the callback to be
executed upon completion.
WEB SERVICES DR. LILIA SFAXI !7
Activity 2: Test the previous steps and verify that the
textviews are loaded with valid information from the web service.
7. Adding Parameters
We want the call to the web service to be parametrized, which means that the city name will
be provided dynamically by the user of the application.
Activity 3: Add a parameter to the getWeatherReport function.
Then add a spinner to your layout, and make sure the weather
information changes according to the selected name of the city in
the spinner.
WEB SERVICES DR. LILIA SFAXI !8

More Related Content

What's hot

Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and ActivityNikmesoft Ltd
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intentDiego Grancini
 
Django tutorial
Django tutorialDjango tutorial
Django tutorialKsd Che
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI WidgetsAhsanul Karim
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101Rich Helton
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Text editors project
Text editors projectText editors project
Text editors projectMohit kumar
 
Identifing Listeners and Filters
Identifing Listeners and FiltersIdentifing Listeners and Filters
Identifing Listeners and FiltersPeople Strategists
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsDenis Minja
 

What's hot (20)

Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
[Android] Intent and Activity
[Android] Intent and Activity[Android] Intent and Activity
[Android] Intent and Activity
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Django tutorial
Django tutorialDjango tutorial
Django tutorial
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 
AspMVC4 start101
AspMVC4 start101AspMVC4 start101
AspMVC4 start101
 
Ppt 2 android_basics
Ppt 2 android_basicsPpt 2 android_basics
Ppt 2 android_basics
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
jQuery plugins & JSON
jQuery plugins & JSONjQuery plugins & JSON
jQuery plugins & JSON
 
Text editors project
Text editors projectText editors project
Text editors project
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Identifing Listeners and Filters
Identifing Listeners and FiltersIdentifing Listeners and Filters
Identifing Listeners and Filters
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Android UI Fundamentals part 1
Android UI Fundamentals part 1Android UI Fundamentals part 1
Android UI Fundamentals part 1
 
Enhancements
Enhancements Enhancements
Enhancements
 
Android Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intentsAndroid Bootcamp Tanzania:intents
Android Bootcamp Tanzania:intents
 
05 intent
05 intent05 intent
05 intent
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 

Similar to Lab 5-Android

ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationAMARAAHMED7
 
7 network programmability concepts api
7 network programmability concepts api7 network programmability concepts api
7 network programmability concepts apiSagarR24
 
7 network programmability concepts api
7 network programmability concepts api7 network programmability concepts api
7 network programmability concepts apiSagarR24
 
7 network programmability concepts python-ansible
7 network programmability concepts python-ansible7 network programmability concepts python-ansible
7 network programmability concepts python-ansibleSagarR24
 
MuleSoft Certified Platform Architect Exam Dumps 2023.pdf
MuleSoft Certified Platform Architect Exam Dumps 2023.pdfMuleSoft Certified Platform Architect Exam Dumps 2023.pdf
MuleSoft Certified Platform Architect Exam Dumps 2023.pdfSkillCertProExams
 
Configuring LIFA for remote communication using web architecture
Configuring LIFA for remote communication using web architecture Configuring LIFA for remote communication using web architecture
Configuring LIFA for remote communication using web architecture Ami Goswami
 
Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014Lou Sacco
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for androidInnovationM
 
Retrofit Library In Android
Retrofit Library In AndroidRetrofit Library In Android
Retrofit Library In AndroidInnovationM
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Akhil Mittal
 
Remote Config REST API and Versioning
Remote Config REST API and VersioningRemote Config REST API and Versioning
Remote Config REST API and VersioningJumpei Matsuda
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteorSapna Upreti
 
Android application architecture
Android application architectureAndroid application architecture
Android application architectureRomain Rochegude
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Akhil Mittal
 

Similar to Lab 5-Android (20)

ASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web applicationASP.NET Core Web API documentation web application
ASP.NET Core Web API documentation web application
 
7 network programmability concepts api
7 network programmability concepts api7 network programmability concepts api
7 network programmability concepts api
 
7 network programmability concepts api
7 network programmability concepts api7 network programmability concepts api
7 network programmability concepts api
 
7 network programmability concepts python-ansible
7 network programmability concepts python-ansible7 network programmability concepts python-ansible
7 network programmability concepts python-ansible
 
MuleSoft Certified Platform Architect Exam Dumps 2023.pdf
MuleSoft Certified Platform Architect Exam Dumps 2023.pdfMuleSoft Certified Platform Architect Exam Dumps 2023.pdf
MuleSoft Certified Platform Architect Exam Dumps 2023.pdf
 
Configuring LIFA for remote communication using web architecture
Configuring LIFA for remote communication using web architecture Configuring LIFA for remote communication using web architecture
Configuring LIFA for remote communication using web architecture
 
Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014
 
Cognos Software Development Kit
Cognos Software Development KitCognos Software Development Kit
Cognos Software Development Kit
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for android
 
Retrofit Library In Android
Retrofit Library In AndroidRetrofit Library In Android
Retrofit Library In Android
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
 
P H P Framework
P H P  FrameworkP H P  Framework
P H P Framework
 
Remote Config REST API and Versioning
Remote Config REST API and VersioningRemote Config REST API and Versioning
Remote Config REST API and Versioning
 
Api RESTFull
Api RESTFullApi RESTFull
Api RESTFull
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...
 
Sst hackathon express
Sst hackathon expressSst hackathon express
Sst hackathon express
 
Web API with ASP.NET MVC by Software development company in india
Web API with ASP.NET  MVC  by Software development company in indiaWeb API with ASP.NET  MVC  by Software development company in india
Web API with ASP.NET MVC by Software development company in india
 
News web application
News web applicationNews web application
News web application
 

More from Lilia Sfaxi

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfLilia Sfaxi
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfLilia Sfaxi
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-CassandraLilia Sfaxi
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-CorrectionLilia Sfaxi
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-CorrectionLilia Sfaxi
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-CorrectionLilia Sfaxi
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-CorrectionLilia Sfaxi
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-SéquencesLilia Sfaxi
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-CorrectionLilia Sfaxi
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - CorrectionLilia Sfaxi
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correctionLilia Sfaxi
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrageLilia Sfaxi
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Lilia Sfaxi
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intentsLilia Sfaxi
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web servicesLilia Sfaxi
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésLilia Sfaxi
 

More from Lilia Sfaxi (20)

chp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdfchp1-Intro à l'urbanisation des SI.pdf
chp1-Intro à l'urbanisation des SI.pdf
 
Plan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdfPlan d'études_INSAT_2022_2023.pdf
Plan d'études_INSAT_2022_2023.pdf
 
Lab3-DB_Neo4j
Lab3-DB_Neo4jLab3-DB_Neo4j
Lab3-DB_Neo4j
 
Lab2-DB-Mongodb
Lab2-DB-MongodbLab2-DB-Mongodb
Lab2-DB-Mongodb
 
Lab1-DB-Cassandra
Lab1-DB-CassandraLab1-DB-Cassandra
Lab1-DB-Cassandra
 
TP2-UML-Correction
TP2-UML-CorrectionTP2-UML-Correction
TP2-UML-Correction
 
TP1-UML-Correction
TP1-UML-CorrectionTP1-UML-Correction
TP1-UML-Correction
 
TP0-UML-Correction
TP0-UML-CorrectionTP0-UML-Correction
TP0-UML-Correction
 
TD4-UML
TD4-UMLTD4-UML
TD4-UML
 
TD4-UML-Correction
TD4-UML-CorrectionTD4-UML-Correction
TD4-UML-Correction
 
TD3-UML-Séquences
TD3-UML-SéquencesTD3-UML-Séquences
TD3-UML-Séquences
 
TD3-UML-Correction
TD3-UML-CorrectionTD3-UML-Correction
TD3-UML-Correction
 
TD2 - UML - Correction
TD2 - UML - CorrectionTD2 - UML - Correction
TD2 - UML - Correction
 
TD1 - UML - DCU
TD1 - UML - DCUTD1 - UML - DCU
TD1 - UML - DCU
 
TD1-UML-correction
TD1-UML-correctionTD1-UML-correction
TD1-UML-correction
 
Android - Tp1 - installation et démarrage
Android - Tp1 -   installation et démarrageAndroid - Tp1 -   installation et démarrage
Android - Tp1 - installation et démarrage
 
Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques Android - Tp2 - Elements graphiques
Android - Tp2 - Elements graphiques
 
Android - Tp3 - intents
Android - Tp3 -  intentsAndroid - Tp3 -  intents
Android - Tp3 - intents
 
Android - TPBonus - web services
Android - TPBonus - web servicesAndroid - TPBonus - web services
Android - TPBonus - web services
 
Android - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancésAndroid - Tp4 - graphiques avancés
Android - Tp4 - graphiques avancés
 

Recently uploaded

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 

Recently uploaded (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Lab 5-Android

  • 1. CS425 - Web and Mobile Software Engineering Dr. Lilia Sfaxi 1 mai 2017 Web Services Android Lab 05 
 WEB SERVICES DR. LILIA SFAXI !1 MedTech
  • 2. I. Retrofit Retrofit is a REST client for Android and Java, enabling the grouping and organisation of all 1 the APIs in a simple and clean way. Annotations are used to control the parameters or the API, defined in interfaces, called from classes. All the http APIs are represented as Interfaces, as follows: 1. APIs Declaration Each method must have an http annotation that provides the method and the URL. Five annotations are provided: GET, POST, PUT, DELETE and HEAD. Here is an example: It is possible to specify the parameters of the requesting the URL: For a dynamic replacement of blocks and parameters in the method, use {}, then reference the block with the annotation @PATH, as follows: It is also possible to use the query parameters as follows: Retrofit : http://square.github.io/retrofit/ 1 WEB SERVICES DR. LILIA SFAXI !2
  • 3. The resulting URL will be as follows: group/id_val/users ?sort=sort_val To insert several parameters in the request, use a Map: Go to the Retrofit website for more examples. 2. Data Format By default, Retrofit can only deserialise HTTP bodies as ResponseBody objects of the library OkHttp, and accepts only request of type RequestBody for the @Body. But several converters exist to support other types, such as Gson. Gson is a Java library used to convert Java objects into their JSON representation, and vice-versa. To support Retrofit, you should first add it to the Gradle dependencies of your project. In Retrofit 2.x, OkHttp is included by default with retrofit, you don’t need to explicitly import it. Activity 1: Create a project called Lab5. Add the following dependencies, by inserting the following line in the file build.gradle (Module:app) in the dependencies part: compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.retrofit2:converter-gson:2.1.0' Don’t forget to synchronise your project for the dependencies to be taken into account. III. Exercise : Access to a Web Service 1. Objective The main objective of this part is to create a simple application that accesses data coming from an external web service. WEB SERVICES DR. LILIA SFAXI !3
  • 4. 2. Generation of POJO classes from JSON data The web service that we call is a public weather service. To see the current weather in Tunis, use the following URL, for example: http://api.openweathermap.org/data/2.5/weather? q=Tunis&APPID=17db59488cadcad345211c36304a9266 The obtained response is the following: WEB SERVICES DR. LILIA SFAXI !4
  • 5. To obtain a valid APPID, you have to sign up to the site: http://openweathermap.org, an APPID is generated for you, you will find it in your profile. You can then just replace the APPID of the previous request with yours. Once a valid response obtained, as a JSON document, it is possible to generate POJO classes that you are going to use to handle the data of the web service easily. To do that: 1. Go to: http://www.jsonschema2pojo.org/ 2. Paste the JSON document you just obtained in the left area. 3. In the right area, write the name of the package (for example com.weather), the name of the main class (for example Model), use JSON as a source type and Gson as an annotation style, like the following: WEB SERVICES DR. LILIA SFAXI !5
  • 6. 4. Click on Zip to download the project containing the generated classes. 5. Copy the generated classes in a new POJO package of your source directory: 3. Layout Creation Start by creating a layout with 4 Textfields, called respectively txt_city, txt_status, txt_humidity and txt_press. Also add in the Manifest file, a permission for internet access. <uses-permission android:name=« android.permission.INTERNET"/> 4. RestInterface WEB SERVICES DR. LILIA SFAXI !6
  • 7. 1. Create an interface classed RestInterface in your project. This interface will map a given URL to a method call, thanks to annotations of Retrofit API. 2. To obtain the weather of Tunis, the code of RestInterface will be as follows: Notice that the URL starts with /weather. In fact, the main path will be defined in the main class, when the service is called. 5. Retrofit Builder In the same RestInterface, add a Retrofit Builder to build your request: The url used here is a static String attribute representing the main URL of the service: http://api.openweathermap.org/data/2.5/ 6. Build the Activity To call the service, follow these steps: 1. Create the appropriate textviews 2. Create an object RestInterface : 3. Call the service: 4. Schedule the call to happen asynchronously and provide the callback to be executed upon completion. WEB SERVICES DR. LILIA SFAXI !7
  • 8. Activity 2: Test the previous steps and verify that the textviews are loaded with valid information from the web service. 7. Adding Parameters We want the call to the web service to be parametrized, which means that the city name will be provided dynamically by the user of the application. Activity 3: Add a parameter to the getWeatherReport function. Then add a spinner to your layout, and make sure the weather information changes according to the selected name of the city in the spinner. WEB SERVICES DR. LILIA SFAXI !8