Method and device for processing network requests in Android application programs

A network request and application technology, applied in the computer field, can solve the problems of difficult maintenance of page update logic, UI update without a unified standard protocol, etc., to achieve the effect of convenient maintenance and ensure uniformity

Active Publication Date: 2017-11-21
WUHAN DOUYU NETWORK TECH CO LTD
5 Cites 2 Cited by

AI-Extracted Technical Summary

Problems solved by technology

[0004] This method of artificially updating the UI directly is a strong coupling relationship, which will lead to no unified specification pro...
View more

Method used

In the embodiment of the present invention, by hiding all the pages displayed in the user interface, and then presenting the pages to be displayed according to the network request return status and data delivered to the UI layer, it is possible to effectively avoid displaying the current When the page is displayed, the previous page is still covered on it, which improves the accuracy of page display and reduces the chance of bugs.
Specifically, when network request is processed, at first network request return state and data are encapsulated in message class, and wherein, message class is message class; Then by the message processor handler in Android system this message class Send it to the main thread, so that the status and data returned by the network request can be obtained in the main thread. Then pass the information in the message class of the main thread to the user interface layer through the update interface interface, where the update interface interface is the UpdateUI interface; The layer's network requests return status and data update pages. And through interface coupling, the coupling relationship between network requests and UI can be reduced, and at the same time, all page updates have the same logical interface, which is convenient for later program maintenance.
Specifically, when network request is processed, first encapsulation sending module 201 is encapsulated into message class by network request return status and data; Then encapsulation and sending module 201 passes this message class by the message processor handler in Android system Send it to the main thread, so that the information returned by the network request can be obtained in the main thread. The transmission module 202 transmits the information in the message class of the main thread to the user interface layer through the update interface interface (ie, the UpdateUI interface), so that the status and data update page can be returned according to the network request transmitted to the UI layer. And through interface coupling, the coupling relationship between network requests and UI can be reduced, and at the same time, all page updates have the same logical interface, which is convenient for later program maintenance.
Utilize function Public void onSuccess (String json) and function public void onError (String code, String message), the network request re...
View more

Abstract

The invention provides a method and device for processing network requests in Android application programs. The method comprises the following steps of: S1, packaging a network request return state and data in a message class and sending the message class to a main thread through a message processor; and S2, transferring information in the message class of the main thread to a user interface layer through updating an interface. According to the method and device, a coupling relationship between network request and page updating is reduced through interface coupling, and a same logic interface is provided for the updating of all the pages, so that the uniformity of user interface updating structures is ensured and the later program maintenance is facilitated.

Application Domain

Interprogram communication

Technology Topic

Application softwareProgram maintenance +4

Image

  • Method and device for processing network requests in Android application programs
  • Method and device for processing network requests in Android application programs
  • Method and device for processing network requests in Android application programs

Examples

  • Experimental program(1)

Example Embodiment

