Friday, April 8, 2016

Slides: Build an efficient REST Client on Android - Matteo Gazzurelli - Droidcon IT 2016 - Torino


I’ve got to ask you a few questions before starting:
  • Do you have an app that consumes a REST endpoint?
  • Do you still use plain HttpUrlConnection to connect to a server?
  • Have you ever parsed a JSON string by hand?
Well, if you answered YES to one of these questions, This is the right talk for you!
Otherwise, it's still ok, and you’ll learn how to implement a good REST client in Android.
If you are wondering… what the hell is he saying… ok, thank you for staying!

An Http 2 Endpoint (http is good, but this is new, and fast!)
Caching, don’t stress out your server
And a good rest client (not all are good)
Http2: it is the new revision of the popular Web Protocol HTTP.
It is based on SPDY (a google protocol) but now is maintained by the HTTP working group.
It is different from the 1.x version  because of a few factors: 
  • It's binary, instead of textual
  • It is fully multiplexed, instead of ordered and blocking
  • It can therefore use one connection for parallelism
  • It uses header compression to reduce overhead
  • It allows servers to “push” responses proactively into client caches
It doesn’t require encryption, but it is strongly recommended , in fact it uses TLS.

But HTTP/2 Alone is not sufficient, we need help from the Caching system.
Mainly there are two types of caching systems. One is good for Dynamic apis and one is good to serve static APIs.
For dynamic apis you have to rely on a server side application such as APC, Varnish, Memcache.

For static APIs you can rely on the same system or there are a few HTTP headers you can use to specify caching rules for your resources.
Expires: Used in HTTP responses, the Expires header tells the client when the resource is going to expire.
Cache-Control: CC is a powerful header to manage your caching directives and strategies: it's a way of combining different directives about the response’s cache.
ETag: The etag is a unique identifier for your response: it is used on conditional requests, usually when a client gets a resource he also has in cache, sending to the origin server this identifier.
Last-Modified: If Date is the header which tells when the resource has been firstly generated, Last-Modified tells us when it has been… ..well, you guessed it.
And now let’s go directly to the point…

What is REST? Rest Stands for Representational State Transfer. 
It's not a language but an architectural style.
It is platform and language independent and It consists in a coordinated set of architectural constraints applied to components, connectors and data elements.

In summary, the five key principles are:
  • Give every “thing” an ID
  • Link things together
  • Use standard methods
  • Allow Resources with multiple representations
  • Communicate statelessly
It’s not the purpose of this talk to go in depth about rest, but how to implement a REST Client in Android.
There are some Android HTTP Clients out there

The plain DefaultHttpClient, Volley (from Google), Spring, Retrofit and many more…
Choosing a rest client for your app is like finding the right partner, each one has a particular style that makes you fall in love with her, but there is only one partner who completes you.
During my experience I’ve tried some of them, but in the end I chose Retrofit.

Retrofit is a Type-Safe Http client for Android. It is a simple HTTP client that uses annotations to:
describe HTTP requests, URL parameter replacement and   query parameter support is integrated by default. 
Additionally, it provides functionality for multipart request body and file uploads.
(for the file download I suggest you  use the Download Manager)
Retrofit ships with support for OkHttp's RequestBody and ResponseBody types but the library is content-format agnostic.

These are the existing Retrofit converters and you can use them directly by integrating the respective gradle dependencies
In the event that you need or want to create your own Retrofit response converter, it is possible, but it is an extensive process.
The most popular converter used is Gson, a JSON serialization and deserialization library that uses reflection to populate your Java model objects from JSON objects.
“If you have a small JSON, the GSON, gives the best result, but for large JSON, Jackson is better.”

Let’s now take a practical look at how retrofit could be implemented in our app.
It is pretty simple, you have to 
  • Create a POJO/JavaBean Model 
  • Create Retrofit Interface
  • Create the Call object
  • Execute the request
  • Use the result
