1 / 85

HTTP and Threads

HTTP and Threads. Getting Data from the Web. Believe it or not, Android apps are able to pull data from the web. Developers can download bitmaps and text and use that data directly in their application. How to use HTTP with Android. Ask for permission Make a connection Use the data.

denise
Download Presentation

HTTP and Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HTTP and Threads

  2. Getting Data from the Web • Believe it or not, Android apps are able to pull data from the web. • Developers can download bitmaps and text and use that data directly in their application.

  3. How to use HTTP with Android • Ask for permission • Make a connection • Use the data

  4. Step 1: Ask for Permission • An application’s Android Manifest specifies which permissions are needed in order for the application to run. • For application wanting to access the internet, it must list that permission in its Manifest.

  5. Step 1: Ask for Permission • Open your project’s AndroidManifeset.xml Add the following xml: <uses-permission android:name="android.permission.INTERNET"/>

  6. Step 1: Ask for Permission <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidhttp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.androidhttp.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

  7. Permission • When a user downloads an app from the Android Play Store, they’ll have to accept and agree to all permissions required by the app before it is installed. • Usually users don’t read the small text and just agree, but it’s still good to know.

  8. Don’t forget to ask • If you fail to add the permission to your AndroidManifest.xml you will NOT get a runtime or compile time error. • Your application will simply fail to connect to the internet.

  9. Step 2: Make a connection Bitmap img=null; URL url; try{ //A uniform resource locator aka the place where the data is //located url=new URL("http://theheroicage.com/wp- content/uploads/2011/04/deadpool-team-up-887-cover-art.jpg"); //Opens an HTTPUrlConnection and downloads the input stream into a //Bitmap img=BitmapFactory.decodeStream(url.openStream()); }catch(MalformedURLException e){ Log.e("CRR","URL is bad"); e.printStackTrace(); }catch(IOException e){ Log.e("CRR","Failed to decode Bitmap"); e.printStackTrace(); } mImage.setImageBitmap(img);

  10. Using a URL to create a Bitmap url=new URL("http://theheroicage.com/wp- content/uploads/2011/04/deadpool-team-up-887-cover-art.jpg"); //Opens an HTTPUrlConnection and downloads the input stream into a //Bitmap img=BitmapFactory.decodeStream(url.openStream());

  11. Step 3: Use the data Bitmap img=null; URL url; try{ //A uniform resource locator aka the place where the data is //located url=new URL("http://theheroicage.com/wp- content/uploads/2011/04/deadpool-team-up-887-cover-art.jpg"); //Opens an HTTPUrlConnection and downloads the input stream into a //Bitmap img=BitmapFactory.decodeStream(url.openStream()); }catch(MalformedURLException e){ Log.e("CRR", "URL is bad"); e.printStackTrace(); }catch(IOException e){ Log.e("CRR", "Failed to decode Bitmap"); e.printStackTrace(); } mImage.setImageBitmap(img);

  12. Full Code for Making a Connection Bitmap img=null; URL url; try{ //A uniform resource locator aka the place where the data is //located url=new URL("http://theheroicage.com/wp- content/uploads/2011/04/deadpool-team-up-887-cover-art.jpg"); //Opens an HTTPUrlConnection and downloads the input stream into a //Bitmap img=BitmapFactory.decodeStream(url.openStream()); }catch(MalformedURLException e){ Log.e("CRR","URL is bad"); e.printStackTrace(); }catch(IOException e){ Log.e("CRR","Failed to decode Bitmap"); e.printStackTrace(); } mImage.setImageBitmap(img);

  13. Convert Bitmap to Drawable img=BitmapFactory.decodeStream(url.openStream()); Drawabled = new BitmapDrawable(img);

  14. Decode Bitmap from stream • http://stackoverflow.com/a/5776903/1222232

  15. Your Turn! • Create an Android application that uses a URL to create a Bitmap and place that Bitmap as the source of an ImageView.

  16. NetworkOnMainThreadException • The exception that is thrown when an application attempts to perform a networking operation on its main thread.

  17. Single Threaded • When launching your Android application, a single system process with a single thread of execution is spawned. • By default your app has 1 process and 1 thread.

  18. UI Thread • That single thread has several names: • main application thread • main user interface thread • main thread • user interface thread • Mostly known as the UI Thread

  19. Why UI Thread • This is the thread where the following occurs • Layout • Measuring • Drawing • Event handling • Other UI related logic • A developer should use the UI Thread for UI

  20. Blocking the UI Thread • Anytime a long running operation takes place on the UI thread, UI execution is paused. • While paused, your app can’t: • Handle Events • Draw • Layout • Measure

  21. UI Thread Execution Handle Touch Events Measure Layout Draw UI Thread

  22. UI Thread Execution with HTTP Request Handle Touch Events Internet Measure Layout Draw UI Thread

  23. ANR (Activity Not Responding) Error Happens when your UI Thread is paused/blocked too long.

  24. Operations to avoid on UI Thread • HTTP Request • Database Querying • File download/upload • Image/Video Processing

  25. How to prevent ANR? • Let the UI thread do UI logic to allow it to stay responsive and allow interaction with the user. • Use a separate thread for all other things!

  26. Threading in Android • Android supports: • Threads • Thread pools • Executors • If you need to update the user interface, your new thread will need to synchronize with the UI thread.

  27. 2 ways to thread and synchronize • Handler • AsyncTask

  28. Handler • A mechanism that allows a worker thread to communicate with the UI Thread in a thread-safe manner. • Use a Handler to send and process • Messages (a data message) • Runnables (executable code)

  29. AsyncTask • Allows you to perform asynchronous work on the UI Thread • Performs blocking operations on the worker thread • Working thread then publishes results to UI Thread.

  30. AsyncTasks are Easier Than Handlers • AsyncTasks were designed as a helper class around Thread and Handler • You don’t have to personally handle • Threads • Handlers • Runnables

  31. AsyncTask basics • Create a class that subclasses AsyncTask • Specify code to run on the worker thread • Specify code to update your UI

  32. UI Thread Execution with AsyncTask Spawn Thread Do Time Consuming Operation Handle Touch Events Synchronize with UI Thread with results Measure Layout Draw UI Thread

  33. AsyncTask Example publicvoidonClick(View v){ newDownloadImageTask().execute("http://example.com/image.png"); } privateclassDownloadImageTaskextendsAsyncTask<String, Void, Bitmap>{ /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String...urls){ returnloadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protectedvoidonPostExecute(Bitmap result){ mImageView.setImageBitmap(result); } }

  34. 1. Subclass AsyncTask publicvoidonClick(View v){ newDownloadImageTask().execute("http://example.com/image.png"); } privateclassDownloadImageTaskextendsAsyncTask<String, Void, Bitmap>{ /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String...urls){ returnloadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protectedvoidonPostExecute(Bitmap result){ mImageView.setImageBitmap(result); } } 1

  35. 2. Specify code for worker thread publicvoidonClick(View v){ newDownloadImageTask().execute("http://example.com/image.png"); } privateclassDownloadImageTaskextendsAsyncTask<String, Void, Bitmap>{ /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String...urls){ returnloadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protectedvoidonPostExecute(Bitmap result){ mImageView.setImageBitmap(result); } } 2

  36. 3. Specify code to update UI publicvoidonClick(View v){ newDownloadImageTask().execute("http://example.com/image.png"); } privateclassDownloadImageTaskextendsAsyncTask<String, Void, Bitmap>{ /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Bitmap doInBackground(String...urls){ returnloadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protectedvoidonPostExecute(Bitmap result){ mImageView.setImageBitmap(result); } } 3

  37. doInBackground() • Triggered by calling the AsyncTask’sexecute() method. • Execution takes places on a worker thread • The result of this method is sent to onPostExecute()

  38. onPostExecute() • Invoked on the UI thread • Takes the result of the operation computed by doInBackground(). • Information passed into this method is mostly used to update the UI.

  39. AsyncTask Parameters extendsAsyncTask<Boolean, Point,MotionEvent> • The three types used are: • Params, the type of parameter sent to the task upon execution. • Progress, the type of progress units published during the background execution. • Result, the type of result of the background computation.

  40. AsyncTask Parameters • Each AysncTask parameters can be any generic type • Use whichever data type fits your use case. • Not all parameters need to be used. To mark a parameters as unused, use the type Void. privateclassMyTaskextendsAsyncTask<Void, Void, Void>{...}

  41. Your turn with AsyncTask • Create a AsyncTask that uses a String to download an image from the internet and then uses the downloaded image for an ImageView located in the UI. URL url = new URL(url_path); BitmapFactory.decodeStream(url.openStream());

  42. AsyncTaskLifeCycle • When an AsyncTask is executed, it goes through 4 steps: • onPreExecute() • doInBackground(Params…) • onProgressUpdate(Progress…) • onPostExecute(Result…)

  43. onPreExecute() • Invoked on the UI Thread immediately after execute() is called. • Use this method to setup the task, show a progress bar in the user interface, etc.

  44. doInBackground(Params…) • Performs background computation. • Use this to publishProgress(Progress…) to publish one or more units of progress to the UI Thread.

  45. onProgressUpdated(Progress…) • Invoked on the UI thread after a call to publishProgress(). • Used to display any form of progress in the User Interface while background computation is taking place. • Use this to animate a progress bar or show logs in a text field.

  46. onPostExecute(Result) • Invoked on the UI thread after doInBackground() completes.

  47. Use cases for AsyncTasks • Create an AsyncTask to load an image contained in a list item view. • Create an AsyncTask to query the Database • Grabbing JSON from the web

  48. HTTP Request • Ask for permission in AndroidManifest! • Make use of HttpClient • GET, PUT, DELETE, and POST methods supported • Use HttpResponse for server response

  49. HTTP Get HttpClient client = newDefaultHttpClient(); HttpGet get = newHttpGet("http://google.com"); try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); if (null != entity) { String result = EntityUtils.toString(entity); Log.d("CRR", result); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

  50. HTTP Get HttpClient client = newDefaultHttpClient(); HttpGet get = newHttpGet("http://google.com"); try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); if (null != entity) { String result = EntityUtils.toString(entity); Log.d("CRR", result); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpClient object is required for all HTTP Methods.

More Related