There are a lot of ways to make HTTP request on Android. However, so far the easiest way I have found to do so is Retrofit.
CONVERSION
This a Java/Android client by Square, Inc. that allows you to convert your HTTP Api to a java interface.
Let’s see a quick sample of how we can create a simple API:
First thing to do is add the dependencies to our project, We can do that by adding the next couple of lines to our project gradle file.
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
The first line is the one that is going to import the retrofit library to our project, the second one is going to allow us to parse the response as a Json Object.
Another important thing is to add the internet permission on our manifest.
<uses-permission android:name="android.permission.INTERNET"/>
START BUILDING WITH RETROFIT
So with this basic configurations done, now We are ready to start building our first test API with Retrofit.
What We are going to do is use httpbin as our test server. We will use the endpoint “https://httpbin.org/get” which is going to return us a json object as a response.
In order to start constructing our API, We need to create a new class, which in my case I called API.
In this class, We are going to do several things:
- Declare an interface which is going to be our API declaration (in this case I called it “Api”, and contains out “testGet” http request declaration).
- Declare the interface that We will use to handle the response of our http calls in whatever class we call them (in my case I called “RequestListener”).
- Declare the interface that is going to receive the response from the http call (called “ApiCallback”).
- Declare a public method, which is the one that We’ll call in order to do the http request and that takes as parameters the context and the RequestListener implementation (“testGet”).
- And finally, our API constructor. This will contains the declaration of our Retrofit instance, and we use this instance to instantiate out API declaration.
So after all of this, our file should look something like this:
public class API {
private final Api api;
public API() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://httpbin.org")
.addConverterFactory(GsonConverterFactory.create())
.build();
api = retrofit.create(Api.class);
}
public void testGet(Context context,
RequestListener<JsonElement> listener) {
api.testGet().enqueue(new
ApiCallback<(context,listener));
}
public interface Api {
@GET("get")
Call<JsonElement> testGet();
}
public interface RequestListener<T> {
void onSuccess(T response);
void onResponse();
void onError();
}
public class ApiCallback<T> implements Callback<T> {
protected RequestListener<T> listener;
protected Context context;
public ApiCallback(Context context, RequestListener<T> listener) {
this.listener = listener;
this.context = context;
}
@Override
public void onResponse(Call<T> call, retrofit2.Response<T> response) {
listener.onResponse();
listener.onSuccess(response.body());
}
@Override
public void onFailure(Call<T> call, Throwable t) {
listener.onResponse();
listener.onError();
}
}
}
After that, all We need to do is create an instance of out API class and call the method getTest.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
API api = new API();
api.testGet(this, new API.RequestListener<JsonElement>() {
@Override
public void onSuccess(JsonElement response) {
Log.i("RETROFIT",response.toString());
}
@Override
public void onResponse() {
}
@Override
public void onError() {
}
});
}
as a result, something like this should appear on your logcat console:
RETROFIT: {"args":{},"headers":{"Accept-Encoding":"gzip","Host":"httpbin.org","User-Agent":"okhttp/3.12.0"},"origin":"187.188.64.105, 187.188.64.105","url":"https://httpbin.org/get"}
WE LIKE FAST AND EASY
And that is basically it! Fast and easy, right? In just in a few minutes, we built an API.
In this case, we are using an HTTP call without parameters to show how it works. However, Retrofit allows you to use any HTTP method (GET, POST, PUT, DELETE), and you can add queries, a body, multipart requests, parameters within the URL, and everything you need.