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
How to use Retrofit to consume a Web API
How to work with Gson to deserialize JSON data
How to use a Retrofit client in conjunction with MVVM and the repository pattern
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:
Consider
For simplicity, it is recommended that you start with an unrestricted Web API, or at least one that does not require OAuth.
What is a Web API?
What does RESTful mean?
What kind of data can your Web API return? How is it structured?
Is the API well-documented?
Include the Gradle dependencies for Retrofit & your chosen converter, & declare the permissions you are going to need in the manifest.
Consider
What is Retrofit?
How does Retrofit compare to OkHttp?
Do you know of any other networking libraries for Android?
What permissions do you need to use Retrofit?
What converters can you choose between?
Have a look at the example project on Github
Create an interface with methods that define how you can interact with the Web API. For starters, enable a GET-request.
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);
}
Consider
Are you using the proper annotations for the methods and parameters?
What kind of HTTP-requests can you make?
What does @Path
& @Query
do? @Body
?
Why is the return type wrapped in a Call
object?
How do you send custom data with in the header?
Create a Response-class, that helps with converting your data into Java objects, that you can use in your application.
Consider
How do you map data from JSON to a Java object with Gson? Any important annotations for this?
What is JSON? What is Gson?
Do you have to convert all of the received JSON data?
How do you access nested JSON objects?
How do you access JSON arrays?
Create a ServiceGenerator singleton, that abstract away the instantiation of your API.
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;
}
}
Consider
What is the base URL?
What is the purpose of the converter factory?
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.
Consider
Are you using enqueue() or execute() on the call object? What is the difference?
What happens if you try to execute the call on the main thread?
How do you retrieve the data from a request?
Are you using HTTP-error codes?
Use your API in conjunction with MVVM and a repository.
Consider
Are you storing the data from the Web API in MutableLiveData?
Why can you only use postValue() and not setValue() on the LiveData?
Are you accessing the API directly in the Repository, or have you created a Client class to abstract away the management of the call?
Already done? Expand on the functionality of your application, or try out one of these:
Find out what WebAPI to use for your app project
Use an API that requires OAuth or an API Key
Post serialized objects to a Web API
Display data from a WebAPI in a list
Use Glide to display images from a Web API
Try out Postman to test WebAPIs or mock your own!
Explore more advanced concepts in the Retrofit youtube playlist or the Gson youtube playlist
Consider
What is is an API Key?
What is OAuth?
What is an access token?
How do you serialize Java Objects to JSON?
What is Glide?
What is Postman?