Notes

251 views
218 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
251
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Notes

  1. 1. Comp194-MA
  2. 2. HTTP? <ul><li>HTTP stands for hyper text transport protocol. </li></ul><ul><li>HTTP is the standard transport protocol for web browser <----> web server communication. </li></ul><ul><li>One of the key features of HTTP is that communication is stateless between requests. </li></ul>
  3. 3. Why HTTP <ul><li>In the last few years (web 2.0) the availability of web services and APIs has exploded. </li></ul><ul><li>See: http://www.programmableweb.com/apis </li></ul><ul><li>The vast majority of these services are available as REST services over HTTP. </li></ul><ul><li>Because of this, using HTTP is crucial to building an application that draws data from external sources. </li></ul>
  4. 4. REST <ul><li>REST stands for representational state transfer. </li></ul><ul><li>REST is an overarching term for applications that follow a “RESTful” service model. </li></ul><ul><li>More information at: http://en.wikipedia.org/wiki/Representational_State_Transfer </li></ul>
  5. 5. Quick aside: JSON <ul><li>JSON stands for Javascript object notation. </li></ul><ul><li>It has quickly become one of the most popular formats for data exchange on the internet. </li></ul><ul><li>More info at: http://en.wikipedia.org/wiki/JSON </li></ul><ul><li>To encode/decode in Java use: </li></ul><ul><li>http://www.json.org/java/ </li></ul>
  6. 6. HTTP in Android <ul><li>HTTP access is provided by the same Java constructs as a generic Java program. </li></ul><ul><li>HTTP URLs can be accessed using the Java.net.URL class </li></ul><ul><li>The associated data can be read using a BufferedReader in conjunction with a InputStreamReader </li></ul>
  7. 7. Sample Code <ul><li>URL u; </li></ul><ul><li>BufferedReader r; </li></ul><ul><li>String data, content = &quot;&quot;; </li></ul><ul><li>try { </li></ul><ul><li>u = new URL(myUrl); </li></ul><ul><li>r = new BufferedReader(new InputStreamReader(u.openStream())); </li></ul><ul><li>while( (data = r.readLine()) != null) { </li></ul><ul><li>content += data; </li></ul><ul><li>} </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>Log. e(TAG, &quot;&quot;, e); </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul>
  8. 8. Easy to use HTTP APIs <ul><li>Yahoo! Boss (search API): </li></ul><ul><ul><li>http://developer.yahoo.com/search/boss/ </li></ul></ul><ul><li>Flickr (photo API – see week 1 code): </li></ul><ul><ul><li>http://www.flickr.com/services/api/ </li></ul></ul><ul><li>Twitter (micro blogging): </li></ul><ul><ul><li>http://apiwiki.twitter.com/ </li></ul></ul><ul><li>These APIs respond with JSON data. See slide 5 for information. </li></ul>

×