I am creating an Android application which basically calls an web service and shows back the response; and I am following the MVP pattern to accomplish it. So the biggest & basic challenge is to check the internet status before every call. I was thinking to create an Abstract layer between the Presenter & the API. So the API request will pass first from the Abstract Internet Layer and if it passes successfully then only the API will get called.
Creating a separate layer looks a better solution in my opinion rather than calling the Internet check logic from Presenter, every time. Please help me with the design if you've any better idea.
Thanks
Asked
Active
Viewed 691 times
2

Salman Khan
- 140
- 1
- 1
- 10
-
If I were you I would use Retrofit library for http requests. It handles internet connection status. Also there is Interceptor class too. – Ozgur Jun 02 '16 at 11:50
-
I'm already using retrofit but I don't know how to handle internet connection status with it. – Salman Khan Jun 02 '16 at 11:54
-
Which version? In retrofit 2 if there is no connection, it will be in onFailure()... method. – Ozgur Jun 02 '16 at 11:55
-
I'm using Retrofit 2 with RxJava. – Salman Khan Jun 02 '16 at 11:56
-
As I said retrofit handles it and if there are any error, onFailure will be called. – Ozgur Jun 02 '16 at 11:58
-
But I think it's a generic callback. onFailed will also get call if the API returns HTTP Status code other than 200. So is there a way to particularly handle **No Internet** error – Salman Khan Jun 02 '16 at 12:01
-
@Coderkhan you should handle it before making the call to save the resources, I am reluctant for putting it in Presenter as it is Android Related stuff, though would love to hear from you what you did and any issues faced? You can look more at http://stackoverflow.com/questions/37007994/android-mvp-where-check-internet-connection# – Akhil Dad Jul 05 '16 at 12:34
1 Answers
0
You can create a BasePresenter
, that presenter will be a super class of all your presenters. Now in BasePresenter
you can write your common APIs (protected/public) stuff like making http
request, showing loader, check for internet connectivity etc.
Each time when you make http
call via BasePresenter
, your BasePresenter
will first check for the requirements and then makes an http call to the server. This is because of, it will be always easy to change anything related to http logic or internet check logic or anything. Because everything will be at one place only (BasePresenter
) and easily accessible to the sub presenters.
So, by this way you can achieve the feature implementation.

naran z
- 486
- 3
- 13
-
-
1Adding android related logic is not a very good idea in presenter, may be look at this post http://stackoverflow.com/questions/37007994/android-mvp-where-check-internet-connection# – Akhil Dad Jul 05 '16 at 12:32
-
I am not talking about to add Android logic in Presenters. Just suggested to add HTTP communication into it. The controllers could be used for same though. – naran z Jul 11 '16 at 06:24