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: ...
Twitter – obtain token response
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAA
DryUgAAAAAAzGlUwrr%2B7WL%2FWU...
Retrofit
public interface TwitterService {
@FormUrlEncoded
@POST("/oauth2/token")
TwitterTokenType getToken(
@Header("Auth...
Retrofit - async
@FormUrlEncoded
@POST("/oauth2/token")
void getToken(
@Header("Authorization") String authorization,
@Fie...
Retrofit – initialize service
RestAdapter.Log log = new RestAdapter.Log() {
@Override
public void log(String s) {
Log.d("D...
Retrofit – send request

service.getToken("Basic " +
getBase64String(Constants.BEARER_TOKEN_CREDENTIALS),
"client_credenti...
Retrofit - callback
private Callback<TwitterTokenType> callback = new
Callback<TwitterTokenType>() {
@Override
public void...
Retrofit - GET
@GET("/1.1/statuses/user_timeline.json")
void getUserTimeline(
@Header("Authorization") String authorizatio...
Retrofit exampel on Github
●

https://github.com/infinum/RetrofitExample
Upcoming SlideShare
Loading in...5
×

Infinum Android talks 2013-11-28 - Retrofit

1,584

Published on

One of the presentations from Android talks lectures, done by employees of Infinum Ltd.

Retrofit is a type-safe REST client for Android which we recently use pretty often. Dario explains Retrofit's perks and the way it can be used. (by Dario Penić Omer)

Published in: Education, Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,584
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Infinum Android talks 2013-11-28 - Retrofit

  1. 1. Retrofit - REST client abstraction based on OkHttp (HTTP client for Android) - by Square (Jake Wharton)
  2. 2. Retrofit ● Turns yor REST API into a Java interface
  3. 3. Retrofit example ● Twitter API ● Obtain token
  4. 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. 5. Twitter – obtain token response {"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAA DryUgAAAAAAzGlUwrr%2B7WL%2FWUvOMLdsQapBr9w %3DVdxZkBQC1tfFSRFrs8J2AklGAJO9xxuo6tMz0t7wUDo2nV3NIn"}
  6. 6. Retrofit public interface TwitterService { @FormUrlEncoded @POST("/oauth2/token") TwitterTokenType getToken( @Header("Authorization") String authorization, @Field("grant_type") String grantType ); }
  7. 7. Retrofit - async @FormUrlEncoded @POST("/oauth2/token") void getToken( @Header("Authorization") String authorization, @Field("grant_type") String grantType, Callback<TwitterTokenType> response );
  8. 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. 9. Retrofit – send request service.getToken("Basic " + getBase64String(Constants.BEARER_TOKEN_CREDENTIALS), "client_credentials", callback);
  10. 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. 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. 12. Retrofit exampel on Github ● https://github.com/infinum/RetrofitExample

×