230 likes | 339 Vues
Don't miss out on the Homecoming Engineering Tailgate Extravaganza! Students need to register by October 21st for an exciting day, including the game on October 29th. Enjoy delicious lunch for $10 or $15 (including lunch and a ticket). Only 300 tickets are available! Get your t-shirts and pay your dues of $10 to receive a free t-shirt. Additionally, don’t forget to attend the upcoming speakers series featuring Dr. Gary Leavens and learn more about APIs with Shane Chism on October 20th!
E N D
Announcements • Homecoming Engineering Tailgate Extravaganza • Students need to register by October 21st • Actual game is October 29th • $10 Lunch, $15 for Lunch + Ticket • 300 tickets available • Now selling t-shirts and dues • $10.00 Dues = Free T-Shirt!
Announcements • Speakers • Dr. Gary Leavens • Next week! • Florida Interactive Entertainment Academy • October 20th • Then social!
APIs and You! Shane Chism, 2011
What’s an API? • Application Programming Interface • Serves as an interface between different software programs and facilitates their interaction • (I want your data) + (You don’t trust me) = API • Implements multiple concepts • Object based requests • JSON / XML • Authentication Shane Chism, 2011
How is a request made? • Multiple methods of making an API request • A request can simply be defined: • Client communicates a request to a Server • For our purposes, we’ll focus on URL based requests Example: GET https://graph.facebook.com/{userId} Shane Chism, 2011
Object Based Requests • APIs can be simple or powerful • Consider an API that only serves the time • Consider an API that translates to and from any language • Object Oriented requests make requesting complex data easy: GET https://graph.facebook.com/chism/posts • This returns all posts for user “chism” • Our main object is the user (chism). posts is our “connection,” a collection of child objects Shane Chism, 2011
How do I use this data? • The API server has no clue what context you’re requesting from • Web app? Desktop app? C? C#? PHP? Java? Whitespace? • It needs to send a response that can be read by any language in any context • Solution: XML!Right? …. Right? …….. Shane Chism, 2011
JSON • XML is cool. • It’s traditionally been used to do what JSON is doing • Send data in a way where we can convert it into usable data structures • JSON is cooler. (opinion, but backed with facts!) • It does what XML does, but it’s much lighter weight Shane Chism, 2011
JSON • Computers can read JSON fairly easily • Getting the data into the data structure, however, isn’t as fun • In PHP: • json_decode( $jsonString, true ); • Produces an associative array with proper variable types • In Java: …. Not as fun. Shane Chism, 2011
JSON • Decoding JSON in Java • You can use any number of open source libraries • We’ll be using GSON • Google JSON Decoder • The trick: We need to build a container for our JSON data Shane Chism, 2011
JSON • Example: Decoding JSON in Java • Suppose this is our JSON response: • Our data structure would be: { "id": "1106460174" } classmy_JSON_Container { public String id; } Shane Chism, 2011
JSON • Example: Decoding JSON in Java • Suppose this is our JSON response: • Our data structure would be: { "id": "1106460174”, "name": "Shane Chism", } classmy_JSON_Container { public String id; public String name; } Shane Chism, 2011
Reading JSON { "id": "1106460174", "name": "Shane Chism", "link": "https://www.facebook.com/chism", "username": "chism", "hometown": { "id": "109466215738919", "name": "Tierra Verde, Florida" }, "languages": [ { "id": "117976438273549", "name": "SQL" }, { "id": "177492448957188", "name": "CSharp" } ] } Strings String key = value
Reading JSON { "id": "1106460174", "name": "Shane Chism", "link": "https://www.facebook.com/chism", "username": "chism", "hometown": { "id": "109466215738919", "name": "Tierra Verde, Florida" }, "languages": [ { "id": "117976438273549", "name": "SQL" }, { "id": "177492448957188", "name": "CSharp" } ] } Arrays Map<String,String>
Reading JSON { "id": "1106460174", "name": "Shane Chism", "link": "https://www.facebook.com/chism", "username": "chism", "hometown": { "id": "109466215738919", "name": "Tierra Verde, Florida" }, "languages": [ { "id": "117976438273549", "name": "SQL" }, { "id": "177492448957188", "name": "CSharp" } ] } Nested Arrays Collection<Map<String,String>>
Making the connection • In Java we use two pre-built classes: • URL • URLConnection • More on this in the tutorial… Shane Chism, 2011
OAUTH Api authentication
Authentication • Often times an API will need to know that we have permission to access user data • Google, Facebook (and many others) use a protocol called OAUTH • This is extremely important, but we won’t be tackling it in this tutorial • At first blush OAuth is very complex. Pace yourself! Shane Chism, 2011
APIs and You! Shane Chism, 2011