inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Mar 09 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
Implementing an Android REST Client using Retrofit

Henrique Boregio wrote this on October 10, 2014 under android, inaka, retrofit .

Last time we talked about how to create a simple REST client using the android-async-http library by James Smith. In this post, we're basically going to implement the same functionality using another awesome library: Retrofit.

What is Retrofit?

Simply put, Retrofit is a library that will let you define your API in a simple Java interface and will automagically convert it into a full-blown REST client. If you want more information on what you can do with Retrofit, you can check out their website (Retrofit Website), but for now let's just assume it's an awesome library and jump start to using it.

What will we cover?

Throughout this post, we'll cover the following topics: * How to use a free API to get weather data from a certain city * Setup an interface to represent our API calls * Construct Java POJOs to represent data returned from the API

So, first things first, we need to download and install the library. All we really need is the Retrofit jar in our build path, but it actually makes use of other libraries if available, so in order to get the full benefits of Retrofit, we'll include these other libraries. Just add the following lines to your build.gradle file:

compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okio:okio:1.0.1'

Once those libraries are downloaded and setup (just hit the "Sync Project with Gradle Files" in Android Studio), we can begin our work.

The API endpoint we're going to use is the following: http://api.openweathermap.org/data/2.5/weather?q=California

This returns a bunch of weather-related data for, in our case, California. For the sake of simplicity, we'll only use some of those values in our app, but before we do that, let's start by defining a Java interface to describe this API. We do this by creating a new java class (Api.java) with the following content:

public interface Api {

  @GET("/weather")
  void getWeather(@Query("q") String cityName, 
          Callback<WeatherResponse> callback);
}

Basically what we're doing is converting the API URL into a format Retrofit understands. As you can see, we're making a GET call to the /weather endpoint, sending it a customizable query string (E.g. q=California) and expecting a callback of type WeatherResponse as a result.

Retrofit will automatically use Gson to convert the JSON response to a WeatherResponse object. To define this object, we can simply use those values of the API response we're interested in. Since there's a lot of data in this JSON, we'll also construct a secondary class to represent the data we want:

public class WeatherResponse {
    private int cod;
    private String base;
    private Weather main;

    // default constructor, getters and setters
}

publc class Weather {
    private int id;
    private String main;
    private String description;

    // default constructor, getters and setters
}

That will do for now, but if you wanted to, you could create your own classes to represent the entire JSON returned in that response (E.g. wind, cloud, etc). As long as you keep your member variable names the same as the JSON fields, GSON will make the convertion from JSON to java model automatically.

Now that we've got our interface and our data model setup, it's time to create the actual REST Client. For that, we'll create a new singleton class (RestClient.java):

public class RestClient {

  private static API REST_CLIENT;
  private static String ROOT = 
          "http://api.openweathermap.org/data/2.5";

  static {
    setupRestClient();
  }

  private RestClient() {}

  public static API get() {
    return REST_CLIENT;
  }

  private static void setupRestClient() {
    RestAdapter.Builder builder = new RestAdapter.Builder()
     .setEndpoint(ROOT)
     .setClient(new OkClient(new OkHttpClient()))
     .builder.setLogLevel(RestAdapter.LogLevel.FULL);
        
     RestAdapter restAdapter = builder.build();
     REST_CLIENT = restAdapter.create(API.class);
  }
}

So, the juicy part is actually inside the setupRestClient() method. This is where we setup Retrofit to use the interface we just recently created to define our endpoints. Now, let's see how everything comes together from inside an Activity:

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   RestClient.get().getWeather("California", new Callback<WeatherResponse>() {
     @Override
     public void success(WeatherResponse weatherResponse, Response response) {
       // success!
       Log.i("App", weatherResponse.getBase());
       Log.i("App", weatherResponse.getWeather().getMain());
       Log.i("App", weatherResponse.getWeather().getDescription());
       // you get the point...
     }

     @Override
     public void failure(RetrofitError error) {
       // something went wrong
     }
   });
  }
}

All we must do is get an instance of our RestClient from our singleton and we have direct access to any methods we define in our interface (Api.java) class. Also, the actual API calls are made off of the UI thread and delivered to us on the main thread, where we can safely use those values to update our UI widgets.

Conclusion

This is it for now. Stay tuned for our next post, where we'll build from this example and add more functionality to our RestClient, especially by adding custom logging, headers, basic authentication and global handlers to handle common errors such as a loss of connectivity or internal server errors.