Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

FiiPractic 2015 - Adroid Pro - Day 3 - API Day

363 visualizaciones

Publicado el

FiiPractic 2015 - Adroid Pro - Day 3 - API Day

Publicado en: Software
  • Sé el primero en comentar

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.

×