Infinum Android talks 2013-11-28 - Retrofit
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Infinum Android talks 2013-11-28 - Retrofit

on

  • 1,494 views

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

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)

Statistics

Views

Total Views
1,494
Views on SlideShare
1,368
Embed Views
126

Actions

Likes
3
Downloads
12
Comments
0

1 Embed 126

http://www.coffeedrivendev.com 126

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Infinum Android talks 2013-11-28 - Retrofit Presentation Transcript

  • 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
  • 3. Retrofit example ● Twitter API ● Obtain token
  • 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