This is a simple POJO Java model.
(A JavaBean follows certain conventions. Getter/setter naming, having a public default constructor, being serializable etc. A POJO (plain-old-Java-object) isn't rigorously defined)

Retrofit turns your REST API into a Java interface! this is the interface.
You must declare the endpoints as annotations, and always as annotation you can define the headers.

After this, go to you Activity and Create the retrofit instance, create the Call object and use it through the two callbacks provided.
Just a note, keep in mind that in retrofit2 onResponse is still called even if there is a problem with the response. I mean, if the response is 404 Not Found, the OnResponse callback is called and you can retrieve the error body from the response object (response.errorBody().string()). 
OnFailure is called only if the communication fails, but if the server responds, onResponse is called.

Retrofit permits you to make a synchronous or Asynchronous call, but remember that a network call must not be performed on the main thread.
If you find out that a call is not necessary, you can cancel it without problems. This thanks to the second release of Retrofit, the first version didn’t allow this.
Retrofit finally got the second major release in March of 2016.

Retrofit 2 comes with various fundamental changes and also includes breaking changes to the internal API. 
They changed the package name in order to avoid conflicts, there is a simple way to declare a service with just a single pattern. A big improvement is the introduction of a way to cancel the ongoing transaction. Retrofit 2 comes with a new URL resolving concept, more similar to the html tag href, a good pattern is to make the base URL: always ends with / and the interface endpoints DO NOT start with /
In this new version Okhttp is now required and is automatically set as a dependency.
But what is OkHttp

OkHttp is a modern, fast and efficient Http client which supports HTTP/2 and SPDY and does a lot of stuff for you. 
Reading how many things OkHttp does - is a good way to understand how hard networking is: Connection pooling, gziping, caching, recovers from network problems, sync and async calls, redirects, retries … and so on.
OkHttp is a very capable networking tool out of the box, without the need of any REST library and it probably is the library most developers would choose if they could only include one library in their projects.

OkHttp sits on top of Okio, a library that complements and java.nio to make it much easier to access, store, and process your data. It provides fast I/O and resizable buffers.
OkHttp can be used in many libraries such as Volley and can be used as an http client for Picasso, glide, fresco…

Since the HTTP connection layer is in OkHttp you can use its interceptors.
Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls.
Interceptors can be chained.
Suppose you have both a compressing interceptor and a checksumming interceptor: you'll need to decide whether data is compressed and then checksummed, or checksummed and then compressed. 
OkHttp uses lists to track interceptors, and interceptors are called in order.

Here’s an example.
You have to use the retrofit builder and add your own OkHttpClient with its interceptors.

The most used interceptor is the Logging interceptor.
It is an OkHttp interceptor which logs HTTP request and response data.
The logs generated by this interceptor when using the HEADERS or BODY levels has the potential to leak sensitive informations, don't forget to disable them in production level environment. 

In this talk, based on my experience, I will tell you about two levels of caching.
The first with OkHttp (a soft caching) and the second is a more persistent cache, with a database.

As we used OkHttp as a network layer, we use it as a caching bucket.
Response caching uses HTTP headers for all configurations. 
You can add request headers like Cache-Control and OkHttp's cache will honor them.
As I mentioned before, there are cache headers to force a cached response, force a network response, or force the network response to be validated with a conditional GET.

To cache responses, you'll need a cache directory that you can read and write to, and a limit on the cache's size. The cache directory should be private, and untrusted applications should not be able to read its contents!
This is what I call soft caching. What I mean with persistent caching is related to storing data in a persistent storage

There are many android storage options:
  • Shared Preferences
  • Internal/External Storage
  • Sqlite 
  • and Realm
You have to choose the right one in order to improve the performance on your app
In this case, Realm wins hands down.

Realm is the first database built from the ground up to run directly inside phones, tablets and wearables, it's not an ORM on top of SQLite. Instead it uses its own persistence engine.
Realm uses very little resources, is incredibly easy to use, and lets you interact with your data faster than any current alternative.
It is cross platform and it supports advanced features like Encryption, graph queries, and easy migrations.
Realm data uses very little disk space and requires no serialization & deserialization, and because it’s cross platform, you’ll get much better performance passing realm files around than shipping SQLite databases or chunks of JSON.
To Get Started check out the link on this slide.

Realm is very Fast, is still up to 100x faster than some SQLite ORMs and on average 10x faster than raw SQLite and common ORMs for typical operations.

A core feature I’d like to tell you about is the Realm Proxy classes. Realm generates a proxy class for each RealmObject at compile time. These objects themselves don’t contain any data, but instead access the data directly in the database.
This is useful when for instance we have a list and our content changes frequently.
How can we integrate it with Retrofit?

In order to use it in our environment, we have to look at the doc, which says: Retrofit does not automatically add objects to Realm, instead you must manually add them using the realm.copyToRealm() or realm.copyToRealmOrUpdate() methods.

Here you are! In the OnResponse callBack check if the response is successful and create the transaction.

I’ve made a short digression about how to create an HTTP/2 backend without knowing a lot of backend programming.
The purpose is to create a static endpoint that serves a static json. It’s not so common but sometimes it is enough. 
In order to do this, AppEngine will come in handy. Go to and create a new project.

For simplicity's sake I’ve chosen Python with Flask. It is very easy, change the project-id in the sample app, edit a few Lines of code so the backend can read the data from the disk and serve it as an application/json response.

Go to the start page of AppEngine and download a sample app in the language you prefer. 
AppEngine supports Python, Java, Php and Go.

To conclude my talk I’ve made a Demo…

Thank you

We are hiring! 
Read More »

Wednesday, March 23, 2016

Droidcon Italy 2016

Droidcon Italy 2016 will be in Turin. Among the speakers Matteo Gazzurelli, CEO of DuckMa

Droidcon is one of the largest conferences at European level, about the Android world.
Landed in Italy two years ago, thanks to Synesthesia, has once again chosen to be hosted in Turin, where it will be held at the Lingotto Conference Centre, on 7 and 8 April.

What is Droidcon?

The main idea behind all droidcon events is to support the Android platform and build up a networking event for developers and companies.
Droidcon offers high-class talks covering different aspects of the ecosystem, including core development, embedded solutions, augmented reality, business solutions and games.
In the exhibition area partecipants and sponsors show their latest hard- and soft- ware solutions, including phones, tablets, consoles, gadgets and, of course, SDKs, APIs, apps and widgets for business and consumer purposes.

Among the speakers at this edition also Matteo Gazzurelli, founder and CEO of the App agency DuckMa.
The 31 years old italian developer started to develop apps for the Android back in 2009, first as an employee and then as a contractor. In 2013 he decided it was time for him to fund his  company, DuckMa.

During the last year Matteo and his contributors in DuckMa specialised their competences on iBeacon technology working with some leading edge customers who operate in the proximity marketing field.

At this Droidcon, April 8, at 10.20, Paris room , Matteo will explain how to build an “efficient REST Client on Android”. “It is important to limit and optimise network traffic in mobile communications. A well designed rest client is the best you can do in order to save network bandwidth and increase your online app responsiveness”.

The talk is going to be focused on how to create an optimal rest client with persistent cache in Android using HTTP/2, Retrofit, OkHttp and Realm while relying on an AppEngine hosted backend.

Check the schedule here:
Read More »

Monday, March 7, 2016

Ladies and Gentlemen... DuckMa Inc.!

Picture by Jared Erondu
Leggi in Italiano
A birth is a new seed and should be planted in a good soil in order to grow strong roots.
The new American "twin" of DuckMa is just born: DuckMa Inc.
We decided to push overseas, not only to have new headquarters in the Silicon Valley, a dream of every digital company, but also for what that place offers.
What? is it? A more mature market: customers in all the relevant levels, giving much more importance to mobile solutions, knowing what they are buying and being convinced of the great potential of the applications world.
Not just that, it's difficult for an Italian company to work abroad, and the best strategy is to have a base on the territory.
The long-term idea idea is to create a support and development team, so that the employees are more motivated to work and influenced by a sparkling ecosystem.
Finally, USA is the ideal breeding ground for the launch of our new product,, a mobile CMS thought for UI/UX agencies that allows anyone to build their own app.

The new US headquarters were incorporated in Delaware but now the headquarters are in San Francisco, where some of our customers are, and where there are more opportunities for growth for a company that is developing high-level applications.

And for those who think that opening a company in the US is a steep and winding road, well: you're wrong. It was very simple, both to incorporate the company and open the bank account.
The key is finding a competent attorney, helpful and very experienced like Matteo Daste. In a few days you can have the company up & running.
Then the game begins.

If you are wondering who we are and what we do:
DuckMa is a mobile focused design and software boutique primarily working with startups and leading brands. We are a group of passionate professionals who make handcrafted mobile native apps.
We are growing a lot and thanks to our structure we are currently able to support medium to enterprise customers, who require dedication to the development of highly accurate code and wish the best for their Mobile apps.
Generally we work closely with our customers on brand new projects.
We can also work as project bootstrapping, we take care of the app engineering, we build the initial frame of the project and then there are two possible roads:
either we work alongside the customer and carry out the project or we can build together with the customer a development team, train them and support the client only when needed.

Read it in English

Una nascita è un nuovo seme e va piantato in un buon terreno perché crescano forti radici.

È appena nata la “gemella” americana di DuckMa: DuckMa Inc.
Abbiamo deciso di spingerci oltreoceano non tanto per avere una sede nella Silicon Valley, sogno di ogni società del settore digital, quanto per quello che, banalmente, quei luoghi offrono.
Che cosa? Un mercato più maturo, clienti di tutti i livelli più competenti, che danno molta più rilevanza alle soluzioni mobile, che sanno cosa stanno acquistando e sono convinti del gran potenziale del mondo app.
Non solo, è difficile per un'azienda italiana lavorare all'estero e la migliore strategia è quella di avere una base sul territorio.
L'idea a lungo termine è di creare un team di supporto e di sviluppo così che i dipendenti siano ancora più motivati a lavorare e influenzati da un ecosistema frizzante.
Infine gli USA sono il terreno ideale per il lancio del nostro nuovo prodotto,, un CMS mobile pensato al momento per le agenzie di comunicazione che consente a chiunque di costruire la propria app.

La nuova sede statunitense è stata incorporata in Delaware ma ha sede a San Francisco, dove risiedono alcuni nostri clienti e dove ci sono più possibilità di crescita per un'azienda che fa sviluppo app di alto livello.

E chi pensa, che aprire una company negli USA, si tratti di una strada ripida e tortuosa, beh, si sbaglia. È stato molto semplice, sia incorporare l'azienda sia aprire il conto in banca. Fondamentale è trovare un avvocato competente, disponibile e con molta esperienza come Matteo Daste. In pochi giorni si riesce ad avere l'azienda up & running.
Lí poi inizia il gioco.

Se vi state chiedendo chi siamo e cosa facciamo:
DuckMa e' una boutique specializzata nello sviluppo di app native, stiamo crescendo molto e grazie alla nostra struttura siamo attualmente in grado di affiancare clienti di medio grandi dimensioni, che richiedono una dedizione allo sviluppo del codice molto accurata e che pretendono il top per le loro app.
Generalmente lavoriamo a fianco del cliente su progetti a lungo termine.
Possiamo anche lavorare come bootstrap di progetto, partiamo dall'ingegnerizzazione dell'app, stendiamo l'incastellatura iniziale del progetto, decidendo poi se affiancare il cliente portando avanti il progetto stesso oppure se costruire assieme un team di sviluppo, formarlo e dare supporto al cliente solo nel momento del bisogno.
Read More »

Thursday, February 11, 2016

Top traits of a great mobile developer

Top Traits of a great mobile developer HEADER

Creativity , empathy , listening , vision: the skills of a perfect developer aren’t only technical.
Companies believe that the ability to write good code, that makes a product come to life, is at the heart of their success.
The current emphasis on these skills seems totally rational, but what companies forget is that this won’t be true forever.
In the future, computers will take over more and more of these tasks, including programming and data crunching.
What can’t be replaced in any organization imaginable in the future is precisely what seems overlooked today: liberal arts skills, such as creativity, empathy, listening, and vision.

So, a good developer should not only have great technical skills, but also the right behavioural and “human” characteristics such as:

  • Developing applications shouldn’t be just a job, but a passion.
  • Our job is driven by creativity : a good developer doesn’t work only 8 hours a day. A good developer never gives up before the process/job has been completed.
  • Thinking out of the box.
  • Listening to advices by more expert developers.
  • Self learning.
  • Should always be open to learning, from the beginning to the more senior stages.
  • Should be always ready to get in the game.
  • Should be customer oriented.
  • Should love to share knowledge, get involved in new projects, be part of the team effort in streamlining the tools/practices.
  • Don’t fear the reaper: you should always be ready to throw away some legacy stuff (even if yours), or start from scratch, to improve the quality of the project.
Important technical skills:
  • Computer science: data structures and algorithms. Object oriented programming.
  • The App life cycle and thread management: multithreading (queue, dispatch, operation,...), main thread, etc...
  • Good Knowledge: UI Guidelines, Frameworks, Sensors (platform-specific).
  • Persistence, ER basic concepts (helpful to model the application domain), basic concepts about architectural patterns (e.g. MVC) and some basic concepts of enterprise design patterns (e.g. version number pattern).
  • Caching: HTTP headers, server-side, client specific cache mechanism.
  • RESTFul. In general a web services overview : XML RPC and SOAP.
  • Familiar to concepts such as Parsing, Mapping, Serialisation.
  • Main Design Patterns and design principles (observer/delegate, factory, singleton, dependency injection, programming by interface...).
  • Dependency management fan.
  • Essential Git/DVCS.
  • Basic Memory Management.
  • Wireframing/Prototyping.
  • Knowledge of Profiling tools and debugging skills.
  • Unit testing/Refactoring practitioner.
Are you a great mobile developer? check out our careers page:

Read More »
DuckMa - Copyright© 2016