As of today, I’ve decided to do the right thing and open source some of the projects THAT I think are valuable, along with some deep theoretical analysis. For those who want to improve the level of Android development technology, but do not have the idea of some methods of enlightenment. At the same time, it can also expand personal technical vision and horizon. Let’s take a closer look at our first open source project.

The project name DDNetCore encapsulates the main purpose of this framework is to simplify the acquisition and parsing of network data, so that business developers can extricate themselves from the complex transaction of network data acquisition and focus more on the development of business logic.

Project git address :github.com/jessie345/D…

Let me try to explain how the framework is used and how it works in a more understandable way. First of all, a frame flow chart can be used to understand the general process of internal interaction of the framework from a macro perspective.






Data request frame.png

Now, in accordance with the execution sequence described by the flow chart, we will gradually explain the use mode and principle of the framework.

The first step in making a data request is to instantiate the request object and shove it to the RequestManager for execution. The native approach involves creating separate Request subclasses for different requests that handle and instantiate request-related logic (overriding the associated callback methods). However, there is a problem with this approach. For most requests, the logic for processing the result of the request and the result of the response is basically the same. To reduce the repetitive effort of creating subclasses, the network library provides the DataRequestBuilder builder, which can create generic object requests (the return result can be equivalent to a bean). List request instance (the return result can be compared to List<Bean>). This mode greatly simplifies the upfront work required to initiate a request and makes writing a data interface fairly easy. Here’s an example:

  Request request = DataRequestBuilder.asArrayRequest("/products")
                .requestId(REQUEST_FETCH_PRODUCTS)
                .requestDefaultStrategy(Constants.STRATEGY_LEVEL3_CACHE)
                .requestExpireStrategy(Constants.STRATEGY_REFRESH_CACHE)
                .memoryCache()
                .dbCache()
                .dataClass(ProductBean.class)
                .httpMethod(Constants.HTTP_METHOD_GET)
                .build();
Copy the code

The above lines of code create a data request to get a list of products. The first line specifies to create an array request with path/Products, that is, the data returned by the server is a list. The second line of code specifies the ID of the request so that I can easily tell which request was returned in the response callback. The third and fourth lines of code indicate the request execution policies for the default case and the request response result expiration case respectively. The framework provides three data request policies, which are: Constants.STRATEGY_LEVEL3_CACHE (level 3 cache policy), Constants.STRATEGY_REFRESH_CACHE(forced refresh cache policy), Constants.STRATEGY_ONLY_NET(Mandatory request network policy). Line 5 and line 6 specify whether the request uses memory and database cache. The seventh line specifies which class to deserialize the data returned by the server. The penultimate line specifies that the request will be executed as GET, and the framework has built-in support for both basic request methods,Constants.HTTP_METHOD_GET(basic GET request), and. The content-type of post can be Application /json,www-form-urlencoded)

With the request instance, we can hand it off to the RequestManager for execution. I wrapped the following code in BaseActivity:

public void enqueueRequest(Request request) { if (! mNetworkControl.isControlListenerRegistered(this)) { mNetworkControl.registerControlListener(this); } mNetworkControl.enqueueRequest(request); }Copy the code

The request is sent to the RequestManager for execution through the NetWorkControl instance, where some of the logic associated with canceling the request is performed.

The request has been submitted to the thread pool for execution, so how do we receive the data results returned? The NetworkControl class provides a listener to get the result of the response:

    public void registerControlListener(@NonNull NetworkControlListener listener) {
        Preconditions.checkNotNull(listener);
        this.mListenerRef = new WeakReference(listener);
    }
Copy the code

Once the listener is registered, the request and response information can be obtained through the following three callbacks related to the request life cycle.

    public void handlePreNetRequest(@NonNull Request request) {
    }

    @Override
    public void handleNetRequestError(@NonNull Request request, @NonNull ResponseHeader rb) {
    }

    @Override
    public void handleReceivedResponse(@NonNull EventResponse event) {
    }
Copy the code

HandlePreNetRequest indicates that a request is about to be added to the request queue for execution. HandleNetRequestError indicates that a request error occurred. HandleReceivedResponse indicates that the result of the request is returned correctly. These three methods are used to determine the execution status of the Request.

See how easy it is to write a request interface.

If you want more information or are interested in the open source project, please add my wechat official account and I will wait for you there





Bear’s open source community. JPG