[0027] In order to make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are the present invention. Invented some embodiments, but not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative work shall fall within the protection scope of the present invention.
[0028] figure 1 This is a flowchart of a method for processing a network request in an Android application provided by an embodiment of the present invention, such as figure 1 As shown, the method includes: S1, encapsulating the return status and data of the network request into a message class, and sending the message class to the main thread through the message processor; S2, sending the message of the main thread through the update interface interface The network request in the class returns status and data to the user interface layer.
[0029] Among them, encapsulation means to hide the attributes and implementation details of the object, only expose the interface to the outside, and control the access level of the read and modify attributes in the program; combine the abstracted data and behavior (or function) to form an organic whole, That is, the data and the source code for operating the data are organically combined to form a "class", in which data and functions are members of the class.
[0030] Among them, the message processor is a handler, which is a tool class provided in the Android system for communication between the main thread and the sub-thread.
[0031] Among them, when a program starts, there is a process created by the operating system (OS), and a thread runs immediately at the same time. This thread is usually called the main thread of the program because it is executed when the program starts. Yes, if you need to create a thread again, then the created thread is a child thread of the main thread. Every process has at least one main thread. In Winform, it should be the thread that creates the GUI. The importance of the main thread is embodied in two aspects: on the one hand, it is the thread that generates other child threads; on the other hand, it must usually complete the final execution such as performing various shutdown actions.
[0032] Among them, the user interface (UI for short, also known as the user interface) is a medium for interaction and information exchange between the system and the user, and it realizes the conversion between the internal form of information and the form acceptable to humans. The user interface is related software designed for interaction and communication between the user and the hardware. The purpose is to enable the user to operate the hardware conveniently and efficiently to achieve two-way interaction and complete the work that the hardware wants to complete. The user interface is widely defined , Including human-computer interaction and graphical user interface, there are user interfaces in all fields that participate in the information exchange between humans and machines.
[0033] In the development process, usually time-consuming business logic such as sending network requests is carried out in the child thread, but the business logic such as UI presentation is carried out in the main thread. In other words, the UI and the network request are not in the same thread, so the UI cannot be updated directly in the child thread of the network request. In order to be able to operate the business logic in the UI thread, the state in the network thread needs to be transferred to the UI thread.
[0034] Specifically, when processing a network request, first encapsulate the return status and data of the network request into a message class, where the message class is the message class; then the message class is sent to the host through the message processor handler in the Android system In this way, the status and data of the network request can be obtained in the main thread. Then pass the information in the message class of the main thread to the user interface layer through the update interface interface. The update interface interface is the UpdateUI interface; the user interface layer is the UI layer, which is the logic layer that controls the UI business, so that it can be passed to the UI according to The network request of the layer returns the status and data update page. And through the interface coupling, the coupling relationship between the network request and the UI can be reduced, and at the same time, all the page updates have the same logical interface, which is convenient for the maintenance of the later program.
[0035] In the embodiment of the present invention, by encapsulating the network request return status and data into a message class, and sending the message class to the main thread through the message processor, the information returned by the network request can be obtained in the main thread; Then the information in the main thread message class is transferred to the user interface layer through the update interface interface, so that the status and data update page can be returned according to the network request passed to the user interface layer, and the network request and UI update are reduced through interface coupling The coupling relationship between them also makes all page updates have the same logical interface, ensuring the unity of the updated user interface structure and facilitating the maintenance of the program later.
[0036] On the basis of the foregoing embodiment, before the S1, it further includes: dividing the network request return status into network request success and network request failure, and the network request success corresponds to a first network request success callback function, and the network The request failure corresponds to a first network request failure callback function; the first network request success callback function and the first network request failure callback function are used to obtain the network request return status and data when the network request succeeds and fails, respectively.
[0037] Specifically, before encapsulating the network request return status and data in the message class, it also includes designing and encapsulating the network request return status, that is, the network request return status is divided into network request success and network request failure. And the network request success corresponds to the network request success callback function, the network request success callback function is used to receive the data returned by the server according to the successful network request, for example, the network request success callback function is onSuccess(Stringjson), but it is not limited to this . The parameter part of the callback function of the successful network request is of type String (string type), and the name of the parameter is json for receiving json data returned by the server.
[0038] The network request failure corresponds to the network request failure callback function. The network request failure callback function is used to receive the failure code and failure reason information returned by the server according to the failed network request. For example, the network request failure callback function is onError(String code, String message) ), but not limited to this. The network request failure callback function has a total of two parameters. These two parameters are: failure code and failure reason information corresponding to each failure code. The string type message is the failure reason information, which is convenient for subsequent maintenance personnel to view the details. The reason for failure.
[0039] Then encapsulate the two parts of the network request status and data into the message class, and send the message class to the main thread through the message handler handler, and pass the information in the main thread message class to the UI layer through the UpdateUI interface , So that you can return the status and data update page according to the network request passed to the UI layer.
[0040] In the embodiment of the present invention, by dividing the return status of the network request into two parts: the success of the network request and the failure of the network request, and the two parts have corresponding callback functions, all the statuses returned by the network request can be completely described. The follow-up logic implementation lays the foundation.
[0041] On the basis of the foregoing embodiments, after the S2, the method further includes: hiding the displayed page in the user interface, and presenting the page to be displayed according to the network request transmitted to the user interface to return status and data.
[0042] Specifically, after the information in the message class of the main thread is transferred to the UI layer through the update interface interface (ie UpdateUI interface), the user interface needs to return the status and data according to the network request passed to the UI layer to present the page to be displayed, for example If the network request fails, it needs to display the failure information page corresponding to the failure code returned by the network request failure; or, if the network request is successful, it needs to display the data information page returned by the network request successfully. Since there are many kinds of web page status, there are various failed pages and data pages, if improper control, it is easy to cause other failed pages to be overlaid on the current page to be displayed, causing bugs. Therefore, before presenting the page to be displayed according to the network request to the UI layer to return the status and data, all the pages that have been displayed in the user interface are hidden, and then the page to be displayed is presented according to the network request to return the status and data, thereby Get the page information that needs to be presented.
[0043] In the embodiment of the present invention, by hiding all the pages that have been displayed in the user interface, and then presenting the page to be displayed according to the state and data of the network request passed to the UI layer, it can effectively avoid displaying the current page. The previous page is still overlaid on it, which improves the accuracy of page display and reduces the chance of bugs.
[0044] On the basis of the foregoing embodiments, the step of encapsulating the return status and data of the network request into a message class further includes: defining a first hash map of the return status of the bearer network request, and the first hash map of the data when the bearer network request is successful. The second hash map, the third hash map that carries the error code when the network request fails, and the fourth hash map that carries the error information when the network request fails; the first hash map and the second hash map , The third hash map and the fourth hash map form a list set, and the list set is encapsulated into the message class through the setdate method in the message class.
[0045] Among them, the hash map is an implementation of the Map interface based on the hash table. This implementation provides all optional mapping operations and allows the use of null values ​​and null keys. (Except for being asynchronous and allowing the use of null, the HashMap class is roughly the same as the Hashtable.) This class does not guarantee the order of mapping, especially it does not guarantee that the order will remain unchanged forever. This implementation assumes that the hash function appropriately distributes the elements among the buckets, which can provide stable performance for basic operations (get and put). The time required to iterate the collection view is proportional to the "capacity" (the number of buckets) of the HashMap instance and its size (the number of key-value mapping relationships).
[0046] Specifically, when encapsulating the return status and data of the network request to the message class, first define the first hash map that carries the return status of the network request. Since there are two types of return status of the network request, define a set of Hashmap objects (key value To key-value) to carry the network request return status. In order to easily know whether the return status of the network request is successful or failed, a hashmap object is defined, that is, the first hash map. Its key is status and value has two cases. If the value is 1, it means that the return status of the network request is successful; If the value is 2, it means that the network request returns status as failed.
[0047] In addition to describing the return status of the network request, additional information other than the status is required. If the return status of the network request is successful, that is, the status is 1. At this time, the json data returned by the network request needs to be encapsulated in the message class. Define a hashmap object, that is, the second hash map, the key in the hashmap object is the data format type json, and the value is the corresponding network data.
[0048] If the return status of the network request is failed, that is, the status is 2. At this time, the failure code and failure information returned by the network request failure must also be encapsulated in the message class. Therefore, two hashmap objects need to be defined, namely the third hash map and the third hash map. Four hash maps, the third hash map is used to store failure codes, and the fourth hash map is used to store failure information corresponding to the failure codes.
[0049] Then these four hashmap objects form a list collection, that is, a list collection, and store the list collection in the message class through the setdate method in the message class, and then send the message class from the child thread to the main thread through the message processor handler .
[0050] In the implementation of the present invention, by defining corresponding hash maps for the return status and data of the network request, and storing the list set composed of these hash maps in the message class through the setdate method in the message class, the network request is returned The status and data are encapsulated into a message class to provide a basis for sending the status and data of the network request in the child thread to the main thread.
[0051] On the basis of the foregoing embodiments, the step of sending the message class to the main thread through the message processor further includes: sending the message class to the main thread through the message sending method in the message processor Message function.
[0052] Specifically, after the return status and data of the network request are encapsulated into the message class, the message class is sent to the main thread through the message handler handler. In order to be able to receive messages from the child threads in the main thread, it needs to be replicated in the main thread The message processing function, that is, the handlermessage function. In other words, the message sending method (ie, sendmessage method) in the message handler handler sends the message class to the handlermessage function in the main thread. Then in the handlerMessage function to process and receive the message of the child thread. When the message processor handler sends a message through the sendMessage method, the system will store the message in the message queue of the handler and then take the message out of the message queue and send it to the handlerMessage function of the main thread, so that it can be in the main thread Got the corresponding news.
[0053] In the embodiment of the present invention, the message class is sent to the message processing function in the main thread through the message sending method in the message processor, and the processing of sending the network request return status and data in the sub-thread to the main thread is realized. In the message function, the return status and data of the network request can be obtained in the main thread.
[0054] On the basis of the foregoing embodiments, the update user interface interface includes a second network request success callback function and a second network request failure callback function; using the second network request success callback function and the second network request failure callback function The callback function respectively transfers the network request return status and data in the main thread message class to the user interface layer when the network request succeeds and fails.
[0055] Specifically, after sending the return status and data of the network request in the child thread to the main thread, the return status and data of the network request sent to the main thread are transferred to the user interface layer through the update interface interface. , UpdateUI interface) includes a second network request success callback function and a second network request failure callback function, and these two functions are completely consistent with the design of the first network request success callback function and the first network request failure callback function. For example, the second network request success callback function is Public void onSuccess(String json), and the second network request failure callback function is public void onError(String code, String message), but it is not limited to this.
[0056] Using the function Public void onSuccess(String json) and the function public void onError(String code, String message), the network request return status and data in the main thread message class when the network request succeeds and fails are respectively transferred to the user interface layer, In this way, the return status and data of the network request sent from the child thread to the main thread can be transparently transmitted to the user interface layer, that is, this step does not perform any processing on the message but only transparently transmits the message. And because the interface coupling relationship is used, the coupling relationship between the network request and the UI is reduced, and the callback function of the page update can be unified.
[0057] On the basis of the foregoing embodiments, the step of transmitting the return status and data of the network request in the main thread message class to the user interface layer by updating the interface interface further includes: the message processing function in the main thread transfers the The network request return status and data in the message class are decoupled; the decoupled network request return status and data are transferred to the user interface layer through the update interface interface.
[0058] Specifically, after sending the return status and data of the network request in the child thread to the main thread, the message processing function in the main thread decouples the return status and data of the network request in the message class, and then returns with the network request The updated interface interface with consistent state design will transparently transmit the decoupled network request return state and data to the user interface layer, so that the user interface layer presents the page to be displayed according to the network request return state and data. And before presenting the page to be displayed, all the pages that have been displayed in the current user interface are hidden, and then the status and data are presented to the page to be displayed according to the network request transparently transmitted to the UI layer, which ensures the accuracy of the page display Sex, reducing the chance of bugs.
[0059] figure 2 It is a network request processing device in an Android application provided by an embodiment of the present invention, such as figure 2 As shown, the device includes: an encapsulation sending module 201 and a delivery module 202; the encapsulation sending module 201 is used to encapsulate the network request return status and data into a message class, and send the message class to the main thread through the message processor; The transfer module 202 is configured to transfer the return status and data of the network request in the message class of the main thread to the user interface layer through the update interface interface.
[0060] Specifically, when processing the network request, first the package sending module 201 encapsulates the network request return status and data into the message class; then the package sending module 201 sends the message class to the host through the message processor handler in the Android system. In this way, the information returned by the network request can be obtained in the main thread. The transfer module 202 then transfers the information in the message class of the main thread to the user interface layer through the update interface interface (ie, the UpdateUI interface), so that the status and data update page can be returned according to the network request passed to the UI layer. And through the interface coupling, the coupling relationship between the network request and the UI can be reduced, and at the same time, all the page updates have the same logical interface, which is convenient for the maintenance of the later program.
[0061] In the embodiment of the present invention, the network request return status and data are encapsulated into a message class through the package sending module, and the message class is sent to the main thread through the message processor, so that the network request return can be obtained in the main thread. The transfer module then transfers the information in the message class of the main thread to the user interface layer through the update interface interface, so that the status and data update page can be returned according to the network request passed to the user interface layer, and the interface is coupled The coupling relationship between network requests and UI updates is reduced, and all page updates have the same logical interface, ensuring the unity of the updated user interface structure and facilitating the maintenance of the program later.
[0062] image 3 It is a structural block diagram of an optimized device for a gift panel in an Android application provided by an embodiment of the present invention, such as image 3 As shown, the device includes: a processor 301, a memory (memory) 302, and a bus 303;
[0063] Wherein, the processor 301 and the memory 302 communicate with each other through the bus 303;
[0064] The processor 301 is configured to call the program instructions in the memory 302 to execute the methods provided in the foregoing method embodiments, for example, including: encapsulating the return status and data of the network request into a message class, and using the message processor The message class is sent to the main thread; the network request return status and data in the message class of the main thread are transferred to the user interface layer through the update interface interface.
[0065] This embodiment provides a non-transitory computer-readable storage medium, the non-transitory computer-readable storage medium storing computer instructions that cause the computer to execute the methods provided in the foregoing method embodiments, for example, including : Encapsulate the return status and data of the network request into a message class, and send the message class to the main thread through the message processor; use the update interface interface to return the network request status and data transfer in the message class of the main thread To the user interface layer.
[0066] A person of ordinary skill in the art can understand that all or part of the steps in the above method embodiments can be implemented by a program instructing relevant hardware. The foregoing program can be stored in a computer readable storage medium. When the program is executed, the program is executed. Including the steps of the foregoing method embodiment; and the foregoing storage medium includes: ROM, RAM, magnetic disk, or optical disk and other media that can store program codes.
[0067] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: The technical solutions recorded in the foregoing embodiments are modified, or some of the technical features are equivalently replaced; these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

PUM

no PUM

Description & Claims & Application Information

We can also present the details of the Description, Claims and Application information to help users get a comprehensive understanding of the technical details of the patent, such as background art, summary of invention, brief description of drawings, description of embodiments, and other original content. On the other hand, users can also determine the specific scope of protection of the technology through the list of claims; as well as understand the changes in the life cycle of the technology with the presentation of the patent timeline. Login to view more.

Similar technology patents

PTC assembling machine

Owner:佛山市九一自动化科技有限公司

Cryopreservation and resuscitation method for ovarian tissue

ActiveCN108641999AGuaranteed uniformitygood cell membrane structure
Owner:阮祥燕

Adjustable garden landscape irrigator

InactiveCN108029518AGuaranteed uniformityconducive to common development
Owner:安徽瀚一规划设计院有限公司

Impregnation device and method of carbon fiber flexible felt prepreg

InactiveCN102513264AGuaranteed uniformityIncrease dipping speed
Owner:深圳市石金科技股份有限公司

Classification and recommendation of technical efficacy words

  • Guaranteed uniformity
  • easy maintenance

Welding device and technique of axle housing assembly Y-shaped weld joint

ActiveCN101362248AHigh control reliabilityEasy maintenance
Owner:JIANGXI JIANGLING CHASSIS CO LTD

Bionic machinery chelonian with two-stage freedom degree flipper mechanism

InactiveCN101016081AIncreased angular velocity and rotational rangeeasy maintenance
Owner:PEKING UNIV
Who we serve
  • R&D Engineer
  • R&D Manager
  • IP Professional
Why Eureka
  • Industry Leading Data Capabilities
  • Powerful AI technology
  • Patent DNA Extraction
Social media
Try Eureka
PatSnap group products