HUJI Post PC Workshop 5 Files, Threading, and Web Services Sasha Goldshtein
Working with Files Android apps can read and write files from their local directory ◦ /data/data/il.ac.huji.myapp/files Standard Java I/O API List students =...; FileOutputStream out = openFileOutput( “students”, MODE_PRIVATE); ObjectOutputStream objOut = new ObjectOutputStream(out); objOut.writeObject(students); objOut.close(); out.close(); List students =...; FileOutputStream out = openFileOutput( “students”, MODE_PRIVATE); ObjectOutputStream objOut = new ObjectOutputStream(out); objOut.writeObject(students); objOut.close(); out.close();
Demonstration: Files and DDMS DDMS can show you the device’s file system, download and upload files
Working with Threads Offload any considerable work to a background thread Thread t = new Thread( new Runnable() { public void run() {... } ); t.start(); Thread t = new Thread( new Runnable() { public void run() {... } ); t.start();
Demonstration: AsyncTask Helper for doing work in the background and posting progress + results public class LineCount extends AsyncTask { protected void onPreExecute() { //Initialize progress dialog } protected Long doInBackground(String... files) { //Process files and call publishProgress periodically } protected void onProgressUpdate(Integer... progress) { //Update progress dialog to progress[0] } protected void onPostExecute(Long result) { //Dismiss progress dialog and display result } public class LineCount extends AsyncTask { protected void onPreExecute() { //Initialize progress dialog } protected Long doInBackground(String... files) { //Process files and call publishProgress periodically } protected void onProgressUpdate(Integer... progress) { //Update progress dialog to progress[0] } protected void onPostExecute(Long result) { //Dismiss progress dialog and display result } new LineCount().execute(“file1”, “file2”, “file3”);
Accessing Web Services The world is at your fingertips with HTTP and JSON ◦ HTTP requests with HttpURLConnection ◦ Parse JSON with JSONObject {..., “results”: [ { “from_user”: “igoogledisrael”, “text”: “Beer in the Hebrew University”,... },... ] } {..., “results”: [ { “from_user”: “igoogledisrael”, “text”: “Beer in the Hebrew University”,... },... ] }
Demonstration: Accessing Web Services Google Geocoding API URL url = new URL(“ + “geocode/json?address=Jerusalem&sensor=false”); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream in = new BufferedInputStream(conn.getInputStream()); String response = readStream(in); //Omitted for brevity JSONObject json = new JSONObject(response); JSONArray arr = json.getJSONArray(“results”); JSONObject res = arr.getJSONObject(0); JSONObject geo = res.getJSONObject(“geometry”); JSONObject loc = geo.getJSONObject(“location”); double latitude = loc.getDouble(“lat”); double longitude = loc.getDouble(“lon”); URL url = new URL(“ + “geocode/json?address=Jerusalem&sensor=false”); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStream in = new BufferedInputStream(conn.getInputStream()); String response = readStream(in); //Omitted for brevity JSONObject json = new JSONObject(response); JSONArray arr = json.getJSONArray(“results”); JSONObject res = arr.getJSONObject(0); JSONObject geo = res.getJSONObject(“geometry”); JSONObject loc = geo.getJSONObject(“location”); double latitude = loc.getDouble(“lat”); double longitude = loc.getDouble(“lon”); { “results”: [ { “geometry”: { “location”: { “lat”: 30.0, “lon”: 24.0 } },... ],... } { “results”: [ { “geometry”: { “location”: { “lat”: 30.0, “lon”: 24.0 } },... ],... }
Homework 5 Todo List App v4 ◦ Repo tag: v4 On start, search for tweets tagged #todoapp and create new tasks Add a todo thumbnail ◦ Retrieved from Flickr API by search keyword 12:38 Buy flowers 20/01/2013 Invite David 26/04/2013 Call Mom 29/04/2013 Delete Item Add Thumbnail Buy flowers Add Thumbnail OKCancel dinner Dialog
Additional Resources java.io summary.html Processes and Threads threads.html AsyncTask HttpURLConnection on.html