1 / 15

Plan for today

Plan for today. Open-Closed and Quizmaster Review of open-closed principle Hopes, dreams, aspirations, reality What is refactoring for? Design first, code second? Repeat until done Third-party APIs v Stock Android How to navigate the online space of options. Quizmaster Review and Preview.

tainap
Download Presentation

Plan for today

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Plan for today • Open-Closed and Quizmaster • Review of open-closed principle • Hopes, dreams, aspirations, reality • What is refactoring for? • Design first, code second? Repeat until done • Third-party APIs v Stock Android • How to navigate the online space of options

  2. Quizmaster Review and Preview • Where are quizzes found? • Hard-wired/coded in QuizGenerator.java • Stored in XML files as part of assets, read and parsed by custom XMLxyz.java classes • What's missing here? Web-based quizzes • How could these alternatives co-exist? • Design for complete/some flexibility, then implement • Get quiz from web, then increase flexibility

  3. Toward Internet/Web connections • Services and permissions on Android must be dealt with intentionally • To open a connection to Internet? Must supply app permission • Permission levels for Android Applications • Android 6.0 change in model • Runtime permission vs. Install permission • https://developer.android.com/training/articles/user-data-permissions.html • We'll see more on best practices later

  4. Adding Permissions for Applications • Use AndroidManifest.xml for permissions • Open Internet socket/connection --- aside: what is a socket? • Also access network state (not needed here) <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" />

  5. How to access Internet? • Step one: Google, StackOverflow, Android book • Open HTTPConnection, similar to how this is done in Java • See code in JSONQuizGenerator.java and method getJSONRaw() • Examine code in gitlab: • https://gitlab.oit.duke.edu/ola/quizmaster/blob/master/app/src/main/java/edu/duke/compsci290/quizmaster/JSONQuizGenerator.java

  6. Concepts in this version of code • Create URL (see constructor), what is a URL? • Store URL as instance variable for use in method to make connection • Alternative from Java/design perspective? • What to do with Exceptions? • In general, throwing exceptions to top level is a good idea • What is alternative? • What about application specific exceptions?

  7. Concepts in HttpURLConnection • Create the connection • Options required or available: type of connection, duration of "try and try again" • Get response code for connection, verify • Open stream for connection • Read stream for connection • Notice stream reading code • Decorator design pattern (more later). Create readers from streams and readers from readers • Dissect code in .readStream()

  8. How to test the connection? • Deploy to phone, offer user choice of creating quiz from online/Internet • Get ready to debug • Android.os.NetworkOnMainThreadException • Say what? E/InputEventReceiver: Exception dispatching input event. 02-13 22:46:45.566 20555-20555/edu.duke.compsci290.quizmaster E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback 02-13 22:46:45.568 20555-20555/edu.duke.compsci290.quizmaster E/MessageQueue-JNI: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)

  9. Is Google/Search your friend? • https://developer.android.com/reference/android/os/AsyncTask.html AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. • Who will be happy with this approach?

  10. Third Party APIs • You will often see these on StackOverflow • How do you know which are "good"? • Good judgment comes from experience, experience comes from bad judgment • Good design comes from experience, experience comes from bad design • Side note: attributing quotes is problematic • In our case if it's referenced in Android documentation …

  11. Let's use the Volley library/APIs • Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. • https://developer.android.com/training/volley/index.html • How to add GitHub or other library to project? • Differences: Android Studio and other Java projects – managing dependencies • Maven, Gradle, others • Trust or investigate fully or …

  12. Let's use the Volley library/API • Choose the right Build.gradle file • compile 'com.android.volley:volley:1.0.0' • Magic happens (ok, that's relative)

  13. What does Volley get us? • Handle network requests simply and appropriately on the right non-UI thread • Call back to main UI thread you manage • See code in new getJSON() method StringRequest request = new StringRequest(Request.Method.GET,mURLString,new Response.Listener<String>(){ // override method here },new Response.ErrorListener(){ // override method here });

  14. What is JSON and why not XML? • Given that we have XMLQuizParser what might be appropriate for testing Internet? • Copy XML file to Internet, get it, parse it, … • Why use JSON instead? • JavaScript Object Notation • What does Google say it is? • Why is it popular on the web? • Why is it used in Android applications?

  15. JSON Example • http://www.cs.duke.edu/csed/quizzes/oscars.json • What is format for JSON file? • This example is NOT created to play nicely with our Quiz and Question classes • However, Compare JSONParser class to XMLQuizParser class • Better alternative: Gson library • Parse directly to Java class/object: reflection

More Related