Working with Retrofit

Learn how to work with the Retrofit and OkHttp networking libraries.

Networking in android

Here, we’ll dive a little deeper into how Retrofit works in Android applications. So far, we’ve used the HTTP methods to call our API endpoints in the interface. Now, we’ll use networking libraries to convert the REST API to Java interface in JSON format to be loaded into our application.

Before we start looking at different components and libraries used in Android networking, let’s first learn about the networking process in Android.

HTTP

The Android networking layer revolves around making hyper text transfer protocol (HTTP) requests and responses because HTTP is the fundamental networking protocol used to connect to the internet. Regardless of whether we’re using the web, mobile, or IoT apps, we’ll still need this protocol.

HTTP (Hyper Text Transfer Protocol)

Android networking layer revolves around making HTTP requests and responses because HTTP is the fundamental networking protocol used to connect to the internet. Whether you are using the web, mobile, or IoT apps, you will still need this protocol.

Regarding HTTP requests and responses: we have status codes returned to help us figure out what’s happening to our request. If there’s a success or failure in our responses, we’ll be able to get to the root of our problem.

These are the status codes:

  • 1xx, Continue: Wait for the request to finish or ignore the response if not already finished.
  • 2xx, OK: The request succeeded.
  • 3xx, More than one response: The response has multiple choices, and the user has to choose the one they’re interested in.
  • 4xx, Bad request: The response is unsuccessful due to client error, or the server just can’t fulfill the request.
  • 5xx, Internal server error: An error from the backend meaning that the server is unable to fulfill the request or can’t get a response in time.

These codes come in handy when we’re testing our backend services or the APIs for a response to avoid errors during production or when the user is making requests to the server.

Get hands-on with 1200+ tech skills courses.