Download slides7

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

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
Compsci 290.3/Mobile, Spring 2017
6.1
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
Compsci 290.3/Mobile, Spring 2017
6.2
Toward Internet/Web connections

Services and permissions on Android must
be dealt with intentionally



Android 6.0 change in model



To open a connection to Internet? Must supply
app permission
Permission levels for Android Applications
Runtime permission vs. Install permission
https://developer.android.com/training/articles/
user-data-permissions.html
We'll see more on best practices later
Compsci 290.3/Mobile, Spring 2017
6.3
Adding Permissions for Applications

Use AndroidManifest.xml for permissions

Open Internet socket/connection --- aside: what
is a socket?
<uses-permission android:name=
"android.permission.INTERNET" />

Also access network state (not needed here)
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE" />
Compsci 290.3/Mobile, Spring 2017
6.4
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/quiz
master/blob/master/app/src/main/java
/edu/duke/compsci290/quizmaster/JSON
QuizGenerator.java
Compsci 290.3/Mobile, Spring 2017
6.5
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?
Compsci 290.3/Mobile, Spring 2017
6.6
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()
Compsci 290.3/Mobile, Spring 2017
6.7
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)
Compsci 290.3/Mobile, Spring 2017
6.8
Is Google/Search your friend?
https://developer.android.com/reference/an
droid/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?
Compsci 290.3/Mobile, Spring 2017
6.9
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 …
Compsci 290.3/Mobile, Spring 2017
6.10
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/in
dex.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
6.11
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)
Compsci 290.3/Mobile, Spring 2017
6.12
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
});
Compsci 290.3/Mobile, Spring 2017
6.13
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?
Compsci 290.3/Mobile, Spring 2017
6.14
JSON Example

http://www.cs.duke.edu/csed/quizzes/oscar
s.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
6.15