Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Android Pro
API Day
– Test Android
“Aplicatia ta foloseste un API (lastfm), sa
spunem ca in format JSON. Cum procedezi sa
consumi aceste date?”
Http?
REST?
Request + Raspuns
[Header]+[Body] ——URL—> [Server]
[Header]+[Body] <————— [Server]
^JSON
Exemplu
http://rest-service.guides.spring.io/greeting
{
"content": "Hello, World!",
"id": 3123
}
JSON?
JSONObject jObj = new JSONObject(data);
Greeting greeting = new Greeting();
greeting.content = jObj.getString(“conte...
Http?
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
ByteA...
Gson
class Greeting{
String content;
long id;
}
Gson gson = new Gson();
Greeting greeting = gson.fromJson(response, Greeti...
Jackson
class Greeting{
String content;
long id;
}
ObjectMapper mapper = new ObjectMapper();
Greeting greeting = mapper.re...
Retrofit
interface GreetService {
@GET("/greeting")
Greeting getGreeting();
@GET("/altceva")
Altceva getAltceva();
}
RestA...
Retrofit
parametri
http://rest-service.guides.spring.io/greeting?name=Bob
public interface GreetService {
@GET("/greeting"...
Retrofit
Async
public interface GreetService {
@GET("/greeting")
void getGreeting(@Query("name") name, Callback<Greeting>
...
Retrofit
@GET, @POST, @PUT, @DELETE, @HEAD
@Query, @Path, @Body, @Header
@Headers
…
Spring Android
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2...
Feign
interface GreetService {
@GET("/greeting")
Greeting getGreeting();
}
GS gs = Feign.builder()
.decoder(new GsonDecode...
Volley, Retrofit, Ion (koush) si alte librarii
similare trateaza mai mult partea de networking decat
cea de REST si discut...
Work
Level 1:
- Incarcati greeting de pe: http://rest-
service.guides.spring.io/greeting
Level 2:
- Faceti un consumator pentru...
Prochain SlideShare
Chargement dans…5
×

FiiPractic 2015 - Adroid Pro - Day 3 - API Day

358 vues

Publié le

FiiPractic 2015 - Adroid Pro - Day 3 - API Day

Publié dans : Logiciels
  • Soyez le premier à commenter

FiiPractic 2015 - Adroid Pro - Day 3 - API Day

  1. 1. Android Pro API Day
  2. 2. – Test Android “Aplicatia ta foloseste un API (lastfm), sa spunem ca in format JSON. Cum procedezi sa consumi aceste date?”
  3. 3. Http? REST?
  4. 4. Request + Raspuns [Header]+[Body] ——URL—> [Server] [Header]+[Body] <————— [Server] ^JSON
  5. 5. Exemplu http://rest-service.guides.spring.io/greeting { "content": "Hello, World!", "id": 3123 }
  6. 6. JSON? JSONObject jObj = new JSONObject(data); Greeting greeting = new Greeting(); greeting.content = jObj.getString(“content"); greeting.id = jObj.getLong("id");
  7. 7. Http? HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(URL)); ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); responseString = out.toString(); out.close();
  8. 8. Gson class Greeting{ String content; long id; } Gson gson = new Gson(); Greeting greeting = gson.fromJson(response, Greeting.class);
  9. 9. Jackson class Greeting{ String content; long id; } ObjectMapper mapper = new ObjectMapper(); Greeting greeting = mapper.readValue(responseString, Greeting.class);
  10. 10. Retrofit interface GreetService { @GET("/greeting") Greeting getGreeting(); @GET("/altceva") Altceva getAltceva(); } RestAdapter restAdapter = new RestAdapter.Builder() .setServer("http://rest-service.guides.spring.io") .build(); GreetService greetService = restAdapter.create(GreetService.class);
  11. 11. Retrofit parametri http://rest-service.guides.spring.io/greeting?name=Bob public interface GreetService { @GET("/greeting") Greeting getGreeting(@Query(“name") String name); }
  12. 12. Retrofit Async public interface GreetService { @GET("/greeting") void getGreeting(@Query("name") name, Callback<Greeting> callback); }
  13. 13. Retrofit @GET, @POST, @PUT, @DELETE, @HEAD @Query, @Path, @Body, @Header @Headers …
  14. 14. Spring Android RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); Greeting greeting = restTemplate.getForObject(url, Greeting.class);
  15. 15. Feign interface GreetService { @GET("/greeting") Greeting getGreeting(); } GS gs = Feign.builder() .decoder(new GsonDecoder()) .target(GreetService.class, “https://…"); gs.getGreeting();
  16. 16. Volley, Retrofit, Ion (koush) si alte librarii similare trateaza mai mult partea de networking decat cea de REST si discutam la cursul 4 despre toate.
  17. 17. Work
  18. 18. Level 1: - Incarcati greeting de pe: http://rest- service.guides.spring.io/greeting Level 2: - Faceti un consumator pentru GitHub, Flickr sau LastFM. - Implementati autentificarea pentru serviciul ales (Nu ma refer la un ecran de login, hardcodati credentialele) Level badass: - Consumati un serviciu pe format XML - Folositi adnotari Jackson ca sa aveti un denumiri diferite in POJO fata de JSON - Extra bonus la tabla, referitor la cast automat.

×