12
Retrofit 2.0 KRISTIJ AN JURKOVIC

Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

  • Upload
    infinum

  • View
    269

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

Retrofit 2.0

KRISTIJAN JURKOVIC

Page 2: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

WHAT IS RETROFIT?

Page 3: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

USAGE

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'

Page 4: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

WHAT’S DIFFERENT?com.squareup.retrofit > com.squareup.retrofit2

Converters

Rest interface methods return Call<T>

OkHttp is required

Security exception if no Internet permission

RestAdapter > Retrofit

Page 5: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

CALL<T>call.enqueue(new Callback<T> { … })

call.execute();

call.cancel();

Page 6: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

ONRESPONSEresponse.isSuccess();

response.code();

response.body();

response.errorBody().string();

Page 7: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

URL HANDLINGsame as web <a href=“…” /> handling

@GET(“blog”)

Base url: http://www.infinum.co/api/v1

Result: http://www.infinum.co/api/blog

Base url: http://www.infinum.co/api/v1/

Result: http://www.infinum.co/api/v1/blog

Page 8: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

URL HANDLING@GET(“/blog”)

Base url: http://www.infinum.co/api/v1

Result: http://www.infinum.co/blog

@GET(“http://www.infinum.co/api/v1/blog”)

@GET

void get(@Url String url);

Page 9: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

LOG

Page 10: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

CONVERTERS.addConverterFactory({pass in converter here});

Gson: com.squareup.retrofit:converter-gsonJackson: com.squareup.retrofit:converter-jacksonMoshi: com.squareup.retrofit:converter-moshiProtobuf: com.squareup.retrofit:converter-protobufWire: com.squareup.retrofit:converter-wireSimple XML: com.squareup.retrofit:converter-simplexml

Converter.Factory interface

Page 11: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

CONVERTERS.addConverterFactory({pass in converter here});

Gson gson = new GsonBuilder() ... add type converters here .create();

Retrofit retrofit = new Retrofit.Builder() .baseUrl(“http://www.infinum.co/api/”) .addConverterFactory(GsonConverterFactory.create(gson)) .build();

Page 12: Infinum Android Talks #16 - Retrofit 2 by Kristijan Jurkovic

Any questions? KRISTIJ[email protected] @KJURKOVIC

Visit infinum.co or find us on social networks:

infinum.co infinumco infinumco infinum