Android network call using Retrofit library

Retrofit : A type-safe HTTP client for Android and Java.

Nowadays, a Smart phone becoming a trend. And hence the scope of Android is increasing continuously. But we also need security while transferring our data on the network. So here is a type-safe library which takes care of the security while making network calls in Android programming.

Retrofit is used to make network calls (REST). This is an annotation based library by Square.

It reduces the long code and makes server calls in few lines of code, which also reduces the time and efforts of a developer.

There are five built-in annotations in Retrofit GET, POST, PUT, DELETE, and HEAD. We will see few of them in brief later.

 Let’s see a sample first

Very first thing, add below dependencies in your project build.gradle(Module: app) file,

compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Second, add an internet permission in AndroidManifest.xml file because we are dealing with network,

<uses-permission android:name="android.permission.INTERNET"/>

Now, Create a new class to create an instance of Retrofit with base Url of your backend API.

public class ApiClient implements Constant {
private static Retrofit retrofit = null;

public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_BACKEND_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

Next, create an interface which will have an actual call to the server, let’s called it ApiInterface.java

public interface ApiInterface {
  @GET("endpoint/user/get_all/")  //this is the rest api url
   Call<Response> getAllUsers(@Query("api_key") String apiValue);

    /* You can also specify query parameters in the URL.
      The response of above and this below query will be same. */
    
