Book Image

RxJava Essentials

By : Ivan Morgillo
Book Image

RxJava Essentials

By: Ivan Morgillo

Overview of this book

<p>RxJava—Reactive Extensions for the JVM—is a library for composing asynchronous and event-based programs using Observable sequences for the Java VM, which will help you beat Android platform limitations to create astonishing Android apps.</p> <p>Starting with some quick background information on the Rx .NET library, this book quickly moves on to your first example. You will understand Observables and learn to filter, transform, or merge them in detail. Next, you will learn how to get rid of Threads, AsyncTasks, and Handlers with Schedulers to create a smooth user experience. Develop an easy, ready-to-go approach to REST API communications and enrich your skills by working with new challenging examples.</p> <p>By the end of the book, you will have explored the reactive programming world and will have created your first Android app without having to think about threading, networking, concurrency, and collection management.</p> <p>The images have been taken from&nbsp;<a href="http://reactivex.io/" target="_blank">http://reactivex.io/</a> which is licensed under a Create Commons 3.0 Attribution license (<a href="https://creativecommons.org/licenses/by/4.0/" target="_blank">https://creativecommons.org/licenses/by/4.0/</a>)</p>
Table of Contents (15 chapters)

Executing a network task


Networking is part of probably 99 percent of mobile apps nowadays: we always need to connect to a remote server to retrieve the information we need in our app.

As a first approach to networking, we are going to create a new scenario in which we are going to:

  • Load a progress bar

  • Start a file download using a button

  • Update the progress bar during the download

  • Start the video player after the download is completed

Our user interface will be very simple. We will just need a fancy progress bar and a DOWNLOAD button.

First of all, we will create mDownloadProgress:

private PublishSubject<Integer>mDownloadProgress = PublishSubject.create();

This is the subject that we are going to use to manage the progress bar updates. This subject works together with the download function:

private boolean downloadFile(String source, String destination) {
boolean result = false;
    InputStream input = null;
    OutputStream output = null;
    HttpURLConnection connection = null;
try {
   ...