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{
@GET
Call<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>>(){
@Override
public 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>>(){
@Override
public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){
...
}
@Override
public 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>>(){
@Override
public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){
...
}
@Override
public 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>>(){
@Override
public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){
...
}
@Override
public 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.br
blog.concretesolutions.com.br
Rio de Janeiro – Rua São José, 90 – cj. 2121
Centro – (21) 2240-2030
São Paulo - Rua Sansão Alves dos Santos, 433
4º andar - Brooklin - (11) 4119-0449

Retrofit 2 - O que devemos saber

  • 1.
    Retrofit 2 Bruno Vieira Oque devemos saber
  • 2.
  • 3.
    New Url Pattern publicinterface 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); } }
  • 4.
    New Url Pattern publicinterface 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); } }
  • 5.
    New Url Pattern publicinterface 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); } }
  • 6.
    New Url Pattern publicinterface 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=...
  • 7.
    New Url Pattern publicinterface 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); } }
  • 8.
    New Url Pattern publicinterface 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); } }
  • 9.
    New Url Pattern publicinterface 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); } }
  • 10.
    New Url Pattern publicinterface 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=...
  • 11.
    New Url Pattern DynamicURL public interface WebServiceApi{ @GET Call<JokeVO> getAJoke(@Url String url); }
  • 12.
  • 13.
  • 14.
  • 15.
    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'
  • 16.
    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();
  • 17.
    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();
  • 18.
  • 19.
    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(); }
  • 20.
    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(); }
  • 21.
    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(); }
  • 22.
  • 23.
    Converters No converter bydefault 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();
  • 24.
    Converters No converter bydefault 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();
  • 25.
  • 26.
    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>>(){ @Override public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){ ... } ... }
  • 27.
    Requests Attention! Call<List<ShotsVO>> call =webServiceManager.getWebServiceApiInstance().getShotsList(accessToken); call.enqueue(new Callback<List<ShotsVO>>(){ @Override public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){ ... } @Override public void onFailure(Call<List<ShotsVO>> call, Throwable throwable) { ... } } https://github.com/OBrunoVieira/Bippples
  • 28.
    Requests Cancel Call<List<ShotsVO>> call =webServiceManager.getWebServiceApiInstance().getShotsList(accessToken); call.enqueue(new Callback<List<ShotsVO>>(){ @Override public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){ ... } @Override public void onFailure(Call<List<ShotsVO>> call, Throwable throwable) { ... } } call.cancel();
  • 29.
    Requests Cancel Call<List<ShotsVO>> call =webServiceManager.getWebServiceApiInstance().getShotsList(accessToken); call.enqueue(new Callback<List<ShotsVO>>(){ @Override public void onResponse(Call<List<ShotsVO>> call, Response<List<ShotsVO>> response){ ... } @Override public void onFailure(Call<List<ShotsVO>> call, Throwable throwable) { ... } } call.cancel();
  • 30.
  • 31.
  • 32.
    www.concretesolutions.com.br blog.concretesolutions.com.br Rio de Janeiro– Rua São José, 90 – cj. 2121 Centro – (21) 2240-2030 São Paulo - Rua Sansão Alves dos Santos, 433 4º andar - Brooklin - (11) 4119-0449