SlideShare a Scribd company logo
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

PDF
Lab2-android
PDF
Lab3-Android
PDF
Lab4 - android
PDF
Angular
PDF
Lab1-android
PDF
Day 8: Dealing with Lists and ListViews
PPTX
Android Tutorials : Basic widgets
ODP
Android App Development - 04 Views and layouts
Lab2-android
Lab3-Android
Lab4 - android
Angular
Lab1-android
Day 8: Dealing with Lists and ListViews
Android Tutorials : Basic widgets
Android App Development - 04 Views and layouts

What's hot (20)

PPTX
Create an android app for database creation using.pptx
PDF
[Android] Intent and Activity
ODP
Android App Development - 02 Activity and intent
PDF
Django tutorial
PDF
Android Components
PPT
Day 4: Android: UI Widgets
PPT
AspMVC4 start101
ODP
Ppt 2 android_basics
PDF
Mvc interview questions – deep dive jinal desai
PPTX
DOCX
Text editors project
PPT
Android - Android Intent Types
PDF
Identifing Listeners and Filters
PDF
Android Basic Components
PPTX
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
PDF
Android UI Fundamentals part 1
PPT
Enhancements
PPT
Android Bootcamp Tanzania:intents
PPTX
05 intent
PPS
JSP Error handling
Create an android app for database creation using.pptx
[Android] Intent and Activity
Android App Development - 02 Activity and intent
Django tutorial
Android Components
Day 4: Android: UI Widgets
AspMVC4 start101
Ppt 2 android_basics
Mvc interview questions – deep dive jinal desai
Text editors project
Android - Android Intent Types
Identifing Listeners and Filters
Android Basic Components
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android UI Fundamentals part 1
Enhancements
Android Bootcamp Tanzania:intents
05 intent
JSP Error handling
Ad

Similar to Lab 5-Android (20)

PDF
Android Libs - Retrofit
PPTX
WELCOME TO OUR PRESENTION.pptx
PDF
Android application architecture
PPTX
Não reinvente a roda! Desenvolvendo com bibliotecas Android
PDF
Java Libraries You Can’t Afford to Miss
PDF
Designing a JavaFX Mobile application
PPT
Web api's
PDF
Advanced #2 networking
PPTX
Using Retrofit framework in implementation of Android REST client (Presentation)
PDF
Introduction to Retrofit
PPTX
Retrofit 2 - O que devemos saber
PDF
Modern Android app library stack
PDF
13 web service integration
PDF
Training Semester Report, Api Types of Apps
PPTX
Retrofit Technology Overview by Cumulations Technologies
PDF
Retrofit
PPTX
Firebase, Jetpack, Retrofit.pptx
PDF
REST more with json-api and fractal
PDF
GDG Algiers DevFest 2013 Cool AndroidLibs
PPTX
Android dev toolbox
Android Libs - Retrofit
WELCOME TO OUR PRESENTION.pptx
Android application architecture
Não reinvente a roda! Desenvolvendo com bibliotecas Android
Java Libraries You Can’t Afford to Miss
Designing a JavaFX Mobile application
Web api's
Advanced #2 networking
Using Retrofit framework in implementation of Android REST client (Presentation)
Introduction to Retrofit
Retrofit 2 - O que devemos saber
Modern Android app library stack
13 web service integration
Training Semester Report, Api Types of Apps
Retrofit Technology Overview by Cumulations Technologies
Retrofit
Firebase, Jetpack, Retrofit.pptx
REST more with json-api and fractal
GDG Algiers DevFest 2013 Cool AndroidLibs
Android dev toolbox
Ad

More from Lilia Sfaxi (20)

PDF
chp1-Intro à l'urbanisation des SI.pdf
PDF
Plan d'études_INSAT_2022_2023.pdf
PDF
Lab3-DB_Neo4j
PDF
Lab2-DB-Mongodb
PDF
Lab1-DB-Cassandra
PDF
TP2-UML-Correction
PDF
TP1-UML-Correction
PDF
TP0-UML-Correction
PDF
TD4-UML
PDF
TD4-UML-Correction
PDF
TD3-UML-Séquences
PDF
TD3-UML-Correction
PDF
TD2 - UML - Correction
PDF
TD1 - UML - DCU
PDF
TD1-UML-correction
PDF
Android - Tp1 - installation et démarrage
PDF
Android - Tp2 - Elements graphiques
PDF
Android - Tp3 - intents
PDF
Android - TPBonus - web services
PDF
Android - Tp4 - graphiques avancés
chp1-Intro à l'urbanisation des SI.pdf
Plan d'études_INSAT_2022_2023.pdf
Lab3-DB_Neo4j
Lab2-DB-Mongodb
Lab1-DB-Cassandra
TP2-UML-Correction
TP1-UML-Correction
TP0-UML-Correction
TD4-UML
TD4-UML-Correction
TD3-UML-Séquences
TD3-UML-Correction
TD2 - UML - Correction
TD1 - UML - DCU
TD1-UML-correction
Android - Tp1 - installation et démarrage
Android - Tp2 - Elements graphiques
Android - Tp3 - intents
Android - TPBonus - web services
Android - Tp4 - graphiques avancés

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
Reach Out and Touch Someone: Haptics and Empathic Computing
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Programs and apps: productivity, graphics, security and other tools
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MIND Revenue Release Quarter 2 2025 Press Release
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Dropbox Q2 2025 Financial Results & Investor Presentation
20250228 LYD VKU AI Blended-Learning.pptx
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.

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