Download Plan for today Quizmaster Review and Preview Toward

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Plan for today
● 
Quizmaster Review and Preview
Open-Closed and Quizmaster
● 
Review of open-closed principle
Hopes, dreams, aspirations, reality
Ø  What is refactoring for?
Ø  Design first, code second? Repeat until done
● 
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
Ø 
Ø 
Ø 
Ø 
Third-party APIs v Stock Android
Ø 
● 
How could these alternatives co-exist?
Design for complete/some flexibility, then
implement
Ø  Get quiz from web, then increase flexibility
Ø 
How to navigate the online space of options
Compsci 290.3/Mobile, Spring 2017
7.1
Toward Internet/Web connections
● 
Where are quizzes found?
Compsci 290.3/Mobile, Spring 2017
Adding Permissions for Applications
Services and permissions on Android must
be dealt with intentionally
● 
Use AndroidManifest.xml for permissions
Ø 
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
Ø 
Also access network state (not needed here)
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE" />
We'll see more on best practices later
Compsci 290.3/Mobile, Spring 2017
Open Internet socket/connection --- aside: what
is a socket?
<uses-permission android:name=
"android.permission.INTERNET" />
Ø 
● 
7.2
7.3
Compsci 290.3/Mobile, Spring 2017
7.4
How to access Internet?
● 
Concepts in this version of code
Step one: Google, StackOverflow, Android
book
● 
Open HTTPConnection, similar to how this is
done in Java
Ø  See code in JSONQuizGenerator.java and
method getJSONRaw()
Store URL as instance variable for use in
method to make connection
Ø  Alternative from Java/design perspective?
Ø  What to do with Exceptions?
Ø 
● 
Ø 
Examine code in gitlab:
Ø 
● 
https://gitlab.oit.duke.edu/ola/
quizmaster/blob/master/app/src/main/
java/edu/duke/compsci290/quizmaster/
JSONQuizGenerator.java
Compsci 290.3/Mobile, Spring 2017
What is alternative?
Ø  What about application specific exceptions?
7.5
Compsci 290.3/Mobile, Spring 2017
7.6
How to test the connection?
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
Ø 
● 
In general, throwing exceptions to top level
is a good idea
Ø 
Concepts in HttpURLConnection
● 
Create URL (see constructor), what is a
URL?
Deploy to phone, offer user choice of
creating quiz from online/Internet
Get ready to debug
Android.os.NetworkOnMainThreadException
Ø  Say what?
Ø 
Ø 
Notice stream reading code
Decorator design pattern (more later). Create
readers from streams and readers from readers
Ø  Dissect code in .readStream()
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)
Ø 
Compsci 290.3/Mobile, Spring 2017
7.7
Compsci 290.3/Mobile, Spring 2017
7.8
Is Google/Search your friend?
Third Party APIs
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.
● 
● 
7.9
Let's use the Volley library/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
Ø 
Ø 
● 
Who will be happy with this approach?
Compsci 290.3/Mobile, Spring 2017
● 
In our case if it's referenced in Android
documentation …
Compsci 290.3/Mobile, Spring 2017
7.10
Let's use the Volley library/API
Volley is an HTTP library that makes
networking for Android apps easier and
most importantly, faster. ● 
Choose the right Build.gradle file
compile 'com.android.volley:volley:1.0.0'
Ø  Magic happens (ok, that's relative)
Ø 
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 …
Compsci 290.3/Mobile, Spring 2017
7.11
Compsci 290.3/Mobile, Spring 2017
7.12
What does Volley get us?
● 
What is JSON and why not XML?
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
});
Compsci 290.3/Mobile, Spring 2017
7.13
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
Compsci 290.3/Mobile, Spring 2017
Given that we have XMLQuizParser what
might be appropriate for testing Internet?
7.15
● 
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?
Ø 
Ø 
Compsci 290.3/Mobile, Spring 2017
7.14