Faced on a project with an unseen before problem.In this article I will tell you how using RxJava and Retrofit 2 – you can solve the problem of creating an Odata client for an Android application.

Thank you so much Jake Wharton for creating such comfortable tools.

Welcome to the world of magic

We have an application that, according to the Odata protocol, must extract data from the server, display it in lists that should be loaded as it scrolls, and send the data created by the user to the server. A trivial task, but it wasn’t here, what works without problem in Java – does not want to work with android as well.

And libraries and documentation on Odata only from Apache – Olingo and Microsoft in C #.

And here the most interesting part begins, Odata is a kind of SQL in the REST API, and that’s what it is for dynamically generated data.

But we have a strongly typed language and without knowledge of the model – data processing and storage create a rather difficult task.

The solution of which cannot be typical and repeatedly described on the network.

Now let’s do the magic:

Create a network service

We will use Retrofit 2 and related Square products

All of these dependencies are the most powerful network library in Java.

So, the Poduct data model is stored on the server, which has some parameters and attributes. All data goes in JSON format and for work we need a POJO class.

I recommend that you configure the HttpLoggingInterceptor to set the level of interception detail – Level.BODY.

We make a listing 4 request in order to maximize the data structure and the answer will be approximately in this format:

listing 5

And already on the basis of this data it is possible to form a request for further data manipulations.

It means, it is a full-fledged object with some kind of behavior and attributes, in order to get this information when creating a request, it is necessary to add conditions on the basis of which we will receive our DataSource without inventing a new bicycle and without screwing crutches to it.

The climax and puppy delight of the comfort of the instrument

Now you can get properties using the Odata service document:

listing 6

Here it is, the strongwoman the heroic library of Retrofit 2. Dynamic Url takes on all of this mass of parameters with which you can play in the code for as long as there is enough imagination.

Summary

It was a useful experience, which I decided to share, in due time this article would really remove a bunch of problems and questions.