Download presentation
Presentation is loading. Please wait.
Published byPhyllis Fleming Modified over 8 years ago
1
HUJI Post PC Workshop 5 Files, Threading, and Web Services Sasha Goldshtein goldshtn@gmail.com
2
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();
3
Demonstration: Files and DDMS DDMS can show you the device’s file system, download and upload files
4
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();
5
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”);
6
Accessing Web Services The world is at your fingertips with HTTP and JSON ◦ HTTP requests with HttpURLConnection ◦ Parse JSON with JSONObject http://search.twitter.com/search.json?q=hebrew%20university {..., “results”: [ { “from_user”: “igoogledisrael”, “text”: “Beer in the sun @ Hebrew University”,... },... ] } {..., “results”: [ { “from_user”: “igoogledisrael”, “text”: “Beer in the sun @ Hebrew University”,... },... ] }
7
Demonstration: Accessing Web Services Google Geocoding API URL url = new URL(“https://maps.googleapis.com/maps/api/” + “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(“https://maps.googleapis.com/maps/api/” + “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 } },... ],... }
8
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
9
Additional Resources java.io http://developer.android.com/reference/java/io/package- summary.html Processes and Threads http://developer.android.com/guide/components/processes-and- threads.html AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html HttpURLConnection http://developer.android.com/reference/java/net/HttpURLConnecti on.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.