Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A modern approach to using RESTful APIs in Andr...

A modern approach to using RESTful APIs in Android applications

Workshop @ DevTalks for Juniors
November 12, 2016
Bucharest, Romania

The goal of the workshop was to provide insight into modern and efficient ways of connecting Android applications to servers by making HTTP requests to RESTful APIs. We started with a base project in which we replaced the HttpUrlConnection client with OkHttp, the manual deserialisation with GSON, and then we added Retrofit.

The workshop was addressed to participants who had:
Practical knowledge of object-oriented programming
Practical knowledge of the Java programming language
Android Studio installed on their machine
An Android device or emulator

Avatar for Eduard Vasilache

Eduard Vasilache

November 12, 2016

Other Decks in Technology

Transcript

  1. • Platform API ◦ String.replace(char oldChar, char newChar); • Library

    API ◦ Picasso.with(this).load(url).resize(50, 50).into(imageView); • Web API ◦ https://api.linkedin.com/v1/companies/1337 Examples of APIs
  2. • The client sends requests to the server and the

    server sends back responses • Verbs ◦ GET - read a resource ◦ POST/PUT - create or update a resource ◦ DELETE - delete a resource ◦ HEAD, CONNECT, OPTIONS, TRACE, PATCH... HTTP
  3. • Status response codes ◦ 1xx - Informational ◦ 2xx

    - Success (e.g. 200 OK) ◦ 3xx - Redirection ◦ 4xx - Client Error (e.g. 404 Not Found) ◦ 5xx - Server Error (e.g. 500 Internal Server Error) HTTP
  4. GET /index.html HTTP/1.1 Host: www.example.com HTTP/1.1 200 OK Date: Mon,

    23 May 2005 22:38:34 GMT ... Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) <html><head><title>An Example Page</title></head><body>...</body></html> HTTP
  5. • Set of conventions for designing Web APIs • Web

    APIs that follow REST conventions are called RESTful APIs REST
  6. • Takes advantage of HTTP verbs and status codes GET

    /tickets - Retrieves a list of tickets GET /tickets/12 - Retrieves a specific ticket POST /tickets - Creates a new ticket PUT /tickets/12 - Updates ticket #12 PATCH /tickets/12 - Partially updates ticket #12 DELETE /tickets/12 - Deletes ticket #12 REST
  7. • Uses a logical hierarchy of resources GET /tickets/12/messages -

    Retrieves list of messages for ticket #12 GET /tickets/12/messages/5 - Retrieves message #5 for ticket #12 POST /tickets/12/messages - Creates a new message in ticket #12 PUT /tickets/12/messages/5 - Updates message #5 for ticket #12 PATCH /tickets/12/messages/5 - Partially updates message #5 for ticket #12 DELETE /tickets/12/messages/5 - Deletes message #5 for ticket #12 REST
  8. • Treats resources as nouns, not verbs ◦ https://api.paypal.com/v1/payments/orders/12345 •

    Uses versioning ◦ https://api.instagram.com/v1/users/232192182 • Has consistency REST
  9. • JavaScript Object Notation • Lightweight human readable format for

    transferring data • Attribute–value pairs • Limited data types ◦ number, string, boolean, array, object, null JSON
  10. JSON { "name": "DevTalks for Juniors 2016", "description": "DevTalks for

    Juniors is the small sister event of DevTalks Romania.", "isInteresting": true, "location": { "city": "Bucharest", "country": "Romania", "streetNumber": 5, "zipCode": null }, "workshops": [ { "name": "Introduction to JSON", "participants": [] }, { "name": "A modern approach to using RESTful APIs in Android applications", "participants": [] } ] }