    @GET("endpoint/style/all/?api_key=apiValue")
    Call<RegionResponse> getAllUsers();

}

 

Next, create another class, which will hold the response returned by the API.
Here, @SerializedName(“response”) is a key of the response object.

public class Response {

@SerializedName("response")
private ArrayList<ResponseModel> results;

public ArrayList<ResponseModel> getResults() {
return results;
}
public void setResults(ArrayList<ResponseModel> results) {
this.results = results;
}
}

Create a model class(pojo class) to hold the response for each object. @SerializedName holds the key of the field.

public class ResponseModel {

@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("image")
private String imageUrl;

public ResponseModel (String id, String name, String imageUrl){
super();
this.id = id;
this.name = name;
this.imageUrl = imageUrl;

}

public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String getImageUrl(){
return imageUrl;
}
public void setImageUrl(String imageUrl){
this.imageUrl = imageUrl;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}

}

Now finally, lets make a call from the activity. Let’s create it!

public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create instance of get Retrofit instance and connect with the network call methods.
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

// Make a call to specific server API
Call<ResponseObject> call = apiService.getAllUsers(API_KEY);
call.enqueue(new Callback<ResponseObject>() {
@Override
public void onResponse(Call<ResponseObject> call, retrofit2.Response<ResponseObject> response) {
// Here you will get the response.
int statusCode = response.code();
ArrayList<ResponseModel> responseModel = response.body().getResults();
Log.e(TAG, "onResponse: statusCode = "+statusCode+" responseModel = "+responseModel);
// Now you can set your adapter for RecyclerView etc...
}

@Override
public void onFailure(Call<com.ilandmusic.retrofit.Response> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
}

Give a try to above code and I hope you will be able to make the first request of server API using the Retrofit library!

 

Understand use of Retrofit

Retrofit supports
* query parameter support
* Object conversion to request body (e.g., JSON, protocol buffers)
* Multipart request body and file upload

Built-in annotations :

GET Method
Lets see how we can get list of all users just by writing couple of lines.

@GET("endpoint/user/get_all/")
Call<Response> getAllUsers(@Query("api_key") String apiValue);

You can also specify query parameters in the URL. The response of above and this below query will be same.

@GET("endpoint/user/get_all/?api_key=apiValue")
Call<Response> getAllUsers();

Also can specify the path, as shown below example,

@GET("endpoint/user/{id}")
Call<Response> getUserDetails(@Path("id") int id, @Query("api_key") String apiValue);

You can send headers by defining @Headers annotation on request call method or as a parameter to the request call method with @Header annotation. Below example explain itself.

@Headers({
"api_key: Api-value",
"token: Tokan-value"
})
@GET("endpoint/user/{id}")
Call<Response> getUserDetails(@Path("id") int id);

POST Method
An object can be specified for use as an HTTP request body with the @Body annotation. Below example shows how we can make a POST server call,

@POST("endpoint/user/create")
Call<User> createUser(@Body User user);

If you are using converter on Retrofit instance, the object user will be converted using it. If no converter is added, only RequestBody can be used as a object. Above query can be,

@POST("endpoint/user/create")
Call<User> createUser(@Body RequestBody user);

If you want to send sensitive data but without @Body, like user details over a network, no worries, Retrofit has the @FormUrlEncoded annotation. @FormUrlEncoded this will encode the data in form of key-value pair which will be annotated with @Field containing the name and the object providing the value.

@FormUrlEncoded
@POST("endpoint/user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);

PUT Method
You can send Multipart requests by using @Multipart annotation on the request call method. Parts are declared using the @Part annotation.

@Multipart
@PUT("endpoint/user/image")
Call<User> updateUser(@Part("image") RequestBody image, @Part("description") RequestBody description);

Deploying with Deploybot

I vividly remember the first time, I messed up a production server. It was my early days of being a programmer, and we had got our first client.

Back then, my deployment strategy was basically to upload files using FTP and then run any commands on the server via the shell. During a routine deployment, I noticed a file which remained in the server, and in trying to remove it, I typed sudo rm -rf /.

On the Production.

I watched the next few minutes in horror as the entire client’s machine was wiped clean and the site went down. Fortunately, my client was understanding, and we had backups – so there was not much of damage – but I had to spend the next 3 days fixing the mess (and contemplating if I am really cut out for this job.)

My biggest learning from the incident was to be very careful when on Production. Over the time, I learned Git and other tools, which made deployments more easier and safer. As someone developing in Laravel, and leading a team of Laravel developers – I am always on the look out to make deployments easier.

And I have tried everything from custom bash scripts to git workflows, where we would git pull on server. None of them however stuck primarily due to the complexities they bought in

And after much experimentation – my team and I zeroed down to DeployBot.

DeployBot allows you to deploy code from anywhere. It takes your code from your Github / Bitbucket or self hosted Git repositories and deploys to any server. At QICE, we primarily use Digital Ocean and AWS – both of which are supported by DeployBot and make it an ease to integrate in our projects.

Here’s how DeployBot has helped us

Continuous Deployment

Over the day, we make 2-3 deployments to our sandboxes on certain projects. And these are fairly large commits. DeployBot seamlessly gets the new commits and automatically ( or manual for production setups ) deploys the latest files to the server.

My team now does not have to worry about deploying to server. All we have to do is push to a branch, and we know it will end up being on the server.

Rollbacks

Despite much preparation, there are moments, when things don’t work on the production for weird reasons. Deploybot has a rollback to a specific version feature, which is quite nifty at times like these.

Pre and Post Deployment Commands

After deployment, we run a few commands ex : Gulp, Migrations and Composer updates.

Deploybot allows us to specify what commands to run before and after deployment. That means, more developer peace and not worrying about switching to server and typing in each command on production machines.

Modifying Configuration Files

Even after all this, you may have to sometimes go to the server to edit your configuration files.

Deploybot eliminates this as well, by asking you to enter your configuration files. Just ensure all the changes are in your configuration file before you deploy, and they are deployed in your next deployment.

Notifications

Pretty much every web app these days has Slack / Email integration – and so does Deploybot. It notifies us everytime there is a deployment in our Slack channels.

No more informing the entire team that the production is done and they can resume their work.

Amazing Support & Reliability

This is something of importance to us. In the past one year, that we used Deploybot, we faced a downtime of exactly 1 minute, where we couldn’t deploy to production. We reached out to Support and got a reply back within the next minute telling us that the issue has been fixed.

Thanks to Deploybot, my team and I can now focus on building stuff than worrying about getting it to our customers. If you are into developing web apps, it is an invaluable part of your toolset and takes care of all your deployment worries.