Android, RxJava and Retrofit: A Simple Guide to HTTP Requests 

Making HTTP requests is an important part of Android development. In this article, we will learn how to use RxJava and Retrofit to handle HTTP requests.

Our Agenda!

  1. Create a basic Android app
  2. Add dependencies in build.gradle
  3. Create ApiService — which contains all HTTP endpoints
  4. Create HttpClient — which transfers data between the app and the server
  5. Create SchedulerProvider — which gives access to background and UI threads
  6. Make a HTTP request and update the UI

1. Create a basic Android app

For this guide, we will create a simple Android app with an Empty Activity. Open your Android Studio, on top-left click File ->New Project. Keep clicking Next till you reach the last screen. On the last screen click Finish.

In AndroidManifest.xml, add the INTERNET permission.

<uses-permission android:name="android.permission.INTERNET" />

2. Add dependencies in build.gradle

In app/build.gradle, add the following dependencies and sync the project.

Now that we have all the necessary libraries, we will write the code to make HTTP requests.

3. Create ApiService

In this guide, we will call a GET request on the below url

This api returns a todo item in JSON format.

Create a java model called that represents a todo item.

Create a java interface, This is the service layer of your app. All your HTTP endpoints are defined here.

Every endpoint will have 4 primary parts.

  1. Request type annotation. In our case, GET
  2. An endpoint without baseUrl. In our case, “/todos/1”
  3. Method name. In our case, getFirstTodo()
  4. Return type. In our case, Flowable<Todo>

4. Create HttpClient

Create a java class,

HttpClient is part of the networking layer. The only function it serves is to transport data between the app and client. It has no knowledge if the request is for login, payment or uploading profile image.

5. Create SchedulerProvider

Think of Scheduler as a thread. So that, SchedulerProvider would be something that gives us access to different threads. In this case, SchedulerProvider provides us UI, IO and Computation thread to work with.

For a HTTP request, we want the request to happen in background thread, so that the main UI thread is not blocked due to network. Once, the request is completed, we want to get the result data to be on the UI thread.

Create a java class,

6. Make a HTTP request and Update the UI

With ApiService, HttpClient and SchedulerProvider in place, we are now ready to make HTTP requests.

In your MainActivity, put the code to make the

Final Words

In this article we have covered how to setup RxJava, Retrofit and make HTTP request in a very simple way. In next part of this guide, we will cover some common use cases like passing auth token in headers, uploading a file to server, how to enable body logging etc.

I hope this article provides useful info to get your Android HTTP requests up and running quickly. Till next time! Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *