android programming lecture 17: file and network i/o...

26
Android Programming Lecture 17: File and Network I/O Menus 11/9/2011

Upload: others

Post on 28-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Android ProgrammingLecture 17: 

File and Network I/OMenus

11/9/2011

Page 2: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

File and Network I/O

Page 3: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Direct File I/O

• Android API provides hook into the Android phone’s file system 

• These methods to open files for input and output– Methods don’t allow folder specification– Typically employ app‐specific permissions 

• Can employ with general Java I/O classes and methods 

Page 4: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Android & Java I/O: Text Output

PrintWriter: use print() and println() method to write arbitrary strings of text, will automatically call toString() on objectsBufferedWriter: handles buffering of data before writing to disk (writes in one big burst instead of each characters – important for efficiency)OutputStreamWriter: handles String to byte conversionFileOutputStream: writes bytes to a file on disk

Page 5: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Android & Java I/O: Example Text Output Code

Java I/O code requires try/catch blocksTry: Attempt to do file processingCatch: If something goes wrong, let 

me know

Page 6: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Java I/O: Text Input

Common wrapping (using Android openFileInput, which gives back a FileInputStream)

Scanner scanner = new Scanner(new BufferedInputStream(openFileInput(filenameGoesHere)));

Page 7: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Android & Java I/O: Text InputScanner allows to read text by line, but also to parse out integers and doubles if they exist:

hasNextLine() – are there more linesnextLine() – give me the next linehasNextDouble() – is the next thing a doublenextDouble() – give me the next double…. 

BufferedInputStream supports buffering of reads from file (efficiency)

FileInputStream supports reading of data from files

Page 8: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Android & Java I/O: Text Input

Continuation of program from a few slides back

Page 9: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Accessing General Web Data

• We have seen in previous slides how to access “web services” – Essentially, calling a function/method on another machine (details of how this works are hidden from programmer)

• What if we just want to access HTML data?– Maybe we could grab Aramark menu data and have a PitMenu app?

Page 10: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Accessing General Web Data

• Direct HTTP access is needed commonly enough that the Android code API already supports it

• One approach:  URLConnection– Specify a URL– Request connection to that URL– Treat open connection as an input stream 

• We already know what to do at this point (from files)– Close connection

• Because network based, requires try/catch to deal with potential errors

Page 11: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

URLConnectionSpecify a URL

URL dataURL = new URL(“address_here”);Request connection to that URL

URLConnection = dataURL.openConnection();Treat open connection as an input stream

BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream());

Process data Above approach will allow line at a time readingvia BufferedReader

Close connectionurlConnection.getInputStream().close();

Page 12: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

URLConnection: ExampleGoal: Have app “scrape” web addresses out of an HTML web page 

– This technique is often used by “web‐crawlers” (such as Google)

Page 13: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

URLConnection: Example

Page 14: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

HTTPURLConnectionOne can cast a URLConnection made to a HTTP address as a HttpURLConnection and take advantage of a few HTTP specific methodsOne important example:

int getResponseCode() , String getResponseMessage()Useful in handling protocol errors (404: page not found)

Page 15: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

URLConnection: Example

Page 16: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

URLConnection: OutputStream (POST)It is possible to use a URLConnection stream as an output stream as well

Supports POST component of HTTP protocol, where data is sent to a server, such as when completing web forms

urlConnection.getOutputStream();

Page 17: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Menus

Page 18: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Menus• There is a hardware menu button on Android phones and tablets– Exposes Activity‐specific menus – These menus can hold up to six menu items, in a tiled fashion, or five items and a More button which displays an arbitrarily long list of additional menu options.

• This is the “Expanded Menu”, and it is hooked to the bottom of the screen

– Menus can trigger submenus, which are floating dialogs (they look much like drop down lists)

Page 19: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Menus: RideTheWake Example

• In the RideTheWakeshuttle tracking app, the menu allows for:– Choosing a bus– Readjusting 

center if strayed away from bus

– Checking for temporary schedule changes

– Changing app settings (polling time)

Normal Menu Expanded Menu

Page 20: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Generating Menus: Meta Ideas

• A menu consists of one or more MenuItems– Menu items in the primary (6‐option‐max) menu can be specified with an icon and text.

– The ExpandedMenu and Submenus do not support icons, but do support Checkboxes and RadioButtons

– All MenuItems can be associated with keyboard shortcuts and condensed titles

Page 21: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Handling Selection Events: Meta Ideas

• Activities are already designed to listen to menu button presses– Just have to write functions

• Primary response mechanism: onOptionsItemSelected handler function – One function to handle any menu press

• Can also associate with each menu item a function to support handling menu selection:– Use android:onClick=functionName attribute in XML 

Page 22: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Generating Menus: XML• Define an xml file in res/menu

– Make one for each menu (activity)– May have to make this directory

• Start with outer <menu> tag • Menu items are indicated by <item>

– Associate with id, icon, title, checkable, radiobox, shortcuts (numeric and alphabetic) attributes

– Submenus are defined as a menu within an item

Full details for <menu> & <item> syntax is here: http://developer.android.com/guide/topics/resources/menu‐resource.html

Page 23: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Generating Menus: XML Examples

Simple three item menu:           Bus menu:  

Three item menu with submenu:

Icons are drawable resourcesIcons show up for first five, rest are onExpandedMenu

Page 24: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Triggering MenusWhen the menu hardware button is pressed, a listener function is called, which should inflate the menu XML:

public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater= getMenuInflater();inflater.inflate(R.menu.menu_name_here, menu);return true;}

Alternatively, you could build the menu directly in Java here [calling MenuItem constructor, setting attributes], but we’ll stick with XML – see page 125 of book for Java approach

Page 25: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Handling Selection EventsFor Menus

A selected menu item is passed to the listener function:

public boolean onOptionsItemSelected(MenuItem item){

// call super function of this function to support any upstream processing

// check id, title, etc. to help decide how to act// return true if handled completely

}

Page 26: Android Programming Lecture 17: File and Network I/O Menuscsweb.cs.wfu.edu/~turketwh/CSC191/Fall2011/191Lecture17.pdf · Android & Java I/O: Text Input Scanner allows to read text

Simple Menu Example