2. ● 20 minutos! (run, Forest, RUN!)
● Objetivo: mostrar uma lib útil e simples p/ desenv Android
● Problema → Retrofit → Vantagens → Ex → The End
Agenda
7. URL url;
HttpURLConnection urlConnection =null;
try {
url = new URL("http://api.mybooks.com/");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
O Problema
HTTP Request
- Código grande
- Muito técnico
- Muito longe do
problema da aplicação ISSO SEM
TRATAR OS
ERROS!
10. ● A type-safe REST* client for Android and Java
○ menos código, mais focado no seu modelo de domínio
● Criada pela Square, Inc.
● Open-Source e gratuita
● http://square.github.io/retrofit/
Retrofit - uma mão na roda!
22. ● comunicação com webservices HTTP+JSON facilitada
● parsing JSON <-> POJO automática
● simplicidade de código
○ + fácil de entender e manter
○ - chance de bugs
● pode ser extendido facilmente
● e mais…
Retrofit - vantagens!
23. ● Outras features:
○ logging automático de todas as requests
○ manipulação de headers (cache, autorização, etc)
○ conversores personalizados com GsonConverter
○ tratamento de erros facilitado
○ integração com RxJava.
Retrofit - vantagens!
27. Retrofit - exemplo rapidinho!
{
"url": "http://apod.nasa.gov/apod/image/1507/Jowisz_i_Wenus2Tomaszewski1024.jpg",
"media_type": "image",
"explanation": "On June 30 Venus and Jupiter were actually far apart, but both appeared
close in western skies at dusk. Near the culmination of this year's gorgeous conjunction,
the two bright evening planets are captured in the same telescopic field of view in this
sharp digital stack of images taken after sunset from Poznań in west-central Poland.
In fact, banded gas giant [...]",
"concepts": null,
"title": "Venus and Jupiter are Far"
}
ex.: https://api.nasa.gov/planetary/apod?concept_tags=false&api_key=DEMO_KEY