Working with MVP and Retrofit 2 in Android with demo project

Bipin Pandey
Resume and CV Builder App
5 min readDec 9, 2017

Let’s start with short talk about Model View Controller(MVC). So, what is MVC? What are the advantages of MVC? What are the drawbacks of MVC?

The MVC pattern is the solution where the user interface logic tends to change more often than the business logic which is more likely used by the desktop and web developers. Advantage: MVC supports rapid and parallel development. With MVC, one programmer can work on the view while other can work on the controller to create business logic of the web application. Drawbacks: The view depend upon both controller and model. The view and the controller are closely coupled which makes modification to one affect the other.

Previously, we had created the simple and easily understandable article about the Model View Presenter and Retrofit Networking library. So, before you jump inside this article please check that out to be more clear.

MVP

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern which mostly used for building user interfaces. In MVP, the presenter assumes the functionality of the “middle-man”. All presentation logic is pushed to the presenter. MVP advocates separating business and persistence logic out of the Activity and Fragment.

Please checkout my MEDIUM BLOG to learn more about MVP.

Retrofit

Retrofit is type-safe HTTP client for Android and Java by Square, Inc. It is an open source library which simplifies HTTP communication by turning remote APIs into declarative, type-safe interfaces. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservices. It automatically serializes the JSON response using a POJO which must be defined in advanced for the JSON Structure.

Please checkout my MEDIUM BLOG to learn about Retrofit.

Project Overview

After learning the basics of MVP and Retrofit. Now it’s turn to make our hand dirty by combining both MVP and Retrofit. Here, we are trying to get JSON data from the server using Retrofit and populate in the recycler view using MVP pattern.

The project structure will be like below. The classes Notice, NoticeList, RetrofitInstance, NoticeAdapter and an interface GetNoticeDataService are explained in this RETROFIT DEMO. Get our full project in GITHUB.

build.gradle

The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. We need recyclerview, cardview and Retrofit dependencies that are added below. To serialize JSON we need a converter to convert it into Gson first. We need to add the following dependencies in our build.grade file.

MainContract.java

This interface consists the three inner interfaces named presenter, MainView, GetNoticeIntractor.

presenter: This interface callbacks will be called when the user interact with the view onRefreshButtonClick(), when the view is destroyed onDestroy() and requesting the data from the server requestDataFromServer() for the first time when the activity is created.

MainView: The interface callback will be called when the user need to show/hide the progressbar showPregress()/hidePregress() , set data to the recycler view setDataToRecyclerView(…) and lastly to show the error if the network response is failed onResponseFailure(…).

GetNoticeIntractor: Intractors are built for fetching the data from the database, webservices or any other data source. This interface callback has one inner interface named onFinishedListener{..} which will be called when the webservices response in finished or the response is failure onFinished(…) and onFailure(…). And it also has another callback named getNoticeArrayList(onFinishListener …) that has the onFinishListener interface as a constructor.

GetNoticeIntractorImpl.java

This class implements the MainAcontract.GetNoticeIntractor Interface which includes the overridden methoid getNoticeArrayList(). This class is responsible to get data using the rest apis using retrofit library and pass data through onFinishedListener callback.

Now, we have to create the retrofit instance service and use that service to fetch data by using the interface. Here, we don’t pass your callback as the last parameter. We use the service to get a call object. Once we’ve invoked .enqueue on the created call object the request will be made by Retrofit. And we should be able to make our very first request with Retrofit.

If everything went well, Retrofit will return you a List<Notice>and then we will pass data through onFinishedListener.onFinished(…) callback. If failure then we will pass data through onFinishedListener.onFailure(…) callback.

MainPresenterImpl.java

This class is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View and it also decides what happens when you interact with the View.

This call implements the MainContract.GetNoticeIntractor.OnFinishedListener, and MainContract.presenter interface which includes their overridden methods as well. And the constructor includes the two params named MainContract.MainAview and MainContract.GetNoticeIntractor.

When the onDestroy method is called from the main activity, this class listen the onDestroy() callback and make the mainview to null. And when onRefreshButtonClick() it calls the showProgress() and getNoticeArrayList(...) callback to get data from server. And When the data is fetched/received from the webservices callback in GetNoticeIntractorImpl class onFinish(…) and onFailure(…) callback is called.

RecyclerItemClickListener.java

This interface is created in order to listen the click callback from the recycler view list.

NoticeAdapter.java

Now, we will create an adapter that is used to display the list of notices the in the recycler view. In the onBindViewHolder(…) overridden method we have setup the onClickistener() that will listen when the holder item view is clicked.

MainActivity.java

This Activity contain a reference to the presenter. The only thing that the view do is to call a method from the Presenter every time there is an interface action.

In this class we implement MainContract.mainView interface that includes its overridden methods. When the onCreate method is called and when the user click the menu update button we use the presenter object to request data from the server. All the logical task are done by the GetNoticeIntractorImpl class and pass the result to MainPresenterImpl class and the result came through callback in MainActivity.java update the ui view.

OUTPUT:

And lastly, the output looks like:

--

--