Detailed Description
The embodiment of the invention provides a network request method, a terminal device and a storage medium, which are used for efficiently carrying out parallel network requests and quickly processing the problem of errors of the network requests.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The first embodiment is as follows:
referring to fig. 1, a schematic flow chart of a network performance parameter acquisition method according to an embodiment of the present invention includes the following steps:
s101, generating a network request object, and storing the request object into a predefined request array;
the network request objects are examples of network request classes, and each network request object corresponds to a network request, such as a segment of text, a picture, and the like. The network request object comprises a request header and a request method.
The predefined request array is a system array, the request object is stored in the request array, and when the network request is initiated, the request is initiated by taking the array as a whole. The request array may include a preset number of network request objects, and may also include network request objects of the same request type, which is not limited herein.
Optionally, a network request class is created, and a basic attribute and a request sending method are defined for the class, where the basic attribute includes a request method, a request path, a request parameter, and a callback function. When a network request is initiated, a request path is determined, a session object is created after a request object is generated, a request task is created, a method in the request task is called, the network request is sent, and a result is returned through a callback method.
Optionally, a flag of a request object is set in the request array. For example, whether the request was successful is marked by a bol type variable.
S102, creating a task group through a system GCD technology, adding the request array into the task group, and initiating a network request;
the GCD technique is primarily used to optimize applications to support multi-core processors and other symmetric multiprocessing systems. The technique is for an application to schedule processes or threads to queue in parallel on any available processing core to perform a particular task, depending on the available resources. In the embodiment of the invention, the GCD technology is used for scheduling the initiation and the reception of the request, so that the thread, the scheduling task and the destruction of the thread can be managed, and the high-efficiency processing of the network request is ensured.
The task group is a parallel queue and is a local variable, a dispatch _ group _ t type group variable representation is usually created, when a network request is added into the parallel queue, the network request can be performed in parallel, and the task completion condition of each thread can be monitored.
After a network request is initiated and server feedback data is received, the current request task is completed, and the group exits.
Optionally, the method further includes:
and creating a response array, and storing the request object into the response array.
Optionally, after receiving the response information of the server, the response information covers the request object.
S103, after receiving the response information of the server, storing the response information into a predefined response array;
the response information is request data obtained by requesting through a network request object, when the request is successful, the data content of the request can be fed back, and when the request is failed, the response information contains error information.
The response array is used for storing response information, specifically, after the network request object receives the server response, the response information is recalled and put into the request object, that is, after the network request object initiates the network request, the network request object includes the response information.
Preferably, when the response array is created, the generated network request object is placed in the response array, and when response information is received, the responded network request object is placed in the response array to overwrite the original network request object.
S104, acquiring the request object which fails to be sent in the response array, and resending the network request object corresponding to the request object which fails to be sent.
And when all the network request objects exit the task group, calling back the throwing method, throwing the response information in the response array, and determining the network request object with abnormal or error request according to the response information.
The request object which fails to send sends a network request for the original request object, and receives the server response information, wherein the response information is generally contained in the original request object, and when the response information contains errors, the network request object before the corresponding request can be determined according to the position of the original request object in the array.
Optionally, after the request object exits the task group, it is determined whether an error identifier exists in response information, and when the error identifier does not exist, the response information is marked, and when the error identifier exists, a label of the request object corresponding to the error identifier is transmitted.
The step is that the GCD technology is used for concurrently processing the network requests and managing the progress of each request, so that the stability of multiple requests can be guaranteed, and meanwhile, the error of the network request can be quickly processed.
Example two:
on the basis of fig. 1, taking program code as an example, a detailed implementation process for processing a network request is described in detail with reference to fig. 2, as follows:
the network request object can be generated through a system API, and in the embodiment of the invention, the object can also be generated through a customized network request class. In S201, a DYRequest class of the network request object is created, and basic attributes are defined: a request method, a request path url, a request parameter params and a callback function, and defines a network request sending method send.
In the send method, using the basic attribute to create NSMutableURLRquest, then creating NSURLSession ', using the ' NSURLSession ' method to create an NSURLSessionTask, then calling the NSURLSessionTask method to send a network request, calling the completion attribute in the callback of the system to transmit the result back to the DYRequest.
The encapsulation for completing the instantiation of the network request can carry out the network request by inputting the request parameters after the instantiation of the network request class.
In S202, the array NSArray provided by the system is extended, a classification of the system array is specifically created, a call-back RequestAsyncBlock of a Block type is defined for the classification, and a BOOL type issuccs tag and an array responseArray are included.
After the network request object is placed into the request array, the requests can be placed into the requestasyncBlock by classification to carry out network requests, and each request uses an issuccs mark, so that the request of each thread can be managed conveniently.
Defining a callback of a Block type RequestSectionBlock and defining the attribute section completion of the RequestSectionBlock for the network request class, and creating a variable of an int type for marking the position of a request object in a group.
In S203, a local variable group of dispatch _ group _ t type is created using a system GCD technique, and the network request object is traversed, and then the array subscript is taken out, added to the group, and a network request is initiated by a sending method in the request array, and each network request object makes a request sending by its own send method.
Before initiating the request, the method also comprises the step of storing all network request objects in the request array into a response array responseArray, and automatically overriding the network request objects in the response array after receiving the response of the server.
After all network requests are responded by the server, the response array is traversed to judge whether error exists, and if not, all network request objects are marked. When an error network request exists, acquiring the number of the error request object in the array, and retransmitting the request object after determining the request object, preferably, modifying the request parameter, checking the error reason and the like according to the error information.
In S205, the response message for obtaining the error is that after all network requests exit from the group, the callback transmits isSuccess and responseArray to the RequestAsyncBlock, so as to obtain the corresponding error request object.
The network request is constructed into an array according to the word size, a sendAsyncCompletion method in the array is called to initiate the request, the callback of RequestAsyncBlock is saved, a group is created and then enters the group, and after the server responds, a RequestSectionBlock is called to exit the group and an error request object is transmitted to the position of the initiation request.
Example three:
the above mainly describes a network request method, and a network requested terminal device will be described in detail below.
Fig. 3 shows a schematic structural diagram of a network-requested terminal device, including:
the generation module 310: the system comprises a request array, a network request object generation module, a network request module and a network management module, wherein the request array is used for generating a network request object and storing the request object into a predefined request array;
optionally, the generating module 310 includes:
a creation unit: the method is used for creating a network request class, and defining basic attributes and a request sending method for the class, wherein the basic attributes comprise a request method, a request path, request parameters and a callback function.
The request module 320: the system is used for creating a task group through a system GCD technology, adding the request array into the task group and initiating a network request;
the GCD technique is primarily used to optimize applications to support multi-core processors and other symmetric multiprocessing systems. The technique is for an application to schedule processes or threads to queue in parallel on any available processing core to perform a particular task, depending on the available resources. In the embodiment of the invention, the GCD technology is used for scheduling the initiation and the reception of the request, so that the thread, the scheduling task and the destruction of the thread can be managed, and the high-efficiency processing of the network request is ensured.
The task group is a parallel queue, when the network request is added into the parallel queue, the network request can be carried out in parallel, and the task completion condition of each thread can be monitored. Specifically, the task group is a local variable of dispatch _ group _ t type.
The storage module 330: the server is used for storing the response information into a predefined response array after receiving the response information of the server;
the response information is request data obtained by requesting through a network request object, when the request is successful, the data content of the request can be fed back, and when the request is failed, the response information contains error information.
The response array is used for storing response information, specifically, after the network request object receives the server response, the response information is recalled and put into the request object, that is, after the network request object initiates the network request, the network request object includes the response information.
Optionally, after receiving the response information of the server, before storing the response information into a predefined response array, the method further includes:
and creating a response array, and storing the request object into the response array.
Optionally, the creating a response array, and storing the request object in the response array further includes:
and after receiving response information of the server, covering the request object by the response information.
The retransmission module 340: and the request object which is failed to send in the response array is obtained, and the network request object corresponding to the request object which is failed to send is retransmitted.
Optionally, the retransmission module 340 includes:
a judging unit: and the label is used for judging whether response information has an error identifier or not after the request object exits the task group, marking the response information when the error identifier does not exist, and transmitting the label of the request object corresponding to the error identifier when the error identifier exists.
The terminal device manages the transmission of the network request through the transmission module, and the storage module and the retransmission module receive the response data and process the error information, so that the network request can be effectively managed.
Example four:
fig. 4 is a schematic diagram of a network request terminal device according to an embodiment of the present invention. The terminal device is a mobile computer device with a touch screen, including but not limited to a smart phone, a smart watch, a notebook, a tablet computer, a POS machine, and even a vehicle-mounted computer. As shown in fig. 4, the server 4 of this embodiment includes: a memory 410, a processor 420, and a system bus 430, the memory 410 including an executable program 4101 stored thereon, it being understood by those skilled in the art that the server architecture shown in FIG. 4 does not constitute a limitation of a server and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The following specifically describes each constituent component of the terminal apparatus with reference to fig. 4:
the memory 410 may be used to store software programs and modules, and the processor 420 performs various functional applications of the terminal and data processing by operating the software programs and modules stored in the memory 410. The memory 410 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the server, and the like. Further, the memory 410 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
An executable program 4101 of a network request method is contained on the memory 410, the executable program 4101 may be divided into one or more modules/units, which are stored in the memory 410 and executed by the processor 420 to complete the delivery of the notification and obtain the notification implementation process, and the one or more modules/units may be a series of computer program instruction segments capable of performing specific functions, which are used for describing the execution process of the computer program 4101 in the server 4. For example, the computer program 4101 may be divided into a generation module, a request module, a storage module and a retransmission module.
The processor 420 is a control center of the server, connects various parts of the entire terminal device using various interfaces and lines, performs various functions of the terminal and processes data by operating or executing software programs and/or modules stored in the memory 410 and calling data stored in the memory 410, thereby integrally monitoring the terminal. Alternatively, processor 420 may include one or more processing units; preferably, the processor 420 may integrate an application processor, which mainly handles operating systems, application programs, etc., and a modem processor, which mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated into processor 420.
The system bus 430 is used to connect functional units inside the computer, and can transmit data information, address information, and control information, and may be, for example, a PCI bus, an ISA bus, a VESA bus, etc. The instructions of the processor 420 are transmitted to the memory 410 through the bus, the memory 410 feeds data back to the processor 420, and the system bus 430 is responsible for data and instruction interaction between the processor 420 and the memory 410. Of course, the system bus 430 may also access other devices such as network interfaces, display devices, and the like.
The terminal device at least comprises a network card, an output device and the like, and other components are not described in detail herein.
In this embodiment of the present invention, the executable program executed by the terminal device 420 included in the server specifically includes:
a network request method, comprising:
generating a network request object and storing the request object into a predefined request array;
creating a task group through a system GCD technology, adding the request array into the task group, and initiating a network request;
after receiving the response information of the server, storing the response information into a predefined response array;
and acquiring the request object which fails to be sent in the response array, and resending the network request object corresponding to the request object which fails to be sent.
Further, the generating the network request object further comprises:
the method comprises the steps of creating a network request class, defining basic attributes and a request sending method for the class, wherein the basic attributes comprise a request method, a request path, request parameters and a callback function.
Further, after receiving the response information of the server, before storing the response information in a predefined response array, the method further includes:
and creating a response array, and storing the request object into the response array.
Further, the creating a response array, and storing the request object in the response array further includes:
and after receiving response information of the server, covering the request object by the response information.
Further, the obtaining of the request object that fails to be sent in the response array specifically includes:
and after the request object exits the task group, judging whether response information has an error identification, if not, marking the response information, and if so, transmitting a label of the request object corresponding to the error identification.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.