Android Programming: Lecture 07

2,208 views
2,142 views

Published on

1) XML Parsing on Android
2) Internet Access in Android Applications
3) Android Data Persistence through Files and SharedPreferences
4) Class home page is at http://vkedco.blogspot.com/2011/08/theory-of-computation-home.html

Published in: Software, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

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

No notes for slide

Android Programming: Lecture 07

  1. 1. Android Programming Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Resources Source code and presentation materials for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-07.htmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Outline ● XML Parsing on Android ● Internet Access in Android Apps ● Data Persistence through Files and Shared Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. XML Parsing on Androidwww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. XML Parsing on Android ● Java + XML = Portable Code + Portable Data ● Android platform strength is that it leverages Java ● Android SDK does not support everything available in JRE, but it does support a significant part of it, including most of Javas XML APIswww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. SAX vs DOM ● Android supports both Javas Simple API for XML (SAX) and the Document Object Model (DOM) ● Android does not support Streaming API for XML (StAX) but does provide a functionally equivalent library ● To the developer, SAX and DOM on Android are the same as they are on JREwww.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. SAX vs DOM ● SAX does not read an XML document into memory ● SAX parses documents piece by piece through event handlers implemented by the programmer ● DOM does read an entire XML document into memory and then gives the programmer a set of methods to manipulate the document tree ● DOM tends to be easier to program than SAX ● Both SAX and DOM are useful tools: it is all about space/time tradeoffs and the needs of a specific applicationwww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. DOMSamples Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Things To Take Away from This Project ● Introduction to DOM on Android ● How to build a XML document with DOM ● How to parse an XML document with DOMwww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Mathematicians.xml <?xml version="1.0" encoding="utf-8"?> <Mathematician> <!-- class of mathematicians --> <GeorgCantor research_area="set theory"> <!-- 1845 - 1918 --> </GeorgCantor> <DavidHilbert research_area="mathematical logic"> <!-- 1862 - 1943 --> </DavidHilbert> <GiuseppePeano research_area="number theory"> <!-- 1858 - 1932 --> </GiuseppePeano> </Mathematician>www.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. How Can One Generate Mathematicians.xml Programmatically with DOM?www.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Generating the Documents Root Element import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element mathematician = doc.createElement(res.getString(R.string.mathematician_tag)); Comment mathematician_comment = doc.createComment("class of mathematicians"); mathematician.appendChild(mathematician_comment); doc.appendChild(mathematician);www.youtube.com/vkedco
  13. 13. Generating the Roots Children // 1. Child element for Georg Cantor Element georg_kantor = doc.createElement(res.getString(R.string.georg_cantor_tag)); Comment georg_kantor_comment = doc.createComment("1845 - 1918"); georg_kantor.appendChild(georg_kantor_comment); georg_kantor.setAttribute(research_area_attrib, "set theory"); mathematician.appendChild(georg_kantor); // 2. Ghild element for Giuseppe Peano Element giuseppe_peano = doc.createElement(res.getString(R.string.giuseppe_peano_tag)); Comment giuseppe_peano_comment = doc.createComment("1858 - 1932"); giuseppe_peano.appendChild(giuseppe_peano_comment); giuseppe_peano.setAttribute(research_area_attrib, "number theory"); mathematician.appendChild(giuseppe_peano);www.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. XMLDocBuilder.buildDocOne() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. After Mathematicians.xml Is Generated, How Can One Manipulate It with DOM?www.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Document Manipulation Element mathematician_elem = doc.getDocumentElement(); NodeList children = mathematician_elem.getChildNodes(); for(int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ( child.getNodeType() == Node.COMMENT_NODE) { // process comment node } else if ( child.getNodeType() == Node.ELEMENT_NODE) { // process element node } else { // do something else }www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. DOMSamplesAct.parseXMLDoc() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
  18. 18. XML Parsing on Android and Internet Accesswww.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. EarthquakeViewer Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. What To Take Away From This Project ● XML Parsing with DOM ● Internet Access from Android Applicationswww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Earthquake Viewer ● Create a list-based activity that connects to an earthquake RSS feed from USGS ● Parse the XML with the list of earthquakes all over the world ● Display the location, magnitude, and time of each earthquake it contains ● When the user clicks on an earthquake, an AlterDialog provides a prompt that includes a link to the USGS web sitewww.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Earthquake Viewer Screenswww.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Web Resources <string name="quake_feed"> http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml </string> <string name="quake_feed_backup"> http://digital.cs.usu.edu/~vkulyukin/vkweb/earthquakes.xml </string>www.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. USGS XML File <?xml version="1.0"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"> <entry><id>urn:earthquake-usgs-gov:nn:00350821</id> <title>M 2.6, Nevada</title> <updated>2011-10-10T22:08:55Z</updated> <link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/nn00350821.php"/> <link rel="related" type="application/cap+xml" href="http://earthquake.usgs.gov/earthquakes/catalogs/cap/nn00350821" /> <georss:point>37.3562 -117.1253</georss:point> <georss:elev>-12000</georss:elev> </entry> </feed>www.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. EarthquakeViewer: main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/earthquakeListView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. EarthquakeViewr Project: Quake.javawww.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. quake_details.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/quakeDetailsTextView" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="14sp" android:autoLink="all" /> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. EarthquakeViewerAct.refreshEarthquakes() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Data Persistence Through Files and Shared Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Data Persistence Techniques ● Local Files ● Shared Preferences ● Content Providers ● Cloud serverswww.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. Local Files Back to DOMSamples Project: XMLDocBuilder.buildDocTwo() and /res/raw/mathematicians.xmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. SharedPreferences ● Lightweight key/value pair mechanism for saving primitive application data (Boolean, string, float, long, and integer) ● Commonly used for saving users application preferences, UI states, application settings ● Instances of SharedPreferences class can contain key/value pairs that can be shared among all activities running within the same application contextwww.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Typical Use: Storing Shared Preferences public static String PREFS = “MyPrefs”; SharedPreferences myPrefs = getSharedPreferences(PREFS, Activity.MODE_PRIVATE); SharedPreferences.Editor edit = myPrefs.edit(); edit.putBoolean(“True”, true); edit.putFloat(“Float”, 5f); edit.putInt(“Int”, 10); edit.putString(“StringKey”, “StringValue”);www.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Typical Use: Retrieving Shared Preferences public static String PREFS = “MyPrefs”; SharedPreferences myPrefs = getSharedPreferences(PREFS, Activity.MODE_PRIVATE); boolean storedBool = myPrefs.getBoolean(“True”, false); float storedFloat = myPrefs.getFloat(“Float”, 0f); int storedInt = myPrefs.getInt(“Int”, 0); String storedStr = myPrefs.getString(“StringKey”, “”);www.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. SharedPrefs1 Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. What To Take Away From This Project ● Saving SharedPreferences ● Loading SharedPreferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. SharedPreferences1 Project: Screenswww.youtube.com/vkedco www.vkedco.blogspot.com
  38. 38. SharedPrefs1 Project: SharedPrefs1Act.safePrefs() and SharedPrefs1Act.loadPrefs()www.youtube.com/vkedco www.vkedco.blogspot.com
  39. 39. Back to EarthquakeViewer Project: Adding Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  40. 40. Adding Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
  41. 41. XML Layout: preferences.xmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  42. 42. Populating Preference Spinners private void populateSpinners() { ArrayAdapter<CharSequence> freqAdapter; freqAdapter = ArrayAdapter.createFromResource(this, R.array.update_freq_options, android.R.layout.simple_spinner_item); int spinner_dd_item = android.R.layout.simple_spinner_dropdown_item; freqAdapter.setDropDownViewResource(spinner_dd_item); updateFreqSpinner.setAdapter(freqAdapter); ArrayAdapter<CharSequence> magnAdapter; magnAdapter = ArrayAdapter.createFromResource(this, R.array.magnitude_options, android.R.layout.simple_spinner_item); magnAdapter.setDropDownViewResource(spinner_dd_item); magnitudeSpinner.setAdapter(magnAdapter); }www.youtube.com/vkedco www.vkedco.blogspot.com
  43. 43. PreverencesAct.updateUIFromPreferences() and PreferencesAct.savePreferences()www.youtube.com/vkedco www.vkedco.blogspot.com
  44. 44. References & Reading Suggestions ● http://developer.android.com/reference/javax/xml/parsers/package-summary.html ● Chapters 5, 6 in “Professional Android 2 Application Development” by Rito Meier ● Chapters 2, 3 in “Java and XML” by Brett McLaughlin ● Two links on serialization on Android (many thanks to Jared Barneck for sending these to me): – http://simple.sourceforge.net – http://simple.sourceforge.networkswww.youtube.com/vkedco www.vkedco.blogspot.com
  45. 45. Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com

×