retrofit 2 - o que devemos saber

Post on 12-Apr-2017

247 Views

Category:

Internet

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Retrofit 2

Bruno Vieira

O que devemos saber

New Url Pattern

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

https://api.dribbble.com/shots?access_token=...

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1/") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1/") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1/") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

New Url Pattern

public interface WebServiceApi{@GET("shots")Call<List<ShotsVO>> getShotsList(@Query("access_token") String accessToken);}

public class WebServiceManagerImpl implements WebServiceManager { private void setupApi() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.dribbble.com/v1/") .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build(); webServiceApi = retrofit.create(WebServiceApi.class); }}

https://api.dribbble.com/v1/shots?access_token=...

New Url Pattern

Dynamic URL

public interface WebServiceApi{

@GETCall<JokeVO> getAJoke(@Url String url);

}

Bippples

https://github.com/OBrunoVieira/Bippples

Dribbble+

Chuck Norris Database

https://github.com/OBrunoVieira/Bippples

Bippples

https://github.com/OBrunoVieira/Bippples

OkHttp

OkHttp

Retrofit 1.9 - Is optional

Retrofit 2.x - Is required

Importing a specific version:

compile ('com.squareup.retrofit2:retrofit:2.0.2') { exclude module: 'okhttp'}

compile 'com.squareup.okhttp3:okhttp:3.2.0'

OkHttp

Interceptors public OkHttpClient clientInterceptor() { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(this); ... return httpClient.build(); } public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder()

.addHeader("Authorization", "123454324rtxccvbfdsfgvbcx!@#!@#!@#").build(); return chain.proceed(request); }

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Environment.SERVER_URL) .client(clientInterceptor()) .build();

OkHttp

Interceptors public OkHttpClient clientInterceptor() { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(this); ... return httpClient.build(); } public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder()

.addHeader("Authorization", "123454324rtxccvbfdsfgvbcx!@#!@#!@#").build(); return chain.proceed(request); }

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Environment.SERVER_URL) .client(clientInterceptor()) .build();

OkHttp.Interceptors

No Logging

OkHttp.Interceptors

No Logging, but..

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

public OkHttpClient clientInterceptor() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(Environment.LOG_LEVEL);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(interceptor); ... return httpClient.build();}

OkHttp.Interceptors

No Logging, but..

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

public OkHttpClient clientInterceptor() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(Environment.LOG_LEVEL);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(interceptor); ... return httpClient.build();}

OkHttp.Interceptors

No Logging, but..

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

public OkHttpClient clientInterceptor() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(Environment.LOG_LEVEL);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); httpClient.addInterceptor(interceptor); ... return httpClient.build();}

Converters

Converters

No converter by default

compile "com.github.aurae.retrofit2:converter-logansquare:1.4.0"

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Environment.SERVER_URL) .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build();

Converters

No converter by default

compile "com.github.aurae.retrofit2:converter-logansquare:1.4.0"

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Environment.SERVER_URL) .addConverterFactory(LoganSquareConverterFactory.create()) .client(webServiceInterceptor.clientInterceptor()) .build();

Requests

Requests

Synchronous and Asynchronous

Call<List<ShotsVO>> call = webServiceManager.getWebServiceApiInstance().getShotsList(accessToken);List<ShotsVO> shotsList = call.execute.body;

Call<List<ShotsVO>> call = webServiceManager.getWebServiceApiInstance().getShotsList(accessToken);call.enqueue(new Callback<List<ShotsVO>>(){

@Overridepublic void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){

...}...

}

Requests

Attention!

Call<List<ShotsVO>> call = webServiceManager.getWebServiceApiInstance().getShotsList(accessToken);call.enqueue(new Callback<List<ShotsVO>>(){

@Overridepublic void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){

...}

@Overridepublic void onFailure(Call<List<ShotsVO>> call, Throwable throwable) {...}}

https://github.com/OBrunoVieira/Bippples

Requests

Cancel

Call<List<ShotsVO>> call = webServiceManager.getWebServiceApiInstance().getShotsList(accessToken);

call.enqueue(new Callback<List<ShotsVO>>(){@Overridepublic void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){

...}

@Overridepublic void onFailure(Call<List<ShotsVO>> call, Throwable throwable) {...}}

call.cancel();

Requests

Cancel

Call<List<ShotsVO>> call = webServiceManager.getWebServiceApiInstance().getShotsList(accessToken);

call.enqueue(new Callback<List<ShotsVO>>(){@Overridepublic void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){

...}

@Overridepublic void onFailure(Call<List<ShotsVO>> call, Throwable throwable) {...}}

call.cancel();

=)

Source

http://square.github.io/retrofit/

https://inthecheesefactory.com/blog/retrofit-2.0/en

http://www.iayon.com/consuming-rest-api-with-retrofit-2-0-in-android/

https://github.com/square/retrofit/blob/master/CHANGELOG.md

www.concretesolutions.com.brblog.concretesolutions.com.br

Rio de Janeiro – Rua São José, 90 – cj. 2121Centro – (21) 2240-2030

São Paulo - Rua Sansão Alves dos Santos, 433 4º andar - Brooklin - (11) 4119-0449

top related