Android Networking library: RxAndroidNetworking — To handle All type of API request(graphQL and Rest APIs)

Arun Pandian M
4 min readApr 13, 2019
Networks: image credits: Photo by Clint Adair on Unsplash

RxAndroidNetworking is a powerful Android library for doing any type of networking call in Android applications which is made on top of Retrofit using RxJava. No other library provides or supports Network calling using RxJava and Retrofit. It provides support to implement all type of network calls which are GET, POST, PUT, DELETE HTTP requests and also supports GraphQl APIs requests as well as rest APIs.

  • Provides a simple interface for doing all types of things in networking like setting params, its type and response model like that
  • No need to write the code to construct RequestBody, Multipart params. Already in this library has the function to that just pass the required params
  • You can check Network availability using inbuilt function
  • You can customize every API request and its response model also, time out etc
  • You can do File upload in this library
  • It supports Rest APIs as well as GraphQl request

Why Using RX Java???

  • RxJava Provides more operators to manipulating API response.
  • You can do Chaining API Calls
  • You can choose thread whether the main thread or worker thread to get the response. (Thread Handlings)
  • You can use all the Rx Java operators because Rx Java operators are very powerful and useful
  • RxJava reduces the response time also

Future Enhancements:

I planned to do the following things in the next version

  • Progressbar loader and its customization
  • File Download
  • Bulk File download and its path customization and thread management while files downloading

It supports from and above Android API 21 version

Implementation:

Add the following dependency in your app level build.gradle

Dependency:

implementation'me.arun.androidrxnetworking:androidrxnetworking:1.0.1

Sample Request Implementation:

Set the base URL:

//set up the base url 
NetworkingApiClient.setBaseUrl(“https://api.themoviedb.org/3/movie/");

Sample steps to follow to make one API request:

  • Initialize the PublishProcessor for Pagination APIs or simply use PublishSubject. Because PublishProcessor handles the backpressure
  • Subscribe the PublishSubject or PublishProcessor with appropriate Observer
  • Building the request object and make API call
// initiate the PublishProcessor for Pagination APIs 
//or use PublishSubject
PublishProcessor<ModelNowPlayingMovie> ppMovieRes = PublishProcessor.create();// PublishSubject initialization
PublishSubject<Movie> psMovie = PublishSubject.create();
//subscribe the PublishSubject with Observer
psMovie.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new Observer<Movie>() {
@Override
public void onSubscribe(Disposable d)
{
Log.d(TAG, "onSubscribe: " + d);
compositeDisposable.add(d);
}

@Override
public void onNext(Movie movie)
{
// Here you will receive response
Log.d(TAG, "onNext: " + movie);
}

@Override
public void onError(Throwable e)
{
Log.d(TAG, "onError: " + e);
}

@Override
public void onComplete()
{
Log.d(TAG, "onComplete: ");
}
});
// building request object for api request

// hash map for params
HashMap<String, String> hmParams = new HashMap<>();
hmParams.put("api_key", "8a03975d504c762ab63b6c5faadcadcadc98e3c17");
RxNetworkRequest<ModelNowPlayingMovie> rxNetworkRequest = new RxNetworkRequest.RxNetworkRequestBuilder<ModelNowPlayingMovie>(this,"now_playing", ObservableType.FLOWABLE, RequestType.POST, ModelNowPlayingMovie.class).setQueryParams(hmParams).build(); // make a API request
rxNetworkRequest.makeRequest(psMovie, null,"");

Customize Request building:

RxNetworkRequest<ModelNowPlayingMovie> rxNetworkRequest = null;RxNetworkRequest.RxNetworkRequestBuilder<Movie> rxNetworkRequestBuilder=new  new RxNetworkRequest.RxNetworkRequestBuilder(this,"your endpoint", ObservableType.SINGLE,RequestType.GET, Movie.class);rxNetworkRequest = rxNetworkRequestBuilder.build();//make request 
rxNetworkRequest.makeRequest(publishsubject, null,"");
  • In the above code snippet explains, We need the endpoint, HTTP request type, Response model class and Observable type for one basic API call.
  • I suggest that use a Single observable type for API calls without pagination.
  • If your API calls have the pagination then you have to set it as a Flowable type to handle the backpressure.
  • Movie.class defines the Response model class

Request Methods:

For String Params:

// hash map for constructing string param
HashMap<String, String> hmParams = new HashMap<>();
hmParams.put("param1", "params1_value");
hmParams.put("param2", "params2_value");
//set Query params
rxNetworkRequestBuilder.setQueryParams(hmParams)
rxNetworkRequestBuilder.build();
// make or call the request
rxNetworkRequest.makeRequest(null, ,"");

For RequestBody params:

Movie movie=new Movie// pass the requestbody object
RequestBody movieRequestbody= ParamsUtil.getRequestBodyFromCustomObject(movie);
//set requestbody in builder object
rxNetworkRequestBuilder.setRequestBody(movieRequestbody)

For form-url-encoded data params:

// hash map for constructing  form-url-encoded param
HashMap<String, String> hmFieldParams = new HashMap<>();
hmFieldParams.put("param1", "params1_value");
hmFieldParams.put("param2", "params2_value");
//set params in builder object
rxNetworkRequestBuilder.setFieldsParams(hmFieldParams);

For multi-part file upload params:

//get the file from path
File imageFile = new File(uripath);
MultipartBody.Part body = ParamsUtil.getImageMultiPart(File file);
// to set the multipart file in builder object
rxNetworkRequestBuilder.setFile(body);

For GraphQl Request:

In RxAndroidNetWorking library, there is no need to set the schema file at all.

Just made the query and set it in the builder object then make the request that's all.

HashMap params = new HashMap();
String param;
param="{\n" +
" country(code: \"BR\") {\n" +
" name\n" +
" native\n" +
" emoji\n" +
" currency\n" +
" languages {\n" +
" code\n" +
" name\n" +
" }\n" +
" }\n" +
"} ";
params.put("query",param);
Gson gson=new GsonBuilder().create();
String queryString= gson.toJson(params);
// request body for the graphQL call
RequestBody requestBody = ParamsUtil.getGraphQlString(queryString);

I hope you will love RxAndroidNetWorking library and also let me know if any issues or difficulties if you faced. I will try to change it to use easy

Github sample repo is here: https://github.com/arunpandian22/RxAndroidNetworking

Happy coding and give appreciation 👏 if you like my work !!!

--

--

Arun Pandian M

Senior Android developer at FundsIndia, A time investor to learn new things about Programming. Currently in a relationship with Green Bug(Android).