Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

Android Bootcamp Week 5

Data and Communication

https://github.com/ThoughtWorksAustralia/AndroidBootcampProject#week-5-data-and-communication

This week:

  • We will introduce and discuss the options available in Android when using the network
  • We will then write some code to download the treasure photos from a given server

Last week:

  • We discussed the options available to use the camera
  • We wrote some code to take a photo
  • We then calculated the distance between the photo and the given treasure

Retrospective...

Next time on

Android Bootcamp:

Google APIs

Next time...

HTTP Clients

  • What did we do well?
  • What could we do better?
  • What puzzles you?

Android ships with two built in HTTP clients

  • Apache HTTPClient - Large API - built in from 1.0
  • HTTPUrlConnection - Lighter, smaller API, recommended.

Something better?

Open source

OKHttp is a third party HTTP client by Square which adds valuable functionality

  • SPDY support
  • Connection pooling
  • GZIP support
  • Caching
  • Error retrying

http://square.github.io/okhttp/

Threads

That's not all...

Android uses a single threaded model

  • Your application is launched into its own "main" thread
  • AKA the UI Thread
  • Handles drawing and handling user interaction

What could go wrong?

Running long tasks on the UI thread will cause your application to hang so...

For any task that is not UI related you should create a worker thread

  • Spawn a thread to do your work
  • Once complete update the UI with results

But I don't like Threads

Get back to the UI Thread!

There is another way without using Threads directly

AsyncTask

  • Performs task in a non blocking worker thread
  • Joins back onto UI Thread when complete

HttpURLConnection

Three options to rejoin onto the UI thread from another thread

  • Activity.runOnUIThread(Runnable)
  • View.post(Runnable)
  • View.postDelayed(Runnable, delay)

Worker Threads

Real Treasures!

Some examples

2 Rules!

  • Dont block the UI Thread!
  • Do not access UI components outside of the UI Thread!

Default HTTP Client

Exercise 2 pt 1 - Writing the client

1. Create an interface called TreasureService in the util package

Exercise 1 - Getting the photos

A REST service for the treasures lives at

We're going to get the photos for real this time, off a real web service! Let's prepare the app up to use Retrofit.

http://android-bootcamp-rest-server.herokuapp.com/

Whats wrong here?

1. Inspect the API. The treasure list lives at /treasures

1. Set up the Internet permission in the Manifest

2. Create a class called Properties in the util package to hold app-wide properties, like our service URL.

2. Write a model class for the treasures.

Exercise 3 pt 2 - Fixing the client

<uses-permission android:name="android.permission.INTERNET" />

PROTIP

At this point you should have real treasures loading into the app. However it will crash when you take a photo - its trying to load the EXIF data from the remote images as if they were a local file. Lets use the co-ordinates provided by the web service.

2. Add retrofit to the build.gradle

http://www.jsonschema2pojo.org

compile 'com.squareup.retrofit:retrofit:1.5.0'

OKHttp

Quite a bit of boilerplate code required here

This will generate the Model class for you.

1. Paste in the treasure JSON

2. Set it to JSON source type

3. ???

4. Generate!

1. Change the 'String mSelectedTreasurePath' variable to

'Treasure mSelectedTreasure.

Could potentially save some code here using apache commons io

2. Write a function which extracts the latitude and longitude from a Treasure object into a Location object

3.Change onActivityResult to use it.

Exercise 3 pt 1 - Using the client

Time to use our shiny new TreasureService client!

Create a new AsyncTask in TreasureListFragment's onViewCreated which fetches the treasure list before creating the adapter.

Networking in mobile

Exercise 2 pt 3 - Writing the client

Almost every mobile app depends on network connectivity to fully function!

1. Adjust TreasureListAdapter to work with List<Treasure> instead of List<String> (don't forget to update the unit test).

JSON

2. You'll notice that the image urls returned by the service are relative, not absolute. Add a stub function which, given a Treasure, constructs a full url for the image url.

JSON - JavaScript Object Notation

3. Write a failing unit test for this function, implement the function, and pass!

A data interchange protocol based on a subset of the JavaScript language.

Commonly used in REST APIS for its lightweight and readability.

Exercise 2 pt 2 - Writing the client

Sample pictures are SO week 3. Prepare to replace the TreasureLoader with our new TreasureService client.

1. In the TreasureListFragment onCreate, remove the creation of treasureLoader.

2. In its place, use Retrofit to build our TreasureService

The slow way - Writing a REST Client

1. Construct URL for query

2. Spawn thread / AsyncTask

3. Use HTTP client to download the JSON data

4. Parse JSON data into an object or dictionary

5. Rejoin main thread and use data

The fast way - Using Retrofit

A library by Square (again) which generates a client for your REST API.

1. Document REST API

2. Generate client

3. Request into object

design by Dóri Sirály for Prezi

Learn more about creating dynamic, engaging presentations with Prezi