cs 10 problem solving via object oriented programming
play

CS 10: Problem solving via Object Oriented Programming - PowerPoint PPT Presentation

CS 10: Problem solving via Object Oriented Programming Winter 2017 Tim Pierson 260 (255) Sudikoff Day 21 Web Services Big picture: query


  1. CS ¡10: ¡ Problem ¡solving ¡via ¡Object ¡Oriented ¡ Programming ¡ Winter ¡2017 ¡ ¡ Tim ¡Pierson ¡ 260 ¡(255) ¡Sudikoff ¡ Day ¡21 ¡– ¡Web ¡Services ¡

  2. Big ¡picture: ¡query ¡Flickr ¡and ¡display ¡results ¡ Overview ¡ Give ¡me ¡pictures ¡of ¡cats ¡ Your ¡ computer ¡ Flickr ¡photo ¡ Click ¡next ¡ database ¡ Ok, ¡here ¡you ¡go ¡ Click ¡next… ¡ 2 ¡

  3. Agenda ¡ 1. Graphical ¡user ¡interface ¡ 2. GeUng ¡stuff ¡from ¡the ¡web ¡ 3. Web ¡services ¡ 4. Processing ¡XML ¡ 5. Finished ¡product ¡ 3 ¡

  4. Crea[ng ¡Graphical ¡User ¡Interfaces ¡(GUIs) ¡ involves ¡graphical ¡elements ¡and ¡listeners ¡ 1. ¡Graphical ¡elements ¡are ¡items ¡on ¡the ¡screen ¡the ¡ user ¡can ¡interact ¡with ¡ • Found ¡in ¡Abstract ¡Window ¡Toolkit ¡(AWT) ¡and ¡ Swing ¡libraries ¡ ¡ • Provide ¡a ¡wide ¡variety ¡of ¡items ¡such ¡as ¡ bu^ons, ¡text ¡fields, ¡combo ¡boxes ¡ • Pla`orm ¡(e.g., ¡Windows, ¡Mac) ¡and ¡device ¡ independent ¡ 2. Listeners ¡respond ¡to ¡user ¡input ¡such ¡as ¡clicking ¡ or ¡entering ¡text ¡ 4 ¡

  5. Java ¡graphical ¡elements ¡consists ¡of ¡ Containers ¡and ¡Components ¡ Components ¡ Containers ¡ JTextField ¡ JComboBox ¡ JBu^on ¡ JFrame ¡ JPanel ¡ • Containers ¡ can ¡hold ¡ JComponent ¡ other ¡ containers ¡or ¡ components ¡ • May ¡be ¡ nested ¡ 5 ¡

  6. Listeners ¡allow ¡us ¡to ¡capture ¡user ¡ interac[on ¡with ¡graphical ¡elements ¡ // create button control JButton search = new new JButton JButton("search" "search"); ); //add listener if action taken on button (e.g., clicked) search.addActionListener(new new AbstractAction AbstractAction() { () { public void public void actionPerformed actionPerformed(ActionEvent ActionEvent e) { ) { // this will run if action taken on button System. out out.println .println("search button” "search button”); ); } }); ¡ Listeners ¡are ¡called ¡back ¡when ¡event ¡fires ¡ Located ¡in ¡awt.event.* ¡(import ¡this) ¡ ¡ 6 ¡

  7. Events ¡call ¡back ¡listeners ¡ Event ¡life ¡cycle ¡ // create button control JButton search = new new JButton JButton("search" "search"); ); //add listener if action taken on button (e.g., clicked) search.addActionListener(new new AbstractAction AbstractAction() { () { public public void void actionPerformed actionPerformed(ActionEvent ActionEvent e) { ) { //must be implemented to get call back // this will run if action taken on button System. out out.println .println("search button” "search button”); ); } 7 ¡ }); ¡ Image: ¡h^p://www3.ntu.edu.sg/home/ehchua/programming/java/j4a_gui.html ¡ ¡

  8. Crea[ng ¡a ¡Graphical ¡User ¡Interface ¡in ¡Java ¡ is ¡tedious ¡without ¡a ¡GUI ¡development ¡tool ¡ FlickrSearchCore.java ¡ • Run ¡to ¡show ¡what ¡we ¡are ¡trying ¡to ¡accomplish ¡– ¡windows ¡with ¡a ¡ few ¡bu^ons, ¡text ¡entry, ¡and ¡drop ¡down ¡box ¡(otherwise ¡window ¡is ¡ blank, ¡photos ¡from ¡Flickr ¡will ¡go ¡in ¡main ¡window ¡por[on) ¡ • Moving ¡away ¡from ¡ DrawingGUI , ¡puUng ¡GUI ¡development ¡in ¡this ¡ file ¡ • You ¡can ¡hand ¡code ¡GUI ¡layouts, ¡but ¡*far* ¡easier ¡to ¡use ¡a ¡GUI ¡ design ¡tool, ¡here ¡we ¡do ¡it ¡by ¡hand ¡ • FlickrSearchCore ¡extends ¡ JFrame , ¡Java’s ¡graphical ¡window ¡class ¡ • Constructor ¡creates ¡a ¡new ¡canvas ¡of ¡type JComponent , ¡point ¡out ¡ the ¡use ¡of ¡anonymous ¡class ¡inside ¡new ¡ JComponent ContentPane is ¡the ¡main ¡container, ¡ canvas ¡holds ¡the ¡pictures ¡ • from ¡Flickr ¡in ¡the ¡container, ¡ gui holds ¡bu^ons, ¡etc ¡at ¡the ¡top ¡ ¡ • Most ¡setup ¡occurs ¡in ¡ setupGUI ¡method ¡ 8 ¡

  9. Crea[ng ¡a ¡Graphical ¡User ¡Interface ¡in ¡Java ¡ is ¡tedious ¡without ¡a ¡GUI ¡development ¡tool ¡ FlickrSearchCore.java ¡– ¡setupGUI ¡method ¡ • Creates ¡bu^on ¡called ¡ prevB and ¡adds ¡a ¡listener ¡for ¡bu^on ¡events ¡ • Same ¡thing ¡for ¡ nextB • Creates ¡ JComboBox ¡with ¡ sorts ¡op[ons ¡with ¡listener ¡for ¡events ¡to ¡ allow ¡user ¡to ¡specify ¡how ¡to ¡sort ¡images ¡(relevance, ¡date, ¡etc) ¡ • Creates ¡ JTextField ¡to ¡allow ¡user ¡to ¡input ¡search ¡criteria ¡ • Adds ¡search ¡bu^on ¡with ¡listener ¡ • Package ¡above ¡components ¡into ¡a ¡ Panel • Finally, ¡add ¡ canvas ¡and ¡ gui ¡to ¡ ContentPane ¡for ¡window ¡ 9 ¡

  10. Agenda ¡ 1. Graphical ¡user ¡interface ¡ 2. GeUng ¡stuff ¡from ¡the ¡web ¡ 3. Web ¡services ¡ 4. Processing ¡XML ¡ 5. Finished ¡product ¡ 10 ¡

  11. To ¡transfer ¡data ¡between ¡computers ¡we ¡ use ¡pre-­‑defined ¡protocols ¡ Network ¡protocols ¡ • Protocols ¡define ¡up ¡front ¡how ¡data ¡will ¡be ¡exchanged ¡so ¡everyone ¡ knows ¡the ¡“rules” ¡ • There ¡are ¡dozens ¡of ¡protocols ¡used ¡for ¡different ¡purposes: ¡ • TCP/IP, ¡FTP ¡ • Wi-­‑Fi, ¡Bluetooth ¡ • HyperText ¡Transfer ¡Protocol ¡(HTTP) ¡is ¡the ¡protocol ¡commonly ¡ used ¡by ¡the ¡World ¡Wide ¡Web ¡to ¡get ¡HyperText ¡Markup ¡Language ¡ (HTML) ¡documents ¡that ¡describes ¡how ¡to ¡render ¡a ¡web ¡page ¡ • We ¡use ¡a ¡Uniform ¡Resource ¡Loca[on ¡(URL) ¡to ¡specify ¡what ¡page ¡ we ¡want ¡to ¡get: ¡ h^p://www.cs.dartmouth.edu/~tjp/cs10/index.php ¡ Protocol ¡ ¡ Computer ¡ Directory ¡where ¡ File ¡(assume ¡index.html ¡or ¡ how ¡we ¡will ¡talk ¡ that ¡has ¡data ¡ data ¡located ¡ index.php ¡if ¡not ¡provided) ¡ 11 ¡

  12. Client ¡makes ¡a ¡request ¡for ¡a ¡resource ¡to ¡a ¡ Server; ¡Server ¡responds ¡to ¡request ¡ Process ¡ Give ¡me ¡this ¡file: ¡ h^p://www.cs.dartmouth.edu/~tjp/cs10/index.php ¡ Your ¡browser ¡ Web ¡server ¡ Browser ¡interprets ¡ HTML ¡data ¡and ¡ displays ¡page ¡ Sure, ¡I ¡have ¡that ¡file, ¡here ¡you ¡go: ¡ <!DOCTYPE ¡html ¡PUBLIC ¡"-­‑//W3C//DTD ¡XHTML ¡1.0 ¡Strict//EN" ¡"h^p://www.w3.org/TR/xhtml1/DTD/xhtml1-­‑strict.dtd"> ¡ <html ¡xmlns="h^p://www.w3.org/1999/xhtml" ¡xml:lang="en" ¡lang="en"> ¡ <head> ¡ <meta ¡h^p-­‑equiv="content-­‑type" ¡content="text/html;charset=u`-­‑8" ¡/> ¡ <[tle>CS ¡10 ¡| ¡Problem ¡solving ¡| ¡Winter ¡2017</[tle> ¡ </head> ¡ ¡ <body> ¡ <div ¡id="page"> ¡ <div ¡id="header"> ¡ ¡ ¡<div ¡id="[tle">CS ¡10, ¡Winter ¡2017</div> ¡ 12 ¡ ¡ ¡<div ¡id="sub[tle">Problem ¡Solving ¡via ¡Object ¡Oriented ¡Programming</div> ¡ </div> ¡… ¡

  13. Java ¡makes ¡it ¡easy ¡to ¡get ¡HyperText ¡ Markup ¡Language ¡(HTML) ¡from ¡the ¡web ¡ GeCng ¡HTML ¡from ¡the ¡web ¡ public public class class WWWGetTry WWWGetTry { { public public static static void void main(String[] main(String[] args args) { ) { try try { { // Create the URL; can throw MalformedURL URL url = new new URL( URL("http:// "http://www.cs.dartmouth.edu www.cs.dartmouth.edu/~ /~tjp tjp/cs10/ /cs10/index.php index.php"); ); System. out out.println .println("*** getting " "*** getting " + + url url); ); // Create the reader for the stream; can throw IO BufferedReader in = new new BufferedReader BufferedReader(new new InputStreamReader InputStreamReader(url url.openStream .openStream())); ())); // Read the lines; can throw IO try try { { String line; while while (( ((line line = = in in.readLine .readLine()) != ()) != null null) { ) { System. out out.println .println(line line); ); } } // Be sure to close the reader, whether or not reading was successful finally finally { { in.close(); } } catch catch ( (MalformedURLException MalformedURLException e) { ) { System. err err.println .println("bad URL" "bad URL"); ); } catch catch ( (IOException IOException e) { ) { System. err err.println .println("problem opening/reading/closing" "problem opening/reading/closing"); ); } System. out out.println .println("*** done" "*** done"); ); } } ¡ 13 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend