A kind of network request method, terminal installation and storage medium
Technical field
The present invention relates to field of computer technology more particularly to a kind of network request methods, terminal installation and storage medium.
Background technique
When progress website jumps or open a new page, the resource of load new page is generally required, is asked by network
Asking server to respond and returning to corresponding data can be achieved with.Specifically, multiple network request objects are needed, concurrent request is corresponding
Resource, unified readjustment after being responded.But request when, some request objects can malfunction, will affect so entirely request at
Function.
Currently, common request successfully quantity using variable label, or marks each to request object, request error is being known
Afterwards, network request can be retransmitted.It is not only complicated for operation to manage concurrent multithreading request in this way, but also asking
It needs to retransmit after finding out mistake, efficiency is lower, it is difficult to the management for being simple and efficient network request.
Summary of the invention
In view of this, the embodiment of the invention provides a kind of network request method, terminal installation and storage medium, to solve
Existing requesting method problem complicated for operation.
The embodiment of the present invention in a first aspect, provide a kind of network request method, including:
Network request object is generated, and the request object is stored in predefined request array;
By system GCD technology, a task groups are created, the task groups, concurrent hauling is added in the request array
Network request;
After receiving server-side response message, the response message is stored in predefined response array;
The request object for sending failure in the response array is obtained, the request object pair for sending failure is retransmitted
The network request object answered.
The second aspect of the embodiment of the present invention provides a kind of network request terminal installation, including:
Generation module:Predefined request array is stored in for generating network request object, and by the request object;
Request module:For creating a task groups by system GCD technology, described appoint is added in the request array
Business group, concurrently plays network request;
Memory module:After receiving server-side response message, the response message is stored in predefined number of responses
In group;
Retransmit module:For obtaining the request object for sending failure in the response array, retransmits the transmission and lose
The corresponding network request object of the request object lost.
The third aspect of the embodiment of the present invention, provides a kind of terminal installation, including memory, processor and is stored in
In the memory and the computer program that can run on the processor, when the processor executes the computer program
It realizes such as the step of first aspect of embodiment of the present invention the method.
The fourth aspect of the embodiment of the present invention, provides a kind of computer readable storage medium, described computer-readable to deposit
Storage media is stored with computer program, realizes that first aspect of the embodiment of the present invention mentions when the computer program is executed by processor
The step of the method for confession.
5th aspect of the embodiment of the present invention, provides a kind of computer program product, the computer program product packet
Computer program is included, realizes that first aspect of the embodiment of the present invention mentions when the computer program is executed by one or more processors
The step of the method for confession.
In the embodiment of the present invention, the request object that one is requested is put into same array, is asked by GCD technology unification management
It asks, meanwhile, it malfunctions in network request and adjusts back the retransmission of corresponding requests object.It is requested based on GCD parallel processing network, it is not only non-
Normal efficient handling requests, moreover, can adjust back retransmission when requesting object error for the request object, allow to
The problem of simple and quick processing network request malfunctions.
Detailed description of the invention
It to describe the technical solutions in the embodiments of the present invention more clearly, below will be to embodiment or description of the prior art
Needed in attached drawing be briefly described, it should be apparent that, the accompanying drawings in the following description is only of the invention some
Embodiment for those of ordinary skill in the art without any creative labor, can also be according to these
Attached drawing obtains other attached drawings.
Fig. 1 is one embodiment flow chart of network request method provided in an embodiment of the present invention;
Fig. 2 is another embodiment flow chart of network request method provided in an embodiment of the present invention;
Fig. 3 is the structural schematic diagram of network request terminal installation provided in an embodiment of the present invention;
Fig. 4 is the structural schematic diagram of terminal installation provided in an embodiment of the present invention.
Specific embodiment
The embodiment of the invention provides a kind of network request method, terminal installation and storage mediums, for efficiently carrying out simultaneously
Row network request, and quickly processing network request error problem.
In order to make the invention's purpose, features and advantages of the invention more obvious and easy to understand, below in conjunction with the present invention
Attached drawing in embodiment, technical scheme in the embodiment of the invention is clearly and completely described, it is clear that disclosed below
Embodiment be only a part of the embodiment of the present invention, and not all embodiment.Based on the embodiments of the present invention, this field
Those of ordinary skill's all other embodiment obtained without making creative work, belongs to protection of the present invention
Range.
Embodiment one:
Referring to Fig. 1, the flow diagram of network performance parameter acquisition method provided in an embodiment of the present invention, including it is following
Step:
S101, network request object is generated, and the request object is stored in predefined request array;
The network request object is the example of network request class, each network request object, which corresponds to a network, asks
It asks, such as passage, a picture etc..It include request header and requesting method in the network request object.
The predefined request array is system array, and the storage request object in the request array is initiating net
It is whole initiation request with array when network is requested.The request array may include the network request object of preset quantity, can also
With the network request object comprising same request type, it is not limited here.
Optionally, network request class is created, defines essential attribute and request sending method for the class, wherein the base
This attribute includes requesting method, request path, required parameter and call back function.When initiating network request, request road is first determined
Diameter after generating request object, creates session object, then request to create task, and the method in call request task sends network and asks
It asks, then by callback method, result is passed back.
Optionally, the label of request object is set in the request array.For example, passing through the variable of a bool type
Whether label request succeeds.
S102, pass through system GCD technology, create a task groups, the task groups are added in the request array, concurrently
Play network request;
The GCD technology is mainly used for optimization application to support multi-core processor and other symmetric multi-processors systems
System.The technology is to allow an application program according to available resource, arranges each process or thread in any available processing core
Upper paralled queue executes particular task.In embodiments of the present invention, it initiates and receives, Neng Gouguan by GCD technology scheduling request
Lineation journey, scheduler task and destroying threads, the efficient process of Logistics networks request.
The task groups are parallel queue, are a local variables, usually create a dispatch_group_t class
The group variable of type indicates, when network request is added in the parallel queue, so that it may carry out network request parallel, and can be with
Listen to the task performance of per thread.
After initiating network request and receiving server-side feedback data, current request task is completed, and can exit group.
Optionally, above-mentioned to further include before:
Creation response array, is stored in the response array for the request object.
Optionally, after receiving server-side response message, the response message covers the request object.
S103, after receiving server-side response message, the response message is stored in predefined response array;
The response message is by network request object, and the request data requested can be fed back when requesting successfully
The data content of request includes error message in response message when requesting to fail.
The response array is for storing response message, specifically, ringing after network request object receives server-side response
It answers information that can be called back, is put into request object, i.e., can include response letter in network request object after initiation network request
Breath.
Preferably, when creating the response array, network request object will be generated and be put into the response array, when connecing
Response message is received, the network request object after response is put into the response array, covers original network request object.
S104, the request object for sending failure in the response array is obtained, retransmits the request for sending failure
The corresponding network request object of object.
After all-network request object exits task groups group, readjustment is dished out method, in the response array of dishing out
Response message can determine the network request object that request is abnormal or malfunctions according to the response message.
The request object for sending failure is that former request object issues network request, and receives server response letter
Breath, the response message is normally contained in former request object, when including wrong in response message, requests object according to original
The position in array, the network request object before can determining its corresponding request.
Optionally, after the request object exits the task groups group, judge response message with the presence or absence of mistake mark
Know, when error identification is not present, mark the response message, when there are error identification, spreads out of the error identification
The label of corresponding request object.
Above-mentioned steps by GCD technology concurrent processing network request, and manage the progress of each request, can ensure
The stabilization of multi-request, meanwhile, it can quickly handle network request error.
Embodiment two:
On the basis of Fig. 1, by taking program coding as an example, the specific implementation process of processing network request is described in detail in conjunction with Fig. 2, such as
Under:
Network request object can be generated by system API, in embodiments of the present invention, can also pass through customized net
Network request class, come generate to as.In S201, the class DYRequest of a network request object is created, and defines basic belong to
Property:Requesting method method, request path url, required parameter params and call back function completion, and define a net
Network requests sending method send.
In the send method, NSMutableURLRequest is created using above-mentioned essential attribute, then create
NSURLSession ', the method for using ' NSURLSession ' create a NSURLSessionTask, recall
The method of NSURLSessionTask sends network request, calls completion attribute to pass result back in the readjustment of system
Into DYRequest.
The encapsulation of above-mentioned completion network request instantiation is passed to required parameter, energy by network request class after instantiation
Carry out network request.
The array NSArray that expansion system provides in S202 creates the classification of a system array, specifically for the classification
The readjustment RequestAsyncBlock of a Block type is defined, enters ginseng for a BOOL type isSucces label and one
Array responseArray.
After network request object is put into request array, request can be put into RequestAsyncBlock by classifying
In, network request is carried out, and each request can be marked using isSucces, the request convenient for management per thread in this way.
For above-mentioned network request class, the readjustment of a Block type RequestSectionBlock is defined, and is defined
The attribute sectionCompletion of RequestSectionBlock type, in the variable for creating an int type for marking
Note request object position in array.
System GCD technology is used in S203, creates the local variable group of a dispatch_group_t type,
And array index will be taken out after network request object traversal, group is added, network is initiated by the sending method in request array
Request, each network request object make requests transmission by the send method of itself.
It further include that will request all-network request object deposit response array in array before initiating request
ResponseArray can override the network request pair in responseArray array automatically after receiving server-side response
As.
After all-network request all obtains server-side response, response array can be traversed and judge whether there is error, when not depositing
When, can mark all-network request object.When the network request that there is mistake, then the request object of the mistake is got in number
Number in group is retransmitted after determining the request object, it is preferred that further includes according to error message, modification request ginseng
Number, investigation error reason etc..
In S205, the response message for obtaining mistake is after group is exited in all-network request, and readjustment will
IsSuccess and responseArray is passed in RequestAsyncBlock, so that it may go to obtain corresponding false request pair
As.
It is above-mentioned that network request is configured to by array according to literal, it calls in array
SendAsAsyncCompletion method initiates request, saves the readjustment of RequestAsyncBlock, and it is laggard to create group
Enter group calls RequestSectionBlock to exit group after server response, and spreads out of false request object to hair
Play request position.
Embodiment three:
A kind of network request method is essentially described above, a kind of terminal installation of network request will be carried out below detailed
Description.
Fig. 3 shows the structural schematic diagram of the terminal installation of network request, including:
Generation module 310:Predefined number of request is stored in for generating network request object, and by the request object
Group;
Optionally, the generation module 310 includes:
Creating unit:For creating network request class, essential attribute and request sending method are defined for the class, wherein
The essential attribute includes requesting method, request path, required parameter and call back function.
Request module 320:It, will be described in request array addition for creating a task groups by system GCD technology
Task groups concurrently play network request;
The GCD technology is mainly used for optimization application to support multi-core processor and other symmetric multi-processors systems
System.The technology is to allow an application program according to available resource, arranges each process or thread in any available processing core
Upper paralled queue executes particular task.In embodiments of the present invention, it initiates and receives, Neng Gouguan by GCD technology scheduling request
Lineation journey, scheduler task and destroying threads, the efficient process of Logistics networks request.
The task groups are parallel queue, when network request is added in the parallel queue, so that it may carry out net parallel
Network request, and the task performance of per thread can be listened to.Specifically, the task groups are dispatch_group_t
The local variable of type.
Memory module 330:After receiving server-side response message, the response message is stored in predefined response
In array;
The response message is by network request object, and the request data requested can be fed back when requesting successfully
The data content of request includes error message in response message when requesting to fail.
The response array is for storing response message, specifically, ringing after network request object receives server-side response
It answers information that can be called back, is put into request object, i.e., can include response letter in network request object after initiation network request
Breath.
Optionally, it is described receive server-side response message after, the response message is stored in predefined response array
In before further include:
Creation response array, is stored in the response array for the request object.
Optionally, the creation responds array, and the request object is stored in the response array and further includes:
After receiving server-side response message, the response message covers the request object.
Retransmit module 340:For obtaining the request object for sending failure in the response array, the transmission is retransmitted
The corresponding network request object of request object of failure.
Optionally, the repeating transmission module 340 includes:
Judging unit:For after the request object exits the task groups, judging response message with the presence or absence of mistake
Mark marks the response message when error identification is not present, and when there are error identification, spreads out of the mistake mark
Know the label of corresponding request object.
The transmission for managing network request in above-mentioned terminal installation by sending module has memory module and retransmits module reception
Response data simultaneously handles error message, can effectively manage the progress of network request.
Example IV:
Fig. 4 is the schematic diagram for the network request terminal installation structure that one embodiment of the invention provides.The terminal installation is
Have the mobile computer device of touch screen, including but not limited to smart phone, smartwatch, notebook, tablet computer, POS
Machine includes even vehicle-mounted computer.As shown in figure 4, the server 4 of the embodiment includes:Memory 410, processor 420 and it is
System bus 430, the memory 410 include the program 4101 run of storage thereon, it will be understood by those skilled in the art that
Server architecture shown in Fig. 4 does not constitute the restriction to server, may include components more more or fewer than diagram, or
Person combines certain components or different component layouts.
It is specifically introduced below with reference to each component parts of the Fig. 4 to terminal installation:
Memory 410 can be used for storing software program and module, and processor 420 is stored in memory 410 by operation
Software program and module, thereby executing the various function application and data processing of terminal.Memory 410 can mainly include
Storing program area and storage data area, wherein storing program area can application journey needed for storage program area, at least one function
Sequence (such as sound-playing function, image player function etc.) etc.;Storage data area can be stored to be created according to using for server
Data (such as audio data, phone directory etc.) etc..In addition, memory 410 may include high-speed random access memory, may be used also
To include nonvolatile memory, for example, at least a disk memory, flush memory device or other volatile solid-states
Part.
Program 4101 is run comprising network request method on memory 410, and the program 4101 that runs can be with
It is divided into one or more module/units, one or more of module/units are stored in the memory 410, and
It is executed by processor 420, with the transmitting of completion notice and obtains notice realization process, one or more of module/units can
To be the series of computation machine program instruction section that can complete specific function, the instruction segment is for describing the computer program
4101 implementation procedure in the server 4.For example, the computer program 4101 can be divided into generation module, ask
Modulus block, memory module and repeating transmission module.
Processor 420 is the control centre of server, utilizes each of various interfaces and the entire terminal device of connection
Part by running or execute the software program and/or module that are stored in memory 410, and calls and is stored in memory
Data in 410 execute the various functions and processing data of terminal, to carry out integral monitoring to terminal.Optionally, processor
420 may include one or more processing units;Preferably, processor 420 can integrate application processor and modem processor,
Wherein, the main processing operation system of application processor, application program etc., modem processor mainly handles wireless communication.It can
With understanding, above-mentioned modem processor can not also be integrated into processor 420.
System bus 430 is for connection to each functional component of computer-internal, can with data information, address information,
Information is controlled, type can be such as pci bus, isa bus, VESA bus.The instruction of processor 420 is passed by bus
It is handed to memory 410,410 feedback data of memory is responsible for processor 420 and memory to processor 420, system bus 430
Data, instruction interaction between 410.Certain system bus 430 can also access other equipment, such as network interface, display are set
It is standby etc..
The terminal installation should include at least network interface card, output equipment etc., and details are not described herein for other component parts.
In embodiments of the present invention, the program that runs of the execution of terminal installation 420 included by the server is specially:
A kind of network request method, including:
Network request object is generated, and the request object is stored in predefined request array;
By system GCD technology, a task groups are created, the task groups, concurrent hauling is added in the request array
Network request;
After receiving server-side response message, the response message is stored in predefined response array;
The request object for sending failure in the response array is obtained, the request object pair for sending failure is retransmitted
The network request object answered.
Further, further include before the generation network request object:
Network request class is created, defines essential attribute and request sending method for the class, wherein the essential attribute packet
Include requesting method, request path, required parameter and call back function.
Further, it is described receive server-side response message after, the response message is stored in predefined number of responses
Further include before in group:
Creation response array, is stored in the response array for the request object.
Further, the creation responds array, and the request object is stored in the response array and further includes:
After receiving server-side response message, the response message covers the request object.
Further, described obtain sends the request object of failure in the response array and is specially:
After the request object exits the task groups, response message is judged with the presence or absence of error identification, when there is no mistakes
When accidentally identifying, the response message is marked, when there are error identification, spreads out of the corresponding request object of the error identification
Label.
It is apparent to those skilled in the art that for convenience and simplicity of description, the system of foregoing description,
The specific work process of device and unit, can refer to corresponding processes in the foregoing method embodiment, and details are not described herein.
In the above-described embodiments, it all emphasizes particularly on different fields to the description of each embodiment, is not described in detail or remembers in some embodiment
The part of load may refer to the associated description of other embodiments.
It is apparent to those skilled in the art that for convenience and simplicity of description, the system of foregoing description,
The specific work process of device and unit, can refer to corresponding processes in the foregoing method embodiment, and details are not described herein.
In the above-described embodiments, it all emphasizes particularly on different fields to the description of each embodiment, is not described in detail or remembers in some embodiment
The part of load may refer to the associated description of other embodiments.
The above, the above embodiments are merely illustrative of the technical solutions of the present invention, rather than its limitations;Although referring to before
Stating embodiment, invention is explained in detail, those skilled in the art should understand that:It still can be to preceding
Technical solution documented by each embodiment is stated to modify or equivalent replacement of some of the technical features;And these
It modifies or replaces, the spirit and scope for technical solution of various embodiments of the present invention that it does not separate the essence of the corresponding technical solution.