An Introduction to Loopj

Loopj is an Android library for making asynchronous HTTP requests. I like it for its ease of use and simplicity. Created by James Smith, it's also known as "Android Asynchronous HTTP Client", and it's used by companies like Instagram, Pinterest, and many others. It's a good entry point to the world of HTTP libraries, and will help you understand important concepts very easily.

To learn to use this library, we are going to create MovieTrivia, a simple app that connects to a web service to retrieve information about movies or TV shows and displays this information to the user.

1. Setup

To start, create a new Android Studio project, with an empty activity. To include Loopj, copy the dependency from the official website. You can find the dependency under the "Installation and basic usage" section of the website (or just copy the string below). 

The message "Sync Now" will pop up in the upper right-hand corner. Click it so Gradle will download the library and make it available to your project.

Because the app will connect to the internet, we must declare the appropriate permission for the user to authorise. Open your AndroidManifest.xml file and, just before the application tag, write:

Now you are ready to start using Loopj in your app.

2. Implementation

We are going to create the simplest possible user interface: just a TextField to enter a search term, a Button to do the search, and a TextView to show the results. Open activity_mail.xml and add the required UI components as shown below.

Now, as usual, wire up these components in your MainActivity.java.

To keep your code clean and organized, create a separate java class, MyLoopjTask.java, to contain all the Loopj-specific code and operations. You will need two instance variables, one of type RequestParams to build the search details into the URL, and another of type AsyncHttpClient to actually make the HTTP requests. Before going further, check that Android Studio added these necessary imports.

Also create a method that takes the user query as an argument—this is where the actual work will be done.

As you see, you can add all the necessary API request parameters keys with the RequestParams instance (in this case, just the query entered by the user). This version of the get() method returns a JSON object to onSuccess, but there are other variations available to fit your needs. (Use Ctrl+O to see the other versions of this method.)

This is all you need to do to implement a Loopj client to make HTTP calls to a web service. In our case, we want to send our request when the user clicks the "Search" button. So, back in MainActivity, create an instance of MyLoopjTask and inside onClick call  executeLoopjCall with the term entered by the user. MainActivity will look as follows.

Now, if you run the app, you should see the results of your request in the log window.

3. Publish Results to the UI

Separating asynchronous operations into dedicated classes helps keep our code clean, but it means we don't have access to UI elements directly. To show the results of these requests, I recommend creating a listener interface. This is a common strategy, discussed for example in this Stack Overflow question. It has three easy steps.

Create an Interface

First, create an interface with a single method that will be called when the results of the HTTP request are obtained from Loopj.

Update the Asynchronous Task Handler

In MyLoopjTask, modify the constructor so it takes a Context and an OnLoopjCompleted instance as parameters. 

Now, when the request results are available in MyLoopjTask, pass them to the listener interface's method.

Update the Activity

Finally, make the activity you want to update—in this case MainActivity—implement OnLoopjCompleted. Update the initialization of myLoopjTask to myLoopjTask = new MyLoopjTask(this, this);.

Since your activity now implements OnLoopjCompleted, Android Studio will force you to implement the method taskCompleted. There, you can update the UI with the new data.

4. Conclusion

There you have it. When you run the app, all the data from your asynchronous HTTP request will be displayed to the user. Naturally, you will want to parse the received JSON data to extract just the parts you need. For example, you could display the movie poster by passing the poster image URL to Picasso.

In this tutorial, you've seen how to use Loopj, a library to perform asynchronous HTTP requests in Android. Loopj is easy to use with just a few lines of code. Now you don't have any excuse for not keeping your app up to date with content from your favourite web service!

Tags:

Comments

Related Articles