android rest client applications-services approach @droidcon bucharest 2012

Post on 25-May-2015

2.920 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presented by Roman Mazur

TRANSCRIPT

Android REST-client applications: services approach

Sharing experience

Case

Case

New startup

Case

New startup

Case

New startup

Case

New startup

Case

New startup

stores

posts sends

provides

notifies suggests

Case

New startup

REST

REST

https://api.twitter.com/1/users/show.json?screen_name=roman_mazur

{

id : 14701612,

name : “Roman Mazur”,

location : “Ukraine, Kyiv”,

}

How to perform requests

@Override

public void onClick(View view) {

URLConnection connection

= new URL(…).openConnection();

}

How to perform requests

@Override

public void onClick(View view) {

URLConnection connection

= new URL(…).openConnection();

}

How to perform requests

• Obviously: not in the main (GUI) thread

• Using either URLConnection or HttpClient

– both have pros and cons

• Choose context: Activity vs. Service

Why not to use activities?

• User controls activity lifecycle

• When all your activities are in background your process is a candidate for killing

• You’ll lose your data

Services: our first implementation

Activity

ApiMethodsSupport (Helper for communication with service)

Service

1. onStart

2. bind 3. registerListener (using binder)

4. performRequest

5. performRequest

6. HTTP GET/POST/…

Services: our first implementation

• Main problem: rotations :)

– we register listener in onStart and remove it in onStop

– what if response is received while we are rotating the screen?

Current implementation

• Loaders! – are awesome since they are not recreated in case

of configuration changes (at least in most cases)

• Custom loader – binds to the service

– registers listener

– performs request

– gets the result

– unbinds

How we perform requests now

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

getLoaderManager().initLoader(1, null, this);

}

How we perform requests now

@Override

public Loader<ResponseData<Profile>>

onCreateLoader(int id, Bundle args) {

return

new SimpleRequestBuilder<Profile>(getActivity()) { }

.setUrl("https://api.twitter.com/1/users/show.json")

.addParam("screen_name", "TwitterAPI")

.getLoader();

}

How we perform requests now

@Override

public void

onLoadFinished(Loader<ResponseData<Profile>> loader,

ResponseData<Profile> data) {

if (data.isSuccessful()) {

Profile profile = data.getModel();

text1.setText(profile.getName());

text2.setText(profile.getDescription());

}

}

How we perform requests now

@Override

public void

onLoadFinished(Loader<ResponseData<Profile>> loader,

ResponseData<Profile> data) {

if (data.isSuccessful()) {

Profile profile = data.getModel();

text1.setText(profile.getName());

text2.setText(profile.getDescription());

}

}

ResponseData

• Result code

• Status message

• User visible message

• Data

Activity Side

• Request builder creates a request description

• Description is passed to a service

– a) as an Intent

– b) with a service binder method performRequest

Service Side

• Either enqueues description processing or performs it in the worker thread using AsyncTask

• Request description builds URLConnection

• Input thread is read, parsed; result is analyzed and then delivered to listeners

Service Side: Response Pipeline

ContentHandler

URLConnection

ResponseDataConverter

ContentAnalyzer

Parsed object

ResponseData

Listeners

Analyzed ResponseData

Conclusions

• Power – requests processing can be easily managed

– requests can triggered by notifications and AlarmManager

• Simplicity – not much to learn if you are familiar with Android

loaders

• Caveats – currently not everything is easy to customize

It’s open source But we lack documentation :)

http://code.google.com/p/enroscar/ and we are preparing to release it on GitHub

Thanks!

Roman Mazur

Head of Android Unit at Stanfy

Kyiv GDD co-organizer

mazur.roman@gmail.com

+Roman Mazur

@roman_mazur

top related