SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
Retrofit

- REST client abstraction based on OkHttp
(HTTP client for Android)
- by Square (Jake Wharton)
Retrofit
●

Turns yor REST API into a Java interface
Retrofit example
●

Twitter API

●

Obtain token
Twitter – obtain token request
●

Host: https://api.twitter.com

●

POST /oauth2/token

●

Headers
–
–

●

Authorization: base64 encoded consumer key +
consumer secret
Content-Type: application/x-www-form-urlencoded;
charset=UTF-8

Body: “grant_type=client_credentials”
Twitter – obtain token response
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAA
DryUgAAAAAAzGlUwrr%2B7WL%2FWUvOMLdsQapBr9w
%3DVdxZkBQC1tfFSRFrs8J2AklGAJO9xxuo6tMz0t7wUDo2nV3NIn"}
Retrofit
public interface TwitterService {
@FormUrlEncoded
@POST("/oauth2/token")
TwitterTokenType getToken(
@Header("Authorization") String authorization,
@Field("grant_type") String grantType
);
}
Retrofit - async
@FormUrlEncoded
@POST("/oauth2/token")
void getToken(
@Header("Authorization") String authorization,
@Field("grant_type") String grantType,
Callback<TwitterTokenType> response
);
Retrofit – initialize service
RestAdapter.Log log = new RestAdapter.Log() {
@Override
public void log(String s) {
Log.d("DARIO", s);
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer(Constants.TWITTER_API_URL)
.setLog(log)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
service = restAdapter.create(TwitterService.class);
Retrofit – send request

service.getToken("Basic " +
getBase64String(Constants.BEARER_TOKEN_CREDENTIALS),
"client_credentials", callback);
Retrofit - callback
private Callback<TwitterTokenType> callback = new
Callback<TwitterTokenType>() {
@Override
public void success(TwitterTokenType token, Response response) {
Toast.makeText(HomeActivity.this, token.getAccessToken(),
Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError retrofitError) {
Toast.makeText(HomeActivity.this, "Retrofit error",
Toast.LENGTH_SHORT).show();
}
};
Retrofit - GET
@GET("/1.1/statuses/user_timeline.json")
void getUserTimeline(
@Header("Authorization") String authorization,
@Query("count") int count,
@Query("screen_name") String screenName,
Callback<Object> response
);
Retrofit exampel on Github
●

https://github.com/infinum/RetrofitExample

Weitere ähnliche Inhalte

Andere mochten auch

Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberRetrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberBruno Vieira
 
Retrofit
RetrofitRetrofit
Retrofitbresiu
 
Android word press + woocommerce rest api
Android word press + woocommerce rest apiAndroid word press + woocommerce rest api
Android word press + woocommerce rest apiJose Berna
 
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API 안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API Gosu Ok
 
大鱼架构演进
大鱼架构演进大鱼架构演进
大鱼架构演进Jun Liu
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Andres Almiray
 
Open api개발을 위한 자료1
Open api개발을 위한 자료1Open api개발을 위한 자료1
Open api개발을 위한 자료1도성 김
 
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기Manjong Han
 
[D2 오픈세미나]4.네이티브앱저장통신
[D2 오픈세미나]4.네이티브앱저장통신[D2 오픈세미나]4.네이티브앱저장통신
[D2 오픈세미나]4.네이티브앱저장통신NAVER D2
 
Seismic retrofit methods
Seismic retrofit methodsSeismic retrofit methods
Seismic retrofit methodsPaul McMullin
 
Staying Afloat with Buoy: A High-Performance HTTP Client
Staying Afloat with Buoy: A High-Performance HTTP ClientStaying Afloat with Buoy: A High-Performance HTTP Client
Staying Afloat with Buoy: A High-Performance HTTP Clientlpgauth
 
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...Brian Campbell
 

Andere mochten auch (13)

Retrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saberRetrofit 2 - O que devemos saber
Retrofit 2 - O que devemos saber
 
Retrofit
RetrofitRetrofit
Retrofit
 
Android word press + woocommerce rest api
Android word press + woocommerce rest apiAndroid word press + woocommerce rest api
Android word press + woocommerce rest api
 
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API 안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API
안드로이드 OAuth 1.0a, 2.0 구현 - Naver, Google API
 
大鱼架构演进
大鱼架构演进大鱼架构演进
大鱼架构演进
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
 
Open api개발을 위한 자료1
Open api개발을 위한 자료1Open api개발을 위한 자료1
Open api개발을 위한 자료1
 
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기
Flask, Redis, Retrofit을 이용한 Android 로그인 서비스 구현하기
 
[D2 오픈세미나]4.네이티브앱저장통신
[D2 오픈세미나]4.네이티브앱저장통신[D2 오픈세미나]4.네이티브앱저장통신
[D2 오픈세미나]4.네이티브앱저장통신
 
Seismic retrofit methods
Seismic retrofit methodsSeismic retrofit methods
Seismic retrofit methods
 
Staying Afloat with Buoy: A High-Performance HTTP Client
Staying Afloat with Buoy: A High-Performance HTTP ClientStaying Afloat with Buoy: A High-Performance HTTP Client
Staying Afloat with Buoy: A High-Performance HTTP Client
 
OAuth2.0
OAuth2.0OAuth2.0
OAuth2.0
 
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
OAuth 2.0 and Mobile Devices: Is that a token in your phone in your pocket or...
 

Mehr von Denis_infinum

Infinum Android Talks #04 - Google Maps Android API utility library
Infinum Android Talks #04 - Google Maps Android API utility libraryInfinum Android Talks #04 - Google Maps Android API utility library
Infinum Android Talks #04 - Google Maps Android API utility libraryDenis_infinum
 
Infinum Android Talks #04 - Android Lint
Infinum Android Talks #04 - Android LintInfinum Android Talks #04 - Android Lint
Infinum Android Talks #04 - Android LintDenis_infinum
 
Infinum Android Talks #04 - CouchBase Lite
Infinum Android Talks #04 - CouchBase LiteInfinum Android Talks #04 - CouchBase Lite
Infinum Android Talks #04 - CouchBase LiteDenis_infinum
 
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...Denis_infinum
 
Android Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersAndroid Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersDenis_infinum
 
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...Denis_infinum
 

Mehr von Denis_infinum (6)

Infinum Android Talks #04 - Google Maps Android API utility library
Infinum Android Talks #04 - Google Maps Android API utility libraryInfinum Android Talks #04 - Google Maps Android API utility library
Infinum Android Talks #04 - Google Maps Android API utility library
 
Infinum Android Talks #04 - Android Lint
Infinum Android Talks #04 - Android LintInfinum Android Talks #04 - Android Lint
Infinum Android Talks #04 - Android Lint
 
Infinum Android Talks #04 - CouchBase Lite
Infinum Android Talks #04 - CouchBase LiteInfinum Android Talks #04 - CouchBase Lite
Infinum Android Talks #04 - CouchBase Lite
 
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...
Infinum Android Talks #04 - How to publish an Android archive (.aar) to Maven...
 
Android Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersAndroid Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and Developers
 
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...
Infinum iOS Talks S01E02 - Things every iOS developer should know about Core ...
 

Kürzlich hochgeladen

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Kürzlich hochgeladen (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Infinum Android talks 2013-11-28 - Retrofit

  • 1. Retrofit - REST client abstraction based on OkHttp (HTTP client for Android) - by Square (Jake Wharton)
  • 2. Retrofit ● Turns yor REST API into a Java interface
  • 4. Twitter – obtain token request ● Host: https://api.twitter.com ● POST /oauth2/token ● Headers – – ● Authorization: base64 encoded consumer key + consumer secret Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Body: “grant_type=client_credentials”
  • 5. Twitter – obtain token response {"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAA DryUgAAAAAAzGlUwrr%2B7WL%2FWUvOMLdsQapBr9w %3DVdxZkBQC1tfFSRFrs8J2AklGAJO9xxuo6tMz0t7wUDo2nV3NIn"}
  • 6. Retrofit public interface TwitterService { @FormUrlEncoded @POST("/oauth2/token") TwitterTokenType getToken( @Header("Authorization") String authorization, @Field("grant_type") String grantType ); }
  • 7. Retrofit - async @FormUrlEncoded @POST("/oauth2/token") void getToken( @Header("Authorization") String authorization, @Field("grant_type") String grantType, Callback<TwitterTokenType> response );
  • 8. Retrofit – initialize service RestAdapter.Log log = new RestAdapter.Log() { @Override public void log(String s) { Log.d("DARIO", s); } }; RestAdapter restAdapter = new RestAdapter.Builder() .setServer(Constants.TWITTER_API_URL) .setLog(log) .setLogLevel(RestAdapter.LogLevel.FULL) .build(); service = restAdapter.create(TwitterService.class);
  • 9. Retrofit – send request service.getToken("Basic " + getBase64String(Constants.BEARER_TOKEN_CREDENTIALS), "client_credentials", callback);
  • 10. Retrofit - callback private Callback<TwitterTokenType> callback = new Callback<TwitterTokenType>() { @Override public void success(TwitterTokenType token, Response response) { Toast.makeText(HomeActivity.this, token.getAccessToken(), Toast.LENGTH_LONG).show(); } @Override public void failure(RetrofitError retrofitError) { Toast.makeText(HomeActivity.this, "Retrofit error", Toast.LENGTH_SHORT).show(); } };
  • 11. Retrofit - GET @GET("/1.1/statuses/user_timeline.json") void getUserTimeline( @Header("Authorization") String authorization, @Query("count") int count, @Query("screen_name") String screenName, Callback<Object> response );
  • 12. Retrofit exampel on Github ● https://github.com/infinum/RetrofitExample