The best of both worlds

I’ve been using Volley – Google’s HTTP library – for a while now . Working on a daily basis with JSON, I was missing a fantastic feature that – another great library – Retrofit has. The capability of parsing the JSON response directly to a java object. HTTP request goes in, Java parsed object comes out. As simples as it sounds.

So, I made two custom requests that try to achieve the retrofit feature using Volley and Gson “A Java library to convert JSON to Java objects and vice–versa”. For simplicity’s sake of the example application, both gson lib and volley lib were already included in the libs folder. One can get them on https://github.com/google/gson and https://android.googlesource.com/platform/frameworks/volley.

The application is pretty simple. One activity with two fragments in split screen. One of the fragments calls for a JSONObject, and the other one calls for a JSONArray. When the request is correctly parsed, a toString() puts the content of the parsed class inside a TextView.

The sample application can be found in https://github.com/ivocosme/gsonenhancingvolley

Leaving AsyncTasks and other “lower level thread like” approaches was one of the best decisions that I could have taken as a developer. Code got cleaner, reduced complexity, no more “if != null”, among other things.

Ivo Cosme

Ivo Cosme
Ivo is a Mobile Software Engineer. With a few professional years now, lots and lots of hard work and developer dedication, he’s now like most Senior Software Engineers out there... He knows that the things he does know are (yet!) far from the amount of things that he does not know.
In programming, the hard part isn’t solving problems, but deciding what problems to solve. - (Paul Graham)

Why are you RESTing when you can be up and querying?

This post brings a quick (non-exhaustive) glance of two approaches (Falcor & GraphQL) that can leverage your game acessing your data exposed through an API. Continue reading