Today, you will learn how to communicate with Web APIs. We will use Retrofit - a type-safe HTTP client library for Android. It is an abstract layer on top of the networking layer, that makes it easier to do HTTP requests. It also works in conjunction with converters such as Gson, that can serialize and deserialize your JSON data.

What you'll learn

Today, you decide what kind of application you want to develop. Choose a Web API, that you will communicate with in your application.

Here is some inspiration:

Include the Gradle dependencies for Retrofit & your chosen converter, & declare the permissions you are going to need in the manifest.

Create an interface with methods that define how you can interact with the Web API. For starters, enable a GET-request.

PokemonApi.java

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface PokemonApi {

   @GET("api/v2/pokemon/{name}")
   Call<PokemonResponse> getPokemon(@Path("name") String name);
}

Create a Response-class, that helps with converting your data into Java objects, that you can use in your application.

Create a ServiceGenerator singleton, that abstract away the instantiation of your API.

ServiceGenerator.java

public class ServiceGenerator {
   private static PokemonApi pokemonApi;

   public static PokemonApi getPokemonApi() {
       if (pokemonApi == null) {
           pokemonApi = new Retrofit.Builder()
                   .baseUrl("https://pokeapi.co")
                   .addConverterFactory(GsonConverterFactory.create())
                   .build()
                   .create(PokemonApi.class);
       }
       return pokemonApi;
   }
}

Create an interface to hold the data from the Web API, and test a request from your Main Activity, to see that your API client works.

Use your API in conjunction with MVVM and a repository.

Already done? Expand on the functionality of your application, or try out one of these: