Android HTTP library: Handle HTTP, JSON, Images

When we develop an Android app, usually we have to connect to a remote server to get information. The connection usually is based on HTTP protocol because it provides a simple mechanism to transport information. Moreover, almost all platforms provide a set of API based on HTTP and it is very common the scenario where an Android app needs to integrate with one of these platforms. For these reasons, it is important to know how to choose the best Android HTTP library. The best choice, of course, depends on the requirements of our Android app, so we can provide some hints that help you to select the best Android HTTP library according to our needs.
Even if our app doesn’t use directly an Android HTTP library, there are tons of android libraries that use it (i.e App analytics, crash reports, Adv platforms and on on.)
As we already know, Android SDK provides a set of API to handle HTTP connection to send and receive data.

Android HTTP library

The Android HTTP library is based on HttpUrlConnection. This Android HTTP library supports HTTP and HTTPS protocol and it is the basic class to use when handling HTTP connection. Before Android 6.0, there was another library shipped with Android SDK, called Android Apache HTTP. Android 6.0 removes the support to this library and it is not advisable to use it anymore.
If you still want to use this Android HTTP library you must add these lines to your build.gradle:

android {
useLibrary 'org.apache.http.legacy'
}

HttpUrlConnection, as said, helps us to handle HTTP connection. A few lines are necessary to make an HTTP request:

Even if it is simple to use there is a drawback: we don’t have to make HTTP calls in UI thread. For this reason, the piece of code above must be wrapped in a different thread. Usually, the class used with HttpUrlConnection is the AsyncTask.
For this and other reasons, many developers prefer to use Android HTTP library alternatives. These third-part libraries have these advantages:

Efficiency

Parallel requests

Caching system

Non-blocking UI thread

HTTP/2 support

At level of HTTP handling there are two main libraries:

Volley

OkHTTP

On top of these two libraries, there are other Android HTTP libraries that are used in more specific tasks (like Image handling over HTTP, JSON data handling and so on).

You can notice, how simply is using Volley and how clean is the code. We don’t have to implement our thread to handle the HTTP connection, but we get notified when the response is ready.
Expecially relevant, it is the fact we can have more control on how the RequestQueue is created. Therefore, using Android Volley we have more control respect to HttpUrlConnection.
In addition to these features, Android Volley provides some API to handle Image download:

As you can notice, it is very simple and you don’t have to create different threads to handle the HTTP requests. OkHTTP handles it for you and exposes two callbacks methods so that you get notified when the response is ready or there was an error.

Even if scheduling the requests is handy, OkHTTP offers the feature to make an HTTP blocking request:

Response response = client.newCall(request).execute();

Be aware using this kind of request!

Specialized HTTP libraries

By now, all the libraries, we covered, are used to handle HTTP requests and read the responses. There are other kind of HTTP libraries that are more specialized and can be very useful in specific scenario.
One interesting HTTP library is RetroFit. Retrofit provides a connection between Java and Rest interfaces. It is an Android Rest Client used to consume Rest services. It uses OkHTTP as HTTP connection handler and can be configured to use several converter to serialize and deserialize data.
The convert supported are:

Gson

Jackson

Moshi

Protobuf

Wire

Simple XML

Scalars

Another interesting library is Picasso used to download and cache images. So this library is useful when we have to deal with images.

Glide is a library that provides features like Picasso to handle images. Moreover, Glide has interesting features to fetch, decode, and display video.
To use Glide in an Android project:

About The Author

I'm an electronic engineer with over 15 years of
experience in computer programming. I'm the author of the book "Android things projects". I'm a MVB at DZone and DZone Guide Author. I have contributed to the IoT guide. I'm the technical reviewer of the book Internet of Things with Arduino Blueprints(published by PacktPub). I love creating IoT projects using Arduino, Raspberry Pi,
Android, and other platforms. I am interested in the convergence
between IoT and the mobile applications. I'm SCEA, SCWCD, and SCJP
certified.
Follow on: Twitter | Linkedin

It will be very helpful if you can suggest something to achieve wallet kind of animation.

These are points I am looking out in this animation module

1. First screen would be the cardstack
2. Once I click on any card , It should be opened up to a detailed card screen with some pop effect as in iOS
3. Next card can be viewed from there itself by swiping the view to right or left
4. When card dragged to below it should again come to the stack
5. If dragged Up/Right/Left it should be dragged back to its position

Thank you so much sir. As I am new to Android and I have lot of questions in mind , so now I am able to understand the details logic behind the sending/receiving services among App and server. Please keep on writing wish you good Luck!!!!

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Sign up for our Newsletter

Enter your email and stay on top of things,

Get SwA updates delivered to your inbox. Article, tips, source code

×

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.