CS 491 Lecture 9 – Web services and backgrounding
Before class
A
Before next class:
B
Before next class:
- Watch http://youtu.be/nR-I8TG9mdE.
- Read http://techtej.blogspot.com/2011/02/android-passing-data-between-main.html. (This post offers some background on how Handlers work. There’s little narrative in the official Android documentation. Note that the UI thread already has a Looper; we need only create a Handler.)
In class
Continue on your task from Tuesday, with the additional task of fetching a grade report for your authenticated user. Fetching the grade report can be done by submitting an HTTP request for http://www.twodee.org/teaching/cs491-mobile/get_grades.php?username=<username>, where <username> is replaced by the username of your authenticated user. Check the JSON it produces out in your web browser.
On your device, parse the JSON into a form that can be displayed to the user.
If you fail to send a username, you’ll get a 401 error code, which means you’re not authenticated.
Note that there are some terrible problems with this app. We are leaving the authentication up to the mobile device instead of the server that actually holds the data. Never ever do this. Our goal today is to explore local databases and JSON, so we are temporarily overlooking the terribleness.
For the curious, here is get_grades.php:
<?php
if (!isset($_REQUEST['username'])) {
header('WWW-Authenticate: BadAuthenticationScheme realm="RandoGrades"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
# We generate consistent but random results for a user by seeding the random
# number generator with a hash of the username.
$hash16 = md5($_REQUEST['username']);
$hash10 = base_convert($hash16, 16, 10);
srand($hash10);
$letters = array('A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D', 'F');
$scores['hw1'] = rand(0, 100);
$scores['hw2'] = rand(0, 100);
$scores['hw3'] = rand(0, 100);
$scores['hw4'] = rand(0, 100);
$scores['exam1'] = rand(0, 100);
$scores['exam2'] = rand(0, 100);
$scores['letter'] = $letters[rand(0, count($letters) - 1)];
echo json_encode($scores);
?>