RetrofitHelper is an Open Source Android library that used to make HTTP request easily.
Here are some advantages of using RetrofitHelper over other libraries:
- Network checking is not required.
- No need to handle ProgressDialog while calling an API.
- ApiInterface and ApiClinet are not required.
- You can handle errors in a single place.
- You can optimise your code.
- Avoid code redundancy.
For using this networking library, you need to follow the below instructions.
Step 1: Add Dependency in build.gradle
compile 'com.droidmentor:helper:1.0.1'
Step 2: Set Base URL and common headers if needed. Base URL field is mandatory.
Step 3: This library checks your internet connection be default so if you want to toast message you can set the error message.
ApiClient.setBaseUrl(BASE_URL); ApiClient.setNetworkErrorMessage("Network Error"); ApiClient.showNetworkErrorMessage(false);
Step 4: APICall is the class do the magic. You can call the API using the following set of code.
APICall networkCall; networkCall = new APICall(this); Map queryParams = new HashMap<>(); queryParams.put("user[email]", etUserEmail.getText().toString()); queryParams.put("user[name]", etUserName.getText().toString()); networkCall.APIRequest(APICall.Method.POST,"user",UserDetails.class,null,queryParams,1,"Adding");
Step 5: APIRequest is the method 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 AndroidManifest.xml
Specify all the needed permissions. If you don't, permission requests fail silently. That's an Android thing.
For more information, check out my detailed guide here : http://droidmentor.com/retrofithelper-library/