Dynamic network request management component packaging method and system based on Android componentization
By adopting a componentized dynamic network request management component encapsulation method on the Android platform, using the network connection pool for connection management and optimization, the problems of large thread overhead, long network delay, and low connection pool utilization in the existing technology are solved, and efficient network request processing and server load reduction are achieved.
Patent Information
- Application Number
- CN202510202578.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-24
- Publication Date
- 2025-05-16
AI Technical Summary
When handling dynamic network requests, the prior art leads to large thread overhead, long network delay, low connection pool utilization, and the inability to effectively classify network requests, increasing the server load.
By designing a component-based dynamic network request management component encapsulation method on the Android platform, using a network connection pool for connection management, the connection is divided into main connection, dynamic connection and temporary connection, dynamically adjust the connection priority according to the network environment level, and dynamically issue network configuration through the server to adjust it.
It reduces thread overhead, shortens network latency, improves the utilization rate of connection pools, realizes hierarchical processing of network requests, reduces server load, and improves network communication authentication and session accuracy.
Smart Images

Figure CN120018172A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of mobile phone mobile terminal App development framework, and specifically relates to a dynamic network request management component encapsulation method and system based on Android componentization. Background Art
[0002] At present, with the comprehensive functions of mobile App, the single network domain name request of the client can no longer meet the current business needs. An App usually involves multiple different businesses or calls multiple third-party services. These businesses and services correspond to different domain names, IP addresses, and port numbers. This puts higher requirements on the encapsulation of mobile network components. At the same time, in the face of some special periods (such as the examination period of this project), the client needs to cooperate to give priority to some high-time network requests, and reduce some network requests to help the server reduce the network load in special periods. There are currently two commonly used client communication methods: one is based on the original network communication library of the system, creating a new Client client to implement GET, POST and other requests; the other is to use a third-party network communication library (such as OkHttp, Retrofit, etc.) based on the encapsulation of the original network communication library for network communication. The main implementation method of these two methods is to create a new thread and create a new Client client in the thread, and encapsulate the request header, set the request parameters, Header, Url and other information, and then send a request to the server according to the relevant interface protocol. After receiving the request, the server returns the result, the client receives the data, and then notifies the UI thread to refresh the data. At present, the common practice of the client is to use the singleton mode to establish a global Client for network requests.
[0003] When facing dynamic network communication services (for example, different services need to communicate with different domain name service hosts or third-party servers), the Client's Url request address needs to be frequently changed to point to different hosts or ports. Using the above general method, there are two ways to deal with it: one is not to use a singleton and frequently create a Client; the other is to use a singleton mode to guarantee a Client and frequently change the Client's Header, Url and other information. Both methods require re-establishing a TCP connection, which will result in frequent TCP three-way handshakes, which will bring a certain time delay. In addition, the first method of frequently creating a Client may cause connection leaks. Each time a Client is created, a connection is created. If these connections are not closed and released correctly, system resources will be occupied, and eventually the connection pool will be exhausted and new connections cannot be established. Frequent creation and destruction of Client will lead to a waste of connection pool resources. Each time a Client is created, operations such as initializing the connection pool and creating a connection are required, which consume system resources. The second method frequently changes the Url, so DNS resolution is required every time. This process involves querying the DNS server. Frequent DNS resolution will also increase the request delay, especially when the DNS server responds slowly or the network is unstable. This delay will be more obvious. Frequent URL changes may also cause the connections in the connection pool to not be effectively reused, reducing the utilization of the connection pool. Frequent URL changes can also easily cause confusion in authentication and session management and request compatibility issues, resulting in request failures or error responses. These two methods also cannot effectively process network requests in a hierarchical manner.
[0004] Therefore, it is necessary to design and develop a dynamic network request management component encapsulation method and system based on Android componentization to reduce thread overhead, shorten network latency, and improve connection pool utilization in order to address the above problems. Summary of the invention
[0005] The purpose of the present invention is to provide a dynamic network request management component encapsulation method and system based on Android componentization to address the problems existing in the prior art, which can not only reduce thread overhead, shorten network delay, improve the utilization rate of connection pool, ensure the accuracy and completeness of network communication authentication and session, but also ensure hierarchical processing of network requests in special periods by dynamically configuring network requests, thereby reducing the server load.
[0006] According to one aspect of the present invention, a dynamic network request management component encapsulation method based on Android componentization is provided, including creating an App project, building a componentized framework, developing common function components, developing various business components, and combining various business components, wherein building the componentized framework includes: Build the system API layer to call the functions, methods and objects in the system, and provide technical support for network communication for the network function layer, network control layer and network preprocessing layer; Build a network function layer to create a request service client and encapsulate the system network request function; Build a network control layer for dynamic network configuration and manage network requests from the preprocessing layer; Build a network preprocessing layer to uniformly manage network request APIs and encapsulate various components for direct calling.
[0007] Furthermore, the construction of the network control layer also includes a control module, which dynamically adjusts the priority levels of the main link, dynamic connection, and temporary connection according to the current network environment level and network configuration, and completes the network request of the network preprocessing layer.
[0008] Furthermore, the development common function component includes a dynamic network request management component, specifically: After launching the App to obtain the default network configuration, initialize the dynamic network request management component; The business component initiates a network request and adds the network request to the request queue. The control module reads the request queue requests in turn and calls the connection selection algorithm to obtain the processing object and processing method of the request. The control module matches the network request of the same connection type in the waiting queue according to the priority, allocates the idle connection to the network request in the waiting queue, and sends the network request to the network connection pool for processing; The network connection pool processes the current network request according to the acquired processing object and processing method; After the network request returns, the state machine status is updated, and the request result is returned at the same time to determine whether to update the cache. After receiving and processing the data, the temporary connection is destroyed.
[0009] Furthermore, the method also includes testing and packaging and publishing.
[0010] According to one aspect of the present invention, a dynamic network request management component encapsulation system based on Android componentization is provided, including an App project creation module, a componentized framework building module, a common function component development module, each business component development module and each business component combination module, wherein the componentized framework building module includes: The system API layer is used to call functions, methods and objects in the system, and provide technical support for network communication for the network function layer, network control layer and network preprocessing layer; The network function layer is used to create a request service client and encapsulate the system network request function; The network control layer is used for dynamic network configuration, including network connection pool, control module, connection selection algorithm, network connection encapsulation, state machine, tool class, request queue, network detection module, and cache module; The network preprocessing layer is used to uniformly manage network request APIs and encapsulate various components for direct calling.
[0011] According to one aspect of the present invention specification, there is provided an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the dynamic network request management component encapsulation method based on Android componentization when executing the computer program.
[0012] According to one aspect of the present invention, a computer-readable storage medium is provided, which stores a computer program, and when the computer program is executed by a processor, the steps of the dynamic network request management component encapsulation method based on Android componentization are implemented.
[0013] According to one aspect of the present invention, there is provided a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the steps of the dynamic network request management component encapsulation method based on Android componentization.
[0014] Compared with the prior art, the present invention has the following beneficial effects: The present invention proposes a dynamic network request management component encapsulation method based on Android componentization. By classifying and managing the connections in the network connection pool, and dividing them into main connections, dynamic connections, and temporary connections according to their importance, the number of network connections of different types can be flexibly configured and differentiated. This can reduce thread overhead, shorten network latency, improve the utilization rate of the connection pool, and improve the accuracy of network communication authentication and sessions.
[0015] The present invention proposes a dynamic network request management component encapsulation method based on Android componentization, which can dynamically adjust the priority levels of the main link, dynamic connection, and temporary connection according to the current network environment level and network configuration through the set network detection module and control module.
[0016] The present invention proposes a dynamic network request management component encapsulation method based on Android componentization, which dynamically adjusts the number of networks at all levels, priorities, number of connections, and number of composite connections through a server dynamically issuing network configurations, making management easier. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 A development flow chart of an embodiment of the present invention; Figure 2 This is an overall architecture diagram of a dynamic network request management component according to an embodiment of the present invention; Figure 3 A control flow chart of a dynamic network request management component according to an embodiment of the present invention; Figure 4 A single network request flow chart of an embodiment of the present invention; Figure 5 This is a network connection pool sequence diagram of an embodiment of the present invention. DETAILED DESCRIPTION
[0019] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0020] like Figure 1 As shown, the embodiment of the present invention provides a development process of a dynamic network request management component encapsulation method and system based on Android componentization, and the specific development steps are as follows: 1. Create an App project: Create a project as a business function carrier to control the entire project.
[0021] 2. Component-based framework construction: Select a suitable component-based framework to provide support for the development of each component and facilitate communication routing control between components.
[0022] 3. Public function component development: various public functions or controls that may be used by the software, such as multimedia components, permission access control components, code scanning capability components, network communication components, etc.
[0023] 4. Development of various business components: The development of business function components is to develop independent module function components after dividing various function modules according to business function requirements. The components can also be divided into various dynamic sub-components or function modules. Each business component may correspond to its own unique network domain name, which can be single or dynamic.
[0024] 5. Business component combination: Combine the required businesses according to business needs to form a complete App software.
[0025] 6. Testing: Use the test box to write test code for unit testing, integration testing, system testing, etc.
[0026] 7. Package and release: Use code obfuscation technology, signature technology, encryption technology, multi-channel packaging and other related technologies to package into executable software.
[0027] like Figure 2 As shown, the embodiment of the present invention also provides an overall architecture diagram of the dynamic network request management component, and explains the various subsystems, functions, and hierarchical structures in the dynamic network request management component. It adopts a layered design concept from abstract to concrete, and there are four layers from bottom to top, namely, the system API layer, the network function layer, the network control layer, and the network preprocessing layer, as follows: System API layer: mainly calls system-related functions, methods, and objects to implement specific functions, and is the basic guarantee for software to communicate over the network; Network function layer: includes http requests, websocket connections, third-party cloud connections, etc., which are used to create specific request service clients, and are encapsulations of the system network request function, providing friendly interface operations and controls for the upper layer. Encapsulate specific third-party network requests downward; Network control layer: mainly includes network connection pool, control module, connection selection algorithm, network connection implementation, state machine, tool class, request queue, network detection module, cache module, etc.; Network preprocessing layer: mainly manages and encapsulates network request API in a unified manner to facilitate direct calls by various components, eliminating the need for each component to have an interface file, facilitating addition, modification, and deletion, including API interface, parameter encapsulation and encryption, data model, adapter, DNS cache, interceptor, network initialization component, and request header encapsulation.
[0028] Specifically, the network control layer is the brain of the dynamic network request management component. The control module manages the network requests of the preprocessing layer and completes the network requests by allocating, replacing, and rebuilding network connections according to the dynamic network configuration, the current level of the network, the number of state machine network connection pools, and the connection selection algorithm. Among them, the network connection pool is an encapsulated network connection thread pool, which contains three types of network connections: primary connection, dynamic connection, and temporary connection. The control module manages the network requests of the preprocessing layer and completes the network requests by allocating, replacing, and rebuilding network connections according to the dynamic network configuration, the current level of the network, the number of state machine network connection pools, and the connection selection algorithm. The connection selection algorithm adopts the chain of responsibility model according to the network configuration, network environment, state machine status, and other conditions. The connection elimination mechanism is specifically as follows: when the number of dynamic connections in the network connection pool has been used up, and the current dynamic connections cannot meet the network request, the dynamic connection must be eliminated at this time. Using the priority scheduling strategy, the least recently used (LRU) elimination mechanism finds the dynamic connection with the lowest usage rate according to the records in the state machine for elimination and replacement; network connection encapsulation is a specific encapsulation implementation of each requested network using the factory mode; the state machine is used to store the status, call count and other information corresponding to each domain name connection in the current network connection pool, providing data support for the connection selection algorithm; the tool class includes some common methods, implementations, conversions and other auxiliary classes; the request queue is a queue for the unified storage and management of related requests of each business component; the network detection module is a module for detecting the current network environment; the cache module is divided into data cache and DNS cache.
[0029] Specifically, the specific process of the connection selection algorithm in the embodiment of the present invention transmitting the request until there is an object that can process the request is as follows: 1. When the API level of the network request is level 1, obtain whether the network connection type required by the current network request of the state machine is available. If it is available, use it directly. If it is not available, create a temporary connection and add it to the network connection pool. At the same time, create a composite connection group based on whether the composite connection is enabled in the network configuration. According to the network level returned by the network detection module, if it is level 1, there is no restriction. When the network is level 2, a notification is issued to suspend the multimedia cache of this App, file upload and download and other network-consuming events. When the network status is level 3, the network requests waiting in the queue are suspended on the basis of level 2. When the request is completed, reply to the corresponding queue request and notify the multimedia cache, etc.
[0030] 2. When the API level of the network request is level 2 or not set, obtain whether the network connection type required by the current network request of the state machine is available. If it is available, use it directly. If it is not available, if there are remaining connections in the network connection pool, a new connection can be added to the connection pool. Otherwise, the request will be added to the waiting queue to wait for a free connection.
[0031] 3. When the API level of the network request is level 3, obtain whether the network connection type required by the current network request of the state machine is available. If it is available, use it directly. If not, add it to the end of the waiting queue and wait for the 1st and 2nd level APIs to complete the request and obtain the corresponding connection for the network request.
[0032] 4. When the API level of the network request is level 4, the current network request is temporarily cached locally and uploaded once when the application is closed or during non-special periods. It is suitable for data with low timeliness requirements or reducing server load during specific periods (such as some non-exam related heartbeat records and behavior data during exams).
[0033] Specifically, the network detection module is a module that detects the current network environment. According to the current network environment and speed, the network is divided into three levels: level 1 high-speed network (5G network or speed above 10Mbps); level 2 medium-speed network (4G network or speed between 1-10Mbps); level 3 weak network speed is generally below 1Mbps. When the network environment changes, the network level is re-evaluated, and if it changes from the previous level, a notification is sent to the control module.
[0034] Specifically, the cache module is divided into data cache and DNS cache. Data cache caches the data requested by the interface. When some pages need to display data in a timely manner, they first read the local cache and then update it after the interface returns new data, such as (home page, course list page). DNS cache converts domain names into IP addresses for caching, and directly uses IP access during the cache validity period to improve domain name resolution efficiency and enhance user network experience.
[0035] like Figure 3 As shown, the embodiment of the present invention also provides a control flow chart of a dynamic network request management component, which uniformly adds the network requests of various business components and public components in the project to the request queue, and the control module selects the optimal connection for network requests based on relevant configurations, environments, algorithms, etc. The number of connections corresponding to the network connection pool is dynamically adjusted according to the network configuration, priority control temporarily suspends access to the interface, and the network connection is dynamically adjusted according to the network environment. Among them, the dynamic network configuration sent by the server is divided into two parts. The first part is the network connection configuration, which mainly configures the number of three types of connections (main connection, dynamic connection, temporary connection), the number of main connection composite connections, etc. The second part is the API priority configuration. When the priority is set to 1, the API is accessed first. If there is no setting, the default level is 2 and no special processing is performed. When set to 3, it will join the waiting queue and wait for level 1 and level 2 to complete before accessing. When set to 4, the current network request will be temporarily cached locally and not sent to the server. The specific workflow is as follows: 1. After starting the App to obtain the default network configuration, initialize the dynamic network request management component, initialize the main connection, and wait for standby.
[0036] 2. The business component initiates a network request and adds the network request to the request queue. The control module reads the request queue requests in turn and calls the connection selection algorithm to obtain the processing object of the request (connection type, whether to enable composite connection, priority, allocation of connection ID, connection replacement, reconstruction, etc.) and the processing method (directly request to enter the network connection pool or wait in the waiting queue).
[0037] 3. The network connection pool processes the current network request according to the obtained processing object. If the composite connection is enabled, a step-by-step concurrent connection is performed at intervals of 100 milliseconds. After one connection is connected, the other connections are canceled.
[0038] 4. After the network request returns, update the corresponding state of the state machine, the last usage time, the number of times used, and other information (except temporary connections). Return the request result to the Ui thread and determine whether to update the cache. Notify the control module of the connection id of the completed network request (except temporary connections). Destroy the temporary connection after receiving and processing the data.
[0039] 5. The control module matches the network request of the same connection type in the waiting queue according to the priority, allocates the idle connection to the network request in the waiting queue, and sends the network request to the network connection pool for processing.
[0040] like Figure 4 As shown, the embodiment of the present invention also provides a single network request flow chart, the main function of which is to establish a specific network connection. The network connection can be a connection in the network connection pool or a newly created temporary network connection. The specific steps are as follows: 1. Construct a network instance, that is, construct an instance of a network type. The usual practice is to use the same singleton object for all network requests. In order to handle the frequently changing network domain name addresses of dynamic networks, the improved approach of the present invention is to create a new network request and then hand it over to the thread pool for management. Figure 2 The algorithm strategy and elimination mechanism related to the network control layer ensure the number of clients while taking into account the stability of network requests.
[0041] 2. Configure network request parameter initialization and uniformly configure network requests to complete the dynamic proxy settings.
[0042] 3. Create a network request object, translate the annotations in the interface into corresponding parameters, determine the return value response type of the network request interface and the corresponding adapter; 4. Send a network request to Figure 2 The network function layer encapsulates related network requests and sends network requests.
[0043] 5. Parse and adapt data, and use the data type adapter configured before the request to parse the data.
[0044] 6. Process the data and display it, switch threads according to the processing results, display the UI thread accordingly or handle exceptions based on the data.
[0045] like Figure 5 As shown, an embodiment of the present invention provides a network connection pool sequence diagram, which mainly explains the network request sequence of the network connection pool. The connection pool adopts a responsibility chain model, matches the used connections in sequence, finds the corresponding connection, and then executes a single network request flow chart from creating a network request object to processing data and displaying it, thereby completing the network request.
[0046] The implementation basis of each embodiment of the present invention is to implement programmed processing through a device with a processor function. Therefore, in engineering practice, the technical solutions and functions of each embodiment of the present invention are encapsulated into various modules. Based on this reality, on the basis of the above embodiments, an embodiment of the present invention provides a dynamic network request management component encapsulation system based on Android componentization, which is used to execute the dynamic network request management component encapsulation system based on Android componentization in the above method embodiment.
[0047] The system includes an App project creation module, a componentized framework building module, a public function component development module, various business component development modules and various business component combination modules, wherein the componentized framework building module includes: a system API layer, which is used to call functions, methods and objects in the system, and provide technical support for network communication for the network function layer, network control layer and network preprocessing layer; a network function layer, which is used to create a request business client and encapsulate the system network request function; a network control layer, which is used for dynamic network configuration, including a network connection pool, a control module, a connection selection algorithm, a network connection encapsulation, a state machine, a tool class, a request queue, a network detection module and a cache module; a network preprocessing layer, which is used to uniformly manage the network request API and encapsulate various components for direct calling.
[0048] The dynamic network request management component encapsulation system based on Android componentization provided by the embodiment of the present invention adopts several modules to meet the requirements that network requests cannot be processed in a hierarchical manner, thread overhead is large, network latency is large, and connection pool utilization is low. By classifying and managing the connections in the network connection pool and dividing them into main connections, dynamic connections, and temporary connections according to their importance, it is possible to flexibly configure and differentiate the number of network connections of different types. Through the set network detection module and control module, it is possible to dynamically adjust the network connection according to the network quality. By dynamically issuing the network configuration through the server, the number of networks at all levels, priorities, number of connections, and number of composite connections can be dynamically adjusted, which is easier to manage.
[0049] Based on the same inventive concept as the aforementioned embodiment, an embodiment of the present invention further provides an electronic device, including a memory and a processor, the memory being used to store computer-executable instructions, and the processor being used to execute computer-executable instructions, to implement a dynamic network request management component encapsulation method based on Android componentization as proposed in the aforementioned embodiment.
[0050] The embodiment of the present invention also provides a computer-readable storage medium on which a computer program is stored. When the program is executed by the processor, it is used to realize flexible configuration and differentiated management of the number of network connections of different types, and to realize dynamic adjustment of network connections according to network quality. The storage medium can be any non-volatile storage device such as a hard disk, a solid-state drive, a flash drive, an optical disk, etc., for storing computer program codes and necessary data files. The stored computer program includes: an App project creation module, a componentized framework building module, a common function component development module, each business component development module, and each business component combination module.
[0051] The embodiment of the present invention also provides a computer program product including instructions, which, when executed on a computer, generates in whole or in part the dynamic network request management component encapsulation method based on Android componentization proposed in the above embodiment. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device.
[0052] Finally, it should be pointed out that the above specific embodiments are only representative examples of the present invention. Obviously, the present invention is not limited to the above specific embodiments, and there are many variations. Any simple modification, equivalent changes and modifications made to the above specific embodiments based on the technical essence of the present invention should be considered to belong to the protection scope of the present invention.
Claims
1. A dynamic network request management component encapsulation method based on Android componentization, characterized in that: It includes creating App projects, building componentized frameworks, developing common functional components, developing various business components, and combining various business components. Building componentized frameworks includes: Build the system API layer to call the functions, methods and objects in the system, and provide technical support for network communication for the network function layer, network control layer and network preprocessing layer; Build a network function layer to create a request service client and encapsulate the system network request function; Build a network control layer for dynamic network configuration and manage network requests from the network preprocessing layer; Build a network preprocessing layer to uniformly manage network request APIs and encapsulate various components for direct calling.
2. According to a method for encapsulating dynamic network request management components based on Android componentization as described in claim 1, it is characterized in that: The network control layer is constructed, and also includes a control module, which dynamically adjusts the priority levels of the main link, dynamic connection, and temporary connection according to the current network environment level and network configuration, and completes the network request of the network preprocessing layer.
3. According to a method for encapsulating dynamic network request management components based on Android componentization as described in claim 1, it is characterized in that: The development common function components include a dynamic network request management component, specifically: After launching the App to obtain the default network configuration, initialize the dynamic network request management component; The business component initiates a network request and adds the network request to the request queue. The control module reads the request queue requests in turn and calls the connection selection algorithm to obtain the processing object and processing method of the request. The control module matches the network request of the same connection type in the waiting queue according to the priority, allocates the idle connection to the network request in the waiting queue, and sends the network request to the network connection pool for processing; The network connection pool processes the current network request according to the acquired processing object and processing method; After the network request returns, the state machine status is updated, and the request result is returned at the same time to determine whether to update the cache. After receiving and processing the data, the temporary connection is destroyed.
4. According to a method for encapsulating dynamic network request management components based on Android componentization as described in claim 1, it is characterized in that: The method also includes testing and packaging for release.
5. A dynamic network request management component packaging system based on Android componentization, characterized in that: It includes App project creation module, componentized framework building module, public function component development module, business component development module and business component combination module. The componentized framework building module includes: The system API layer is used to call functions, methods and objects in the system, and provide technical support for network communication for the network function layer, network control layer and network preprocessing layer; The network function layer is used to create a request service client and encapsulate the system network request function; The network control layer is used for dynamic network configuration and management of network requests from the network preprocessing layer; The network preprocessing layer is used to uniformly manage network request APIs and encapsulate various components for direct calling.
6. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the dynamic network request management component encapsulation method based on Android componentization described in any one of claims 1 to 4 are implemented.
7. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the dynamic network request management component encapsulation method based on Android componentization described in any one of claims 1 to 4 are implemented.
8. A computer program product comprising instructions, characterized in that When it is run on a computer, the computer is enabled to execute the steps of the dynamic network request management component encapsulation method based on Android componentization as described in any one of claims 1 to 4.
Citation Information
Cited By
Page information display method and device, storage medium and program product
CN120632236A