Weitere ähnliche Inhalte
Ähnlich wie Retrofit 2 - O que devemos saber (20)
Kürzlich hochgeladen (20)
Retrofit 2 - O que devemos saber
- 3. 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);
}
}
- 4. 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);
}
}
- 5. 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);
}
}
- 6. 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=...
- 7. 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);
}
}
- 8. 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);
}
}
- 9. 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);
}
}
- 10. 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=...
- 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();
- 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();
}
- 23. 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();
- 24. 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();
- 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();