- Android Studio
- Swagger Codegen
- Retrofit (http://square.github.io/retrofit/)
- RxJava (https://github.com/ReactiveX/RxJava)
- Renderers (https://github.com/pedrovgs/Renderers)
This sample makes use of code generated from the Oxford Dictionary API Swagger documentation - take a look at https://github.com/psh/oxford-dictionary-api-code-gen for the steps to generate clients in a variety of other languages.
Retrofit REST calls to the Oxford Dictionary API look like you are making a simple method call, for instance
entriesApi.getDictionaryEntries("en", searchTerm, BuildConfig.APP_ID, BuildConfig.APP_KEY);
RxJava makes it a breeze to process the highly nested data structure that is returned from the service call
entriesApi.getDictionaryEntries("en", searchTerm, BuildConfig.APP_ID, BuildConfig.APP_KEY)
.doOnSubscribe(d -> hideKeyboard())
.flatMap(re -> Observable.fromIterable(re.getResults()))
.flatMap(he -> Observable.fromIterable(he.getLexicalEntries()))
.flatMap(le -> Observable.fromIterable(le.getEntries()).map(e -> new CategorizedEntry(searchTerm, le.getLexicalCategory(), e)))
.flatMap(ce -> Observable.fromIterable(ce.entry.getSenses()).map(s -> new Definition(ce.category, ce.word, ce.entry, s)))
.toList()
.observeOn(AndroidSchedulers.mainThread())
.map(this::createAdapter)
.subscribe(this::updateRecyclerView);
There are a number of points to note:
- The
Observable
chain doesnt execute until a call is made to subscribe(). ThedoOnSubscribe()
lambda is therefore calls immediately before the service call kicks off. - The Retrofit interface was configured to automatically run on a background thread, so we switch back to Android's main thread again for the last couple of steps (by calling
observeOn()
) before we touch any GUI components. - The APP_ID and the APP_KEY are externally defined in the build.gradle file.
- Walking down the chain of data from
RetrieveEntry
toHeadwordEntry
toLexicalEntry
toSense
would normally result in a deeply nested set of loops. Transforming that processing into anObservable
chain makes the code much more readable. - For simplicity this sample omits error handling. That said, it's pretty easy to supply an additional
Consumer
to the subscribe() call that will allow you to catch errors that might arise.
Writing the adapter for a RecyclerView
can be a lot of work. The Renderers library allows you to focus on the parts of the process that are most important leaving the library to take care of the rest. This sample doesnt quite do the library justice as there is only a single type of row in the RecyclerView
!