SlideShare a Scribd company logo
Android + Retrofit +
REST + JSON = Easy
presented by Chris Ollenburg
Basic REST API
• Create - http://www.fakeservice.com/user
• Read - http://www.fakeservice.com/user/{id}
• Update - http://www.fakeservice.com/user/{id}?
name={new name}
• Delete - http://www.fakeservice.com/user/{id}
public interface UserService {
@POST("/user") // CREATE
void createUser(@Body User user);
@GET("/user/{userId}") // READ
User getUser(@Path("userId") String userId);
@PUT("/user/{userId}") // UPDATE
void updateUserName(@Path("userId") String userId,
@Body User user);
@DELETE(“/user/{userId}“) // DELETE
void deleteUser(@Path("userId") String userId);
}
public class User {

String name; 

Date birthDate;

Integer age;
}
Synchronous
public interface MyService {

@GET(“/test") 

Message getMessage(); 

}
Asynchronous
public interface MyService { 

@GET(“/test") 

void getMessage(Callback<Message> msg); 

}
Observable / Rx
public interface MyService { 

@GET(“/test") 

Observable<Message> getMessage(); 

}
Path Variables
@GET("/user/{userId}") // READ
User getUser(@Path("userId") String userId);
Query Parameters
@GET("/user") // READ 

User getUser(@Query("userId") String userId);
Http Headers
@Headers({"Accept: application/json”})
@GET(“/user/self“)
User getUser(@Header("Authorization") String auth)
Request Body
@PUT("/user/{userId}") 

void updateUserName(@Path("userId") String userId,
@Body User user);
@Multipart
@POST(“/user")
void createUser(@Part("fName") String fName,
@Part("lName") String lName);
@FormUrlEncoded
@POST(“/user")
void createUser(@Field("fName") String fName,
@Field("lName") String lName);
Sync Call
public void sendSync() { 

RestAdapter restAdapter = new RestAdapter.Builder() 

.setEndpoint(“http://www.myservice.com") 

.build(); 

DemoService service = restAdapter.create(DemoService.class); 



DemoModel rep = service.getMessage(apiId);
// Do something w/ object 

}
Async Call
public void sendAsyncButton() { 

service.getMessage(apiId, new Callback<DemoModel>() { 

@Override 

public void success(DemoModel demoModel, retrofit.client.Response response) { 

MainActivity.this.contentView.setText(demoModel.message); 

} 



@Override 

public void failure(RetrofitError error) {
Log.e(TAG, error.getMessage()); 

MainActivity.this.contentView.setText(error.getMessage()); 

} 

}); 

}
Call w/ RxJava
public void sendRx() { 

Observable<DemoModel> observable = service.getMessageRx(apiId);
observable.subscribe(new Action1<DemoModel>() { 

@Override 

public void call(DemoModel demoModel) { 

MainActivity.this.contentView.setText(demoModel.message); 

} 

}); 

}
Extras
• Adjustable logging level
• Custom GSON deserializers
• Custom deserializer
• Custom Http Client Implementations
Gotcha
Not Cancelable
Demo
Chris Ollenburg
• Principal Consultant at Cardinal Solutions

collenburg@cardinalsolutions.com

@cardinalNow 

www.cardinalsolutions.com
• CTO of Taggd’ LLC 

chris@taggdstreetart.com

www.taggdstreetart.com

Links
http://square.github.io/retrofit/
https://github.com/OllenDev/RetrofitDemo

More Related Content

What's hot

REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
PiXeL16
 
Integration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + SeleniumIntegration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + Selenium
tka
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Nina Zakharenko
 
Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2
Almog Baku
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
Hiroshi Nakamura
 
httpie
httpiehttpie
Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)
Mindfire Solutions
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
MongoDB
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
A python web service
A python web serviceA python web service
A python web service
Temian Vlad
 
Md+wiki
Md+wikiMd+wiki
Retrofit
RetrofitRetrofit
Retrofit
Amin Cheloh
 
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
Codemotion
 
REST Web API with MongoDB
REST Web API with MongoDBREST Web API with MongoDB
REST Web API with MongoDB
MongoDB
 
DevOps and Chef
DevOps and ChefDevOps and Chef
DevOps and Chef
PiXeL16
 
Entity provider selection confusion attacks in JAX-RS applications
Entity provider selection confusion attacks in JAX-RS applicationsEntity provider selection confusion attacks in JAX-RS applications
Entity provider selection confusion attacks in JAX-RS applications
Mikhail Egorov
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話
Takehito Tanabe
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
Leticia Rss
 
Introduction to Flask Micro Framework
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro Framework
Mohammad Reza Kamalifard
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
Taylor Lovett
 

What's hot (20)

REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
 
Integration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + SeleniumIntegration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + Selenium
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
 
httpie
httpiehttpie
httpie
 
Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
A python web service
A python web serviceA python web service
A python web service
 
Md+wiki
Md+wikiMd+wiki
Md+wiki
 
Retrofit
RetrofitRetrofit
Retrofit
 
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK -  Nicola Iarocci - Co...
RESTFUL SERVICES MADE EASY: THE EVE REST API FRAMEWORK - Nicola Iarocci - Co...
 
REST Web API with MongoDB
REST Web API with MongoDBREST Web API with MongoDB
REST Web API with MongoDB
 
DevOps and Chef
DevOps and ChefDevOps and Chef
DevOps and Chef
 
Entity provider selection confusion attacks in JAX-RS applications
Entity provider selection confusion attacks in JAX-RS applicationsEntity provider selection confusion attacks in JAX-RS applications
Entity provider selection confusion attacks in JAX-RS applications
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
 
Introduction to Flask Micro Framework
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro Framework
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 

Viewers also liked

Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJava
Fabio Collini
 
Retrofit
RetrofitRetrofit
Retrofit
bresiu
 
RxJava + Retrofit
RxJava + RetrofitRxJava + Retrofit
RxJava + Retrofit
Dev2Dev
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
Ted Liang
 
Rx java x retrofit
Rx java x retrofitRx java x retrofit
Rx java x retrofit
Shun Nakahara
 
Using Retrofit framework in implementation of Android REST client (Presentation)
Using Retrofit framework in implementation of Android REST client (Presentation)Using Retrofit framework in implementation of Android REST client (Presentation)
Using Retrofit framework in implementation of Android REST client (Presentation)
Zlatko Stapic
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
Tomáš Kypta
 
Introduction to rx java for android
Introduction to rx java for androidIntroduction to rx java for android
Introduction to rx java for android
Esa Firman
 
Realm: Building a mobile database
Realm: Building a mobile databaseRealm: Building a mobile database
Realm: Building a mobile database
Christian Melchior
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Retrofitting
RetrofittingRetrofitting
Retrofitting
Rizwan Samor
 
Designing Teams for Emerging Challenges
Designing Teams for Emerging ChallengesDesigning Teams for Emerging Challenges
Designing Teams for Emerging Challenges
Aaron Irizarry
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
Seth Familian
 

Viewers also liked (13)

Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJava
 
Retrofit
RetrofitRetrofit
Retrofit
 
RxJava + Retrofit
RxJava + RetrofitRxJava + Retrofit
RxJava + Retrofit
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
 
Rx java x retrofit
Rx java x retrofitRx java x retrofit
Rx java x retrofit
 
Using Retrofit framework in implementation of Android REST client (Presentation)
Using Retrofit framework in implementation of Android REST client (Presentation)Using Retrofit framework in implementation of Android REST client (Presentation)
Using Retrofit framework in implementation of Android REST client (Presentation)
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
Introduction to rx java for android
Introduction to rx java for androidIntroduction to rx java for android
Introduction to rx java for android
 
Realm: Building a mobile database
Realm: Building a mobile databaseRealm: Building a mobile database
Realm: Building a mobile database
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Retrofitting
RetrofittingRetrofitting
Retrofitting
 
Designing Teams for Emerging Challenges
Designing Teams for Emerging ChallengesDesigning Teams for Emerging Challenges
Designing Teams for Emerging Challenges
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 

Similar to Retrofit Android by Chris Ollenburg

External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQuery
Doncho Minkov
 
A evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no androidA evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no android
Rodrigo de Souza Castro
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
Jeroen van Dijk
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
JBug Italy
 
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
South Tyrol Free Software Conference
 
Azure Mobile Services .NET Backend
Azure Mobile Services .NET BackendAzure Mobile Services .NET Backend
Azure Mobile Services .NET Backend
Shiju Varghese
 
RESTEasy
RESTEasyRESTEasy
Migration microservices to GraphQL
Migration microservices to GraphQLMigration microservices to GraphQL
Migration microservices to GraphQL
Roman Krivtsov
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4
DEVCON
 
Vulpes tribes backend final
Vulpes tribes backend finalVulpes tribes backend final
Vulpes tribes backend final
Jiří Soušek
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
Loiane Groner
 
Spring boot
Spring boot Spring boot
Spring boot
Vinay Prajapati
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
Ignacio Coloma
 
Server Side Swift with Swag
Server Side Swift with SwagServer Side Swift with Swag
Server Side Swift with Swag
Jens Ravens
 
Using database in android
Using database in androidUsing database in android
Using database in android
University of Potsdam
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
Arnout Kazemier
 
Drupal Mobile
Drupal MobileDrupal Mobile
Drupal Mobile
Ruben Teijeiro
 
Foundations of a Social Application Platform
Foundations of a Social Application PlatformFoundations of a Social Application Platform
Foundations of a Social Application Platform
Jonathan LeBlanc
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
Kris Wallsmith
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
Sven Haiges
 

Similar to Retrofit Android by Chris Ollenburg (20)

External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQuery
 
A evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no androidA evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no android
 
Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
SFScon17 - Patrick Puecher: "Exploring data with Elasticsearch and Kibana"
 
Azure Mobile Services .NET Backend
Azure Mobile Services .NET BackendAzure Mobile Services .NET Backend
Azure Mobile Services .NET Backend
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Migration microservices to GraphQL
Migration microservices to GraphQLMigration microservices to GraphQL
Migration microservices to GraphQL
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4
 
Vulpes tribes backend final
Vulpes tribes backend finalVulpes tribes backend final
Vulpes tribes backend final
 
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobileJavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
JavaOne Brasil 2016: JavaEE e HTML5: da web/desktop ao mobile
 
Spring boot
Spring boot Spring boot
Spring boot
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
 
Server Side Swift with Swag
Server Side Swift with SwagServer Side Swift with Swag
Server Side Swift with Swag
 
Using database in android
Using database in androidUsing database in android
Using database in android
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 
Drupal Mobile
Drupal MobileDrupal Mobile
Drupal Mobile
 
Foundations of a Social Application Platform
Foundations of a Social Application PlatformFoundations of a Social Application Platform
Foundations of a Social Application Platform
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 

Retrofit Android by Chris Ollenburg

  • 1. Android + Retrofit + REST + JSON = Easy presented by Chris Ollenburg
  • 2. Basic REST API • Create - http://www.fakeservice.com/user • Read - http://www.fakeservice.com/user/{id} • Update - http://www.fakeservice.com/user/{id}? name={new name} • Delete - http://www.fakeservice.com/user/{id}
  • 3. public interface UserService { @POST("/user") // CREATE void createUser(@Body User user); @GET("/user/{userId}") // READ User getUser(@Path("userId") String userId); @PUT("/user/{userId}") // UPDATE void updateUserName(@Path("userId") String userId, @Body User user); @DELETE(“/user/{userId}“) // DELETE void deleteUser(@Path("userId") String userId); } public class User {
 String name; 
 Date birthDate;
 Integer age; }
  • 4. Synchronous public interface MyService {
 @GET(“/test") 
 Message getMessage(); 
 }
  • 5. Asynchronous public interface MyService { 
 @GET(“/test") 
 void getMessage(Callback<Message> msg); 
 }
  • 6. Observable / Rx public interface MyService { 
 @GET(“/test") 
 Observable<Message> getMessage(); 
 }
  • 7. Path Variables @GET("/user/{userId}") // READ User getUser(@Path("userId") String userId);
  • 8. Query Parameters @GET("/user") // READ 
 User getUser(@Query("userId") String userId);
  • 10. Request Body @PUT("/user/{userId}") 
 void updateUserName(@Path("userId") String userId, @Body User user); @Multipart @POST(“/user") void createUser(@Part("fName") String fName, @Part("lName") String lName); @FormUrlEncoded @POST(“/user") void createUser(@Field("fName") String fName, @Field("lName") String lName);
  • 11. Sync Call public void sendSync() { 
 RestAdapter restAdapter = new RestAdapter.Builder() 
 .setEndpoint(“http://www.myservice.com") 
 .build(); 
 DemoService service = restAdapter.create(DemoService.class); 
 
 DemoModel rep = service.getMessage(apiId); // Do something w/ object 
 }
  • 12. Async Call public void sendAsyncButton() { 
 service.getMessage(apiId, new Callback<DemoModel>() { 
 @Override 
 public void success(DemoModel demoModel, retrofit.client.Response response) { 
 MainActivity.this.contentView.setText(demoModel.message); 
 } 
 
 @Override 
 public void failure(RetrofitError error) { Log.e(TAG, error.getMessage()); 
 MainActivity.this.contentView.setText(error.getMessage()); 
 } 
 }); 
 }
  • 13. Call w/ RxJava public void sendRx() { 
 Observable<DemoModel> observable = service.getMessageRx(apiId); observable.subscribe(new Action1<DemoModel>() { 
 @Override 
 public void call(DemoModel demoModel) { 
 MainActivity.this.contentView.setText(demoModel.message); 
 } 
 }); 
 }
  • 14. Extras • Adjustable logging level • Custom GSON deserializers • Custom deserializer • Custom Http Client Implementations
  • 16. Demo
  • 17. Chris Ollenburg • Principal Consultant at Cardinal Solutions
 collenburg@cardinalsolutions.com
 @cardinalNow 
 www.cardinalsolutions.com • CTO of Taggd’ LLC 
 chris@taggdstreetart.com
 www.taggdstreetart.com
 Links http://square.github.io/retrofit/ https://github.com/OllenDev/RetrofitDemo