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

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

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