Select Page

There’s something that probably you can’t avoid in an Android project: Networking. Requesting data from an API server or getting a single byte from the internet, you are doing networking.

No doubt there are a lot of good libraries out there and the wheel doesn’t need to be reinvented, but we can realign it based on our needs.

I recently published a library, which I believe to be the simplest way to handle networking with Android using Retrofit.


Retrofit is type-safe HTTP client for Android and Java by Square, Inc. It provides a powerful and flexible framework for authenticating and interacting with APIs. You can find basic details about Retrofit in my previous post.

This article talks about RetrofitHelper. It is an Open Source Android library that used to make HTTP request easily. Here are some advantages of using RetrofitHelper over other libraries:

  1. Network checking is not required.
  2. No need to handle ProgressDialog while calling an API.
  3. ApiInterface and ApiClinet are not required.
  4. You can handle errors in a single place.
  5. You can optimise your code.
  6. Avoid code redundancy.

retrofit_executionFor using this networking library, you need to follow the below instructions.

Step 1:  You need to add the following dependency to your build.gradle file.

implementation 'com.droidmentor:helper:1.0.1'

Step 2:  Then add the required permission in your AndroidManifest

<!- Allows applications to open network sockets. ->
<uses-permission android:name="android.permission.INTERNET" />
<!- Allows applications to access information about networks. ->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Step 3: At next set the Base URL and common headers using the following code

Map<String, String> default_headers = new HashMap<>();
default_headers.put("token", Url_handler.user_token);

This library checks the network connection by default and shows a Toast with “Network connection failure” message. You can change this message and also if you don’t want this toast message you can hide this too by using the below code.

ApiClient.setNetworkErrorMessage("Network Error");

setting Base URL is mandatory and others are optional. You are required to do this only once.

Step 4: Then create an instance for APICall. APICall is the class which connects ApiListener and ApiInterface.

APICall networkCall;
networkCall = new APICall(this);

If you want to set additional values in the header then you can use the following snippet,

APICall networkCall;
Map<String, String> custom_headers = new HashMap<>();
default_headers.put("token", Url_handler.user_token);
networkCall = new APICall(this,true,custom_headers);

You can create an instance based on your requirement, using the below set of constructor with the respective arguments.

// Standard Object
public APICall(Activity activity) {
init(activity, ApiClient.getCommonHeaders(), null);
// boolean flag decides the header is needed or not
public APICall(Activity activity, boolean with_header) {
if (with_header)
init(activity, ApiClient.getCommonHeaders(), null);
init(activity, null, null);
// you can add additional header
public APICall(Activity activity, boolean with_header, Map<String, String> customHeaders) {
if (with_header)
init(activity, ApiClient.getCommonHeaders(), customHeaders);
init(activity,null, customHeaders);
// set default header
public APICall(Activity activity, Map<String, String> defaultHeaders) {
init(activity, defaultHeaders, null);
// set default and additional header
public APICall(Activity activity, Map<String, String> defaultHeaders, Map<String, String> customHeaders) {
init(activity, defaultHeaders, customHeaders);


Step 5: Then call APIRequest method with the needed parameters. Here only the magic happens, this is the method which simplifies your process and act wisely based on your parameters. This method can handle the following arguments.

  • type : type of the request.
  • url : endpoint
  • responseModel : response model class.
  • body :  request body.
  • params : URL parameters.
  • from : int value represent the source, because in a single page you can trigger multiple requests.
  • is_PDshow : boolean flag shows the visibility of the ProgressDialog.
  • message : Loader title message.
  • responseListener : It is an interface, it contains the callback methods (onSuccess, onFailure, onNetworkFailure).

In this arguments list : you must specify the values for type, url, responseModel and responseListener . You can send NULL value for body and params. If you want to show the progress dialog then set the title message else neglect it.

I made a simple sample app that makes API request using RetrofitHelper , it’s over on GitHub if you want to check it out.

View on Github

Feel free to comment and share, keep watching this space to get more updates on Android Stuff!


Jaison Fernando

Android Developer at NFN Labs