How to separate application logic from network layer in Android using Retrofit 2

I'm new to Android and upgrading, and you have one problem.

I want my words to allow the use of the "ServerCommunication" (singelton) class, where all the Retrofit magic is executed, and it will have public methods in which REST calls are made.

I want to use this instance of "ServerCommunication" in my actions to call the Rest service, but it is. Application logic must be executed in action. Thus, some Login actions call the Login (POJORequest) method in "ServerCommunication", where a real REST call is made through the Retrofit infrastructure, and the POJOResponse is returned. Thus, Activity does not care about REST communications, while ServerCommunication does not care about what logic should be applied to the REST service response since then.

With update 2, I don’t understand how I can block activity to wait for a response from the modification and how it can be returned. Well, I might think that I can use some callback methods in activity so that these methods can be called from ServerCommunication in OnPostExecute () to apply some logic based on the data from the response. I just think this should be a simpler approach.

Well, to clarify this whole mess above, imagine a simple case: you have data in your main action, you pass this data to your communication class, where the REST call is made and the answer is received. This answer must be checked to continue. And you want this validation to be performed in the main activity, and NOT in the communication class.

What is an Android template with Retrofit2?

Thank you in advance

+7
android rest retrofit retrofit2
source share
2 answers

What I usually do:

  • Build your interface (where you have all your REST methods - GET and POST, etc.).
  • Create a class that makes the actual calls with the appropriate methods (see REST interface methods). I would call it something like ServiceAPIImplementor. Here you actually create your adapter for revision.
  • In your activity, create an instance of the developer class and call methods and pass the expected arguments.
  • After calling the methods, you should probably show a progress dialog so that the user knows that something is happening.
  • When the onResponse or onFailure method is called , use the Event template (EventBus library?) To notify you of an action that has been completed by a network operation. After the event has received a notification, it should then cancel the progress dialog and update the user interface accordingly - with the newly received data or the completed operation (expected result).

Hope this helps you get closer to what you are trying to achieve!

+6
source share

Service Interface (IPhotoService):

@GET("/photos/kudos") Call<String> fetchKudos(@Header("Authorization") String authorization, @Query("offset") int offset, @Query("mt") boolean mt); 

Impl service (PhotoService):

 private GoApiProvider<IPhotoService> mGoProvider = new GoApiProvider<>(); public Promiser<List<Photo>, HttpError> fetchKudos() { return new Promiser<>((resolve, reject) -> mGoProvider.getService(IPhotoService.class).fetchKudos(mSession.getToken(), mOffsetKudos, true).enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if (response.isSuccessful()) { PhotoParser JSON = new PhotoParser(); try { mOffsetKudos = mOffsetKudos + 20; resolve.run(JSON.photosFromJson(response.body())); } catch (JSONException e) { Log.e("fetchKudos", e.toString()); } } else { reject.run(new HttpError(response.code(), response.message())); } } @Override public void onFailure(Call<String> call, Throwable t) { reject.run(new HttpError(YPErrorType.Undefined.getType(), t.getMessage())); } }) ); } 

Activity or fragment:

 private void loadPhoto() { new PhotoService().fetchKudos() .success(this::resultSucceeded) .error(this::resultError); } private void resultSucceeded(List<Photo> photos) { mPhotoAdapter.setItems(photos); } private void resultError(HttpError httpError) { httpErrorToast(httpError); } 

If you want to use Promizer: Click here.

0
source share

All Articles