SlideShare a Scribd company logo
1 of 20
The Lazy Programmers Guide to Consuming Web
Services (in Android)
Max Bhuiyan http://au.linkedin.com/in/mamnun
Ben Kenny http://au.linkedin.com/pub/ben-kenny/3a/b9a/b67
Disclaimer
These slides are available under the Creative
Commons Attribution-ShareAlike (BY-SA) 3.0
License.
Trademarks and brands are the property of their respective
owners.
This presentation is not affiliated with Yahoo, Yahoo7 or Seven
Media.
Web API integration challenges
● Authentication
● Response
deserialization
● Caching
● Unreliable Network
Android specific challenges
● Network calls on background thread
o Async Tasks
o Loader
o Service/Daemon
● Response Processing : CPU + Memory
constraint
No network, No problem
Standing on the
shoulders of
giants
● RoboSpice
● Jackson
● JSONPath
● Retrofit
● OkHttp
● Signpost
: Cache Manager
● Uses an AndroidService to execute network
requests in background
● Plugin Architecture
● Supports request queue & prioritization
● Per request caching policy
● Handles activity/fragment lifecycle
Jackson : JSON Processor
● FAST!!!
● Streaming parser
● Easy annotation
based mapping
● Handles
polymorphic
types(more about
this from Ben)
RetrofitRetrofit
● Super simple API definition
o 2 lines to define an endpoint
o 1 class with 3 functions to code the Request
● Uses OkHttp(uses nio)
Overview
Thanks for listening.
Have a potato :)
He who controls the spice, controls the universe!
-- Baron Harkonnen
Quick example - Github Keys API
$ curl -i https://api.github.com/users/snookle/keys
[
{
"id": 8050932,
"key": "ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQC6lev8ySCrH9IIysnPsgpNyVcPomAM/n
Gn2nC8Cx4y5ttL5K9C5He1qkw7QQW5jTDsgtdYhS5u46CvKQFzrFLcdffkEJP1
dsnR1y+PWgzpXA81xvGFrOlYBtGh3n9Iuh7z5pw2WEBXnTqLkAmDtozFkJ0uJb
0bUnIc2mVRj9noCk5/dGKnzIweKpxaMpSWhR7I46lrAWlKE1sX24gqSU9x7BK6
jrTX6wk/s62Y7DYkb6tON3z8pWil/weaHd1ipdPmLa+UNwfCv/rrS/Don/UGe8
/mXLurdufT1FipbWAXNOgof01LRhYl/su4Z8vHUEWLOg9d9gq59cql7N+AEqIl
"
},
...
]
Define Model and API interface
@JsonIgnoreProperties(ignoreUnknown = true)
public class Key {
public int id;
@JsonProperty(“key”)
public String publicKey;
public static class List extends ArrayList<Key>{}
}
public interface GithubAPI {
@GET("/users/{user}/keys")
Response userKeys(@Path("user") String user);
}
Subclass RetrofitSpiceService
public class GithubService extends RetrofitSpiceService {
@Override
public void onCreate(){
super.onCreate();
addRetrofitInterface(GithubAPI.class);
}
@Override
public CacheManager createCacheManager(Application application) throws
CacheCreationException {
return new CacheManager().addPersister(new
JacksonRetrofitObjectPersisterFactory(application, null));
}
@Override
protected String getServerUrl() { return “https://api.github.com”; }
@Override
protected Converter createConverter() { return new JacksonConverter(); }
}
AndroidManifest and SpiceManager
<service
android:name="com.example.github.GithubService"
android:exported="false"
/>
public class GithubSpiceManager extends SpiceManager {
public GithubSpiceManager() {
super(GithubService.class);
}
}
Define Request class
public class GithubUserKeyRequest extends RetrofitSpiceRequest<Key.List.class,
GithubAPI.class> {
private final JsonReader jsonReader = new JsonReader();
private final ObjectMapper objectMapper = new ObjectMapper();
private final String user;
public GithubUserKeyRequest(String user) {
super(Key.List.class, GithubAPI.class);
this.user = user;
}
@Override
public Episode.List loadDataFromNetwork() throws Exception {
Response response = getService().userKeys(user);
Object result =
this.jsonReader.parse(response.getBody().in()).read("$.");
response.getBody().in().close();
return (Key.List)this.objectMapper.convertValue(result, Key.List.class);
}
}
Spice up your life!
public class UserKeyFragment extends Fragment {
private final GithubSpiceManager spiceManager = new GithubSpiceManager();
@Override
public void onStart() {
super.onStart();
spiceManager.start(getActivity());
spiceManager.execute(new GithubUserKeyRequest("snookle"), "cacheKey",
1000 * 60, new RequestListener<Key.List>() {
@Override
public void onRequestFailure(SpiceException e) {
Toast.makeText(getActivity(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
@Override
public void onRequestSuccess(Key.List keys) {
// do cool stuff here????
}
});
}
}
Gotchas
▪ Request converter and Cache converter are separate and defined in
different classes
▪ JSON weirdness: “null”/null, and true/false/0/1
▪ spiceManager.isDataInCache() will cause a deadlock if called from
Activity/Fragment onStart().
▪ Inner classes must be declared static to access without an instance of
outer class.
› BAD: public class List extends ArrayList<Key>{}
› NOT VERY HELPFUL EXCEPTION: JsonMappingException: No
suitable constructor found for type [simple type, class
com.example.github.Key.List]: can not instantiate from
JSON object (need to add/enable type information?)
› BETTER: public static class List extends
ArrayList<Key>{}
The Lazy Programmers Guide to Consuming Web Services (in Android

More Related Content

What's hot

Groovy in the Cloud
Groovy in the CloudGroovy in the Cloud
Groovy in the CloudDaniel Woods
 
Lifthub (#rpscala 26)
Lifthub (#rpscala 26)Lifthub (#rpscala 26)
Lifthub (#rpscala 26)k4200
 
Lifthub (rpscala #31)
Lifthub (rpscala #31)Lifthub (rpscala #31)
Lifthub (rpscala #31)k4200
 
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Matt Raible
 
Intro to meteor @py gotham Aug 15-16 2015
Intro to meteor @py gotham Aug 15-16 2015Intro to meteor @py gotham Aug 15-16 2015
Intro to meteor @py gotham Aug 15-16 2015christieewen
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav DukhinFwdays
 
Micronaut For Single Page Apps
Micronaut For Single Page AppsMicronaut For Single Page Apps
Micronaut For Single Page AppsZachary Klein
 
Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Fátima Casaú Pérez
 
Full Stack Reactive with React and Spring WebFlux - PWX 2019
Full Stack Reactive with React and Spring WebFlux - PWX 2019Full Stack Reactive with React and Spring WebFlux - PWX 2019
Full Stack Reactive with React and Spring WebFlux - PWX 2019Matt Raible
 
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020Matt Raible
 
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019Matt Raible
 
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magecom UK Limited
 
Play vs Grails Smackdown - Devoxx France 2013
Play vs Grails Smackdown - Devoxx France 2013Play vs Grails Smackdown - Devoxx France 2013
Play vs Grails Smackdown - Devoxx France 2013Matt Raible
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6William Marques
 

What's hot (20)

Groovy in the Cloud
Groovy in the CloudGroovy in the Cloud
Groovy in the Cloud
 
Java EE 6
Java EE 6Java EE 6
Java EE 6
 
Lifthub (#rpscala 26)
Lifthub (#rpscala 26)Lifthub (#rpscala 26)
Lifthub (#rpscala 26)
 
Lifthub (rpscala #31)
Lifthub (rpscala #31)Lifthub (rpscala #31)
Lifthub (rpscala #31)
 
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
 
Intro to meteor @py gotham Aug 15-16 2015
Intro to meteor @py gotham Aug 15-16 2015Intro to meteor @py gotham Aug 15-16 2015
Intro to meteor @py gotham Aug 15-16 2015
 
Dropwizard
DropwizardDropwizard
Dropwizard
 
Web Space10 Overview
Web Space10 OverviewWeb Space10 Overview
Web Space10 Overview
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
 
Micronaut For Single Page Apps
Micronaut For Single Page AppsMicronaut For Single Page Apps
Micronaut For Single Page Apps
 
Java 9 and Beyond
Java 9 and BeyondJava 9 and Beyond
Java 9 and Beyond
 
Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?
 
Full Stack Reactive with React and Spring WebFlux - PWX 2019
Full Stack Reactive with React and Spring WebFlux - PWX 2019Full Stack Reactive with React and Spring WebFlux - PWX 2019
Full Stack Reactive with React and Spring WebFlux - PWX 2019
 
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020
Full Stack Reactive with React and Spring WebFlux - Switzerland JUG 2020
 
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019
Full Stack Reactive with React and Spring WebFlux - Dublin JUG 2019
 
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
 
Play vs Grails Smackdown - Devoxx France 2013
Play vs Grails Smackdown - Devoxx France 2013Play vs Grails Smackdown - Devoxx France 2013
Play vs Grails Smackdown - Devoxx France 2013
 
Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
Micronaut Launchpad
Micronaut LaunchpadMicronaut Launchpad
Micronaut Launchpad
 
GWT and PWA
GWT and PWAGWT and PWA
GWT and PWA
 

Similar to The Lazy Programmers Guide to Consuming Web Services (in Android

using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'sAntônio Roberto Silva
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Enginecatherinewall
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020Emily Jiang
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020Emily Jiang
 
"Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native ""Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native "FDConf
 
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexusMicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexusEmily Jiang
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Matt Raible
 
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020Matt Raible
 
Hybrid App using WordPress
Hybrid App using WordPressHybrid App using WordPress
Hybrid App using WordPressHaim Michael
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Matt Raible
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EEAlexis Hassler
 
Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017Matt Raible
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)Google
 
Docker & ECS: Secure Nearline Execution
Docker & ECS: Secure Nearline ExecutionDocker & ECS: Secure Nearline Execution
Docker & ECS: Secure Nearline ExecutionBrennan Saeta
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Roberto Franchini
 
Elastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara MicroElastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara MicroOndrej Mihályi
 

Similar to The Lazy Programmers Guide to Consuming Web Services (in Android (20)

using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Engine
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020
 
Cloud nativemicroservices jax-london2020
Cloud nativemicroservices   jax-london2020Cloud nativemicroservices   jax-london2020
Cloud nativemicroservices jax-london2020
 
Angular beans
Angular beansAngular beans
Angular beans
 
"Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native ""Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native "
 
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexusMicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
MicroProfile, Docker, Kubernetes, Istio and Open Shift lab @dev nexus
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Jforum S...
 
Android development
Android developmentAndroid development
Android development
 
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020
Choose Your Own Adventure with JHipster & Kubernetes - Denver JUG 2020
 
Hybrid App using WordPress
Hybrid App using WordPressHybrid App using WordPress
Hybrid App using WordPress
 
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
Microservices for the Masses with Spring Boot, JHipster, and OAuth - Switzerl...
 
softshake 2014 - Java EE
softshake 2014 - Java EEsoftshake 2014 - Java EE
softshake 2014 - Java EE
 
Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)
 
Docker & ECS: Secure Nearline Execution
Docker & ECS: Secure Nearline ExecutionDocker & ECS: Secure Nearline Execution
Docker & ECS: Secure Nearline Execution
 
Arquitecturas de microservicios - Medianet Software
Arquitecturas de microservicios   -  Medianet SoftwareArquitecturas de microservicios   -  Medianet Software
Arquitecturas de microservicios - Medianet Software
 
C#on linux
C#on linuxC#on linux
C#on linux
 
Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!Integration tests: use the containers, Luke!
Integration tests: use the containers, Luke!
 
Elastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara MicroElastic and Cloud-ready Applications with Payara Micro
Elastic and Cloud-ready Applications with Payara Micro
 

Recently uploaded

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Recently uploaded (20)

Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

The Lazy Programmers Guide to Consuming Web Services (in Android

  • 1. The Lazy Programmers Guide to Consuming Web Services (in Android) Max Bhuiyan http://au.linkedin.com/in/mamnun Ben Kenny http://au.linkedin.com/pub/ben-kenny/3a/b9a/b67
  • 2. Disclaimer These slides are available under the Creative Commons Attribution-ShareAlike (BY-SA) 3.0 License. Trademarks and brands are the property of their respective owners. This presentation is not affiliated with Yahoo, Yahoo7 or Seven Media.
  • 3. Web API integration challenges ● Authentication ● Response deserialization ● Caching ● Unreliable Network
  • 4. Android specific challenges ● Network calls on background thread o Async Tasks o Loader o Service/Daemon ● Response Processing : CPU + Memory constraint
  • 5. No network, No problem
  • 6. Standing on the shoulders of giants ● RoboSpice ● Jackson ● JSONPath ● Retrofit ● OkHttp ● Signpost
  • 7. : Cache Manager ● Uses an AndroidService to execute network requests in background ● Plugin Architecture ● Supports request queue & prioritization ● Per request caching policy ● Handles activity/fragment lifecycle
  • 8. Jackson : JSON Processor ● FAST!!! ● Streaming parser ● Easy annotation based mapping ● Handles polymorphic types(more about this from Ben)
  • 9. RetrofitRetrofit ● Super simple API definition o 2 lines to define an endpoint o 1 class with 3 functions to code the Request ● Uses OkHttp(uses nio)
  • 12. He who controls the spice, controls the universe! -- Baron Harkonnen
  • 13. Quick example - Github Keys API $ curl -i https://api.github.com/users/snookle/keys [ { "id": 8050932, "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6lev8ySCrH9IIysnPsgpNyVcPomAM/n Gn2nC8Cx4y5ttL5K9C5He1qkw7QQW5jTDsgtdYhS5u46CvKQFzrFLcdffkEJP1 dsnR1y+PWgzpXA81xvGFrOlYBtGh3n9Iuh7z5pw2WEBXnTqLkAmDtozFkJ0uJb 0bUnIc2mVRj9noCk5/dGKnzIweKpxaMpSWhR7I46lrAWlKE1sX24gqSU9x7BK6 jrTX6wk/s62Y7DYkb6tON3z8pWil/weaHd1ipdPmLa+UNwfCv/rrS/Don/UGe8 /mXLurdufT1FipbWAXNOgof01LRhYl/su4Z8vHUEWLOg9d9gq59cql7N+AEqIl " }, ... ]
  • 14. Define Model and API interface @JsonIgnoreProperties(ignoreUnknown = true) public class Key { public int id; @JsonProperty(“key”) public String publicKey; public static class List extends ArrayList<Key>{} } public interface GithubAPI { @GET("/users/{user}/keys") Response userKeys(@Path("user") String user); }
  • 15. Subclass RetrofitSpiceService public class GithubService extends RetrofitSpiceService { @Override public void onCreate(){ super.onCreate(); addRetrofitInterface(GithubAPI.class); } @Override public CacheManager createCacheManager(Application application) throws CacheCreationException { return new CacheManager().addPersister(new JacksonRetrofitObjectPersisterFactory(application, null)); } @Override protected String getServerUrl() { return “https://api.github.com”; } @Override protected Converter createConverter() { return new JacksonConverter(); } }
  • 16. AndroidManifest and SpiceManager <service android:name="com.example.github.GithubService" android:exported="false" /> public class GithubSpiceManager extends SpiceManager { public GithubSpiceManager() { super(GithubService.class); } }
  • 17. Define Request class public class GithubUserKeyRequest extends RetrofitSpiceRequest<Key.List.class, GithubAPI.class> { private final JsonReader jsonReader = new JsonReader(); private final ObjectMapper objectMapper = new ObjectMapper(); private final String user; public GithubUserKeyRequest(String user) { super(Key.List.class, GithubAPI.class); this.user = user; } @Override public Episode.List loadDataFromNetwork() throws Exception { Response response = getService().userKeys(user); Object result = this.jsonReader.parse(response.getBody().in()).read("$."); response.getBody().in().close(); return (Key.List)this.objectMapper.convertValue(result, Key.List.class); } }
  • 18. Spice up your life! public class UserKeyFragment extends Fragment { private final GithubSpiceManager spiceManager = new GithubSpiceManager(); @Override public void onStart() { super.onStart(); spiceManager.start(getActivity()); spiceManager.execute(new GithubUserKeyRequest("snookle"), "cacheKey", 1000 * 60, new RequestListener<Key.List>() { @Override public void onRequestFailure(SpiceException e) { Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show(); } @Override public void onRequestSuccess(Key.List keys) { // do cool stuff here???? } }); } }
  • 19. Gotchas ▪ Request converter and Cache converter are separate and defined in different classes ▪ JSON weirdness: “null”/null, and true/false/0/1 ▪ spiceManager.isDataInCache() will cause a deadlock if called from Activity/Fragment onStart(). ▪ Inner classes must be declared static to access without an instance of outer class. › BAD: public class List extends ArrayList<Key>{} › NOT VERY HELPFUL EXCEPTION: JsonMappingException: No suitable constructor found for type [simple type, class com.example.github.Key.List]: can not instantiate from JSON object (need to add/enable type information?) › BETTER: public static class List extends ArrayList<Key>{}