During this period, the company needs to rebuild the original project to form the MVP architecture. I have sorted out the MVP architecture by referring to some online MVP project descriptions and demos of Android and some actual requirements of my own project. The architecture used Retrofit as the network framework, used POST requests, added a caching mechanism for interface requests, and used RXbus instead of broadcast. The whole project took about two weeks to rebuild and test.

Since this project refers to other netizens’ projects, if any netizens find me quoting my own project, please feel free to contact me, AND I will explain it in the article. The project link is attached below:

https://github.com/gongchenghao/my_mvp_projectgongchenghao/my_mvp_projectgongchenghao/my_mvp_project

When I looked up Retrofit, I didn’t find that retrofit had a built-in caching mechanism, and most of the online caching methods for RetroFit were also for GET requests, but not for POST requests, so I had to use a cached tool class for caching. This utility class can set the cache time for each interface, and when the cache expires, the latest data will be fetched from the server.

Here are some solutions to some of the problems I encountered during project refactoring:

1: When using Retrofit to access the network, you need to create javabeans first. For some developers who need to get Json to create Javabeans, or need to see the data returned from Json, this kind of thing can be used

Packaging is too much of a good thing, so I at the time of initial retrofit, will addConverterFactory (GsonConverterFactory. The create ()) this method commented out,

Observable postDaDian(@fieldMap Map Map), and change the general after Observable to ResponseBody in the APIStores class.

So the onSuccess() method of ApiCallback gets the ResponseBody object, and we call new String(responseBody.string()) to get the returned Json

2: In order to prevent APP crash caused by failure to parse Json string using Gson, we need to add try/catch when parsing Json. For this, I created DefaultParser generic class for unified parsing

3. Cache issues with RetroFIT: After looking through some information I found that RetroFIT does not provide caches, so we need to add caches separately. Some examples on the web are mostly for GET

Requests add caching, which is rarely mentioned for POST requests. Therefore, I found ACache tool class for ACache from the Internet. This tool class can set the cache time of each interface, and it is quite flexible to use.

I’ve combined ACache with Retrofit to return cached data when a user calls a network request method in Presenter before the cache time is up.

4: Before rXBus is used to transmit messages, broadcast or eventBus is usually used to transmit data. This reconstruction directly uses RXBus to replace most broadcasts to send messages. Rxbus can transmit various kinds of messages

Type of data, such as Javabeans, HashMap, ArrayList, String, etc., which are shown in the sample demo. The reason I’m replacing most of the broadcasts is because the project uses the alarm clock function

You need to send a broadcast.

Retrofit’s network request method can be called directly in a utility class, adapter, or Dialog that requires access to the network, without combining MVP with RXJava

In ApiStores, an Observable postGetIocnList(@fieldMap Map Map) should be replaced with a Call

6: RXbus repeatedly receives messages: problems caused by repeated registration. Scenario: Send a message and receive multiple messages. When deleting a list, rXbus will be called to send and receive messages and update the list

The list of the UI. Each time a Adapter is created here, rXBus is registered, and each drop-down refresh recreates adapter and registers RXbus, resulting in repeated registration of rXbus. The solution is to register every time

The registered tag is added to the ArrayList collection. The next time you register, the collection is iterated to see if there is a tag. If there is, the registered code is not used.

public static boolean isAdded(Class tClass)

{

Logutils.i (” pass tClass: “+ tclass.toString ());

boolean isAdd = false;

for (int i = 0; i

if (tClass.toString().equals(mArrayList.get(i)))

{

isAdd = true;

}

}

return isAdd;

}