Fetching data from a network endpoint
For the purpose of this section, we will use The Cat API (https://thecatapi.com/). This RESTful API offers us vast data about, well…cats.
To get started, we will create a new project. We then have to grant our app internet access permission. This is done by adding the following code to your AndroidManifest.xml
file right before the Application
tag:
<uses-permission android:name="android.permission.INTERNET" />
Next, we need to set up our app to include Retrofit. Retrofit is a type-safe library provided by Square, which is built on top of the OkHttp
HTTP client. Retrofit helps us generate Uniform Resource Locators (URLs), which are the addresses of the server endpoints we want to access. It also makes the decoding of JSON payloads easier by providing integration with several parsing libraries. Sending data to the server is also easier with Retrofit, as it helps with encoding the requests.