CN105681394A - Network request method and device based on OKHttp and GSON - Google Patents

Network request method and device based on OKHttp and GSON Download PDF

Info

Publication number
CN105681394A
CN105681394A CN201511019544.3A CN201511019544A CN105681394A CN 105681394 A CN105681394 A CN 105681394A CN 201511019544 A CN201511019544 A CN 201511019544A CN 105681394 A CN105681394 A CN 105681394A
Authority
CN
China
Prior art keywords
readjustment
okhttp
gson
network request
network
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Withdrawn
Application number
CN201511019544.3A
Other languages
Chinese (zh)
Inventor
代杰
邹仁平
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen Zhiying Technology Co ltd
Original Assignee
SHENZHEN RUIMING UNLIMITED TECHNOLOGY Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by SHENZHEN RUIMING UNLIMITED TECHNOLOGY Co Ltd filed Critical SHENZHEN RUIMING UNLIMITED TECHNOLOGY Co Ltd
Priority to CN201511019544.3A priority Critical patent/CN105681394A/en
Publication of CN105681394A publication Critical patent/CN105681394A/en
Withdrawn legal-status Critical Current

Links

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/56Provisioning of proxy services
    • H04L67/565Conversion or adaptation of application format or content
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/02Protocols based on web technology, e.g. hypertext transfer protocol [HTTP]
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/34Network arrangements or protocols for supporting network services or applications involving the movement of software or configuration parameters 
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/56Provisioning of proxy services
    • H04L67/565Conversion or adaptation of application format or content
    • H04L67/5651Reducing the amount or size of exchanged application data
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/50Network services
    • H04L67/60Scheduling or organising the servicing of application requests, e.g. requests for application data transmissions using the analysis and optimisation of the required network resources

Landscapes

  • Engineering & Computer Science (AREA)
  • Computer Networks & Wireless Communication (AREA)
  • Signal Processing (AREA)
  • Telephonic Communication Services (AREA)

Abstract

The invention discloses a network request method and device based on OKHttp and GSON. The method comprises the steps of receiving a network request command; requesting a server by the network request command through the OKHttp, and processing contents in a JSON format returned by the server through the GSON, and obtaining corresponding business data in a page callback according to the processing result. By adopting the network request method and device based on the OKHttp and GSON disclosed by the invention, a network request process is data modular, and network abnormality processing is unified; and network abnormality, logic abnormality and other errors are separated to reduce the coupling property between the callbacks and ensure a clear and feasible network request process.

Description

Based on network request method and the device of OKHttp and GSON
Technical field
The present invention relates to network request field, particularly relate to a kind of network request method based on OKHttp and GSON and device.
Background technology
In the networking request process of prior art, it is generally in non-UI thread, adopts android to carry network request method, according to the data of different functions and parsing, process data and refresh UI in network request processing module. This process is complicated, is not easy to expansion and operation.
Summary of the invention
The main purpose of the present invention is for providing a kind for the treatment of processes simply based on network request method and the device of OKHttp and GSON.
In order to realize foregoing invention object, the present invention proposes a kind of network request method based on OKHttp and GSON, comprising:
Receive network request order;
OKHttp request server is passed through in network request order, the content of the JSON form returned by GSON processing server;
According to result, in the page is adjusted back, obtain corresponding business datum.
Further, described according to result, in the page is adjusted back, obtain the step of corresponding business datum, comprising:
Result is encapsulated into default basic data model; Wherein, described basic data model is the data model that the data model of other network processes is inherited;
Encapsulated content according to basic data model, obtains corresponding business datum in the page is adjusted back.
Further, the described step obtaining corresponding business datum in the page is adjusted back, comprising:
Preset the readjustment of multiple mistake type, call corresponding mistake readjustment according to result.
Further, described mistake readjustment, comprising:
The abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level.
The present invention also provides a kind of network request device based on OKHttp and GSON, comprising:
Receive unit, for receiving network request order;
Processing unit, for passing through OKHttp request server by network request order, the content of the JSON form returned by GSON processing server;
Readjustment unit, for according to result, obtaining corresponding business datum in the page is adjusted back.
Further, described readjustment unit, comprising:
Package module, for being encapsulated into default basic data model by result; Wherein, described basic data model is the data model that the data model of other network processes is inherited;
Acquisition module, according to the encapsulated content of basic data model, obtains corresponding business datum in the page is adjusted back.
Further, described acquisition module, comprising:
Call submodule block, for presetting the readjustment of multiple mistake type, call corresponding mistake readjustment according to result.
Further, described mistake readjustment, comprising:
The abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level.
The network request method based on OKHttp and GSON of the present invention and device, by OKHttp request server, the content of the JSON form returned by GSON processing server, it is not necessary to pay close attention to resolving, make network request treating processes more simple.
Accompanying drawing explanation
Fig. 1 is the schematic flow sheet of the network request method based on OKHttp and GSON of one embodiment of the invention;
Fig. 2 be one embodiment of the invention according to result, in the page is adjusted back, obtain the schematic flow sheet of corresponding business datum step;
Fig. 3 is the structure schematic block diagram of the network request device based on OKHttp and GSON of one embodiment of the invention;
Fig. 4 is the structure schematic block diagram of the readjustment unit of one embodiment of the invention;
Fig. 5 is the structure schematic block diagram of the acquisition module of one embodiment of the invention.
The realization of the object of the invention, functional characteristics and advantage will in conjunction with the embodiments, are described further with reference to accompanying drawing.
Embodiment
It is to be understood that specific embodiment described herein is only in order to explain the present invention, it is not intended to limit the present invention.
With reference to Fig. 1, the embodiment of the present invention provides a kind of network request method based on OKHttp and GSON, comprises step:
S1, reception network request order;
S2, by network request order by OKHttp request server, the content of the JSON form returned by GSON processing server;
S3, according to result, the page adjust back in obtain corresponding business datum.
In the present embodiment, above-mentioned OKHttp is a efficient HTTP client terminal, same socket is shared in the link of the same address of Supporting connectivity, and (two programs on network realize the exchange of data by a two-way communication connection, this one end connected is called a socket), reduce response by connection pool to postpone, request buffer memory etc.; Above-mentioned JSON is JSON (JavaScriptObjectNotation) is the data exchange form of a kind of lightweight. By the combination of OKHttp and GSON, make network request treating processes more simple.
With reference to Fig. 2, in the present embodiment, above-mentioned according to result, in the page is adjusted back, obtain the step S3 of corresponding business datum, comprising:
S31, result is encapsulated into default basic data model; Wherein, described basic data model is the data model on the basis that the data model of other network processes is inherited;
S32, encapsulated content according to basic data model, obtain corresponding business datum in the page is adjusted back.
As described in above-mentioned steps S31 and step S32, the data model of each network processes is all inherited in basic data model, make the result of generation can be encapsulated in this basic data model, make the data structure of result identical, because result is different, so the difference such as data operation and data constraint, it is possible to do not need the problem being concerned about data parsing, directly in the page is adjusted back, obtain corresponding business datum, there is the feature of consistence height, extendability.
In the present embodiment, the above-mentioned encapsulated content according to basic data model, obtains corresponding business datum step S32 in the page is adjusted back, comprising:
S321, the readjustment presetting multiple mistake type, call corresponding mistake readjustment according to result.
As described in above-mentioned steps S321, in network request process, there will be two kinds and ask situation, one is asked successfully, obtains corresponding business datum, and another kind is asked unsuccessfully, and during how the reason of failure have, so a kind of readjustment can be set by unified for the failure cause of different types so that it is modularization, so in treating processes, by data modeling, network abnormality processing unitizes, and the coupling reduced between each readjustment falls, being that network request process is more simple, expansion is stronger. Above-mentioned mistake readjustment, it is possible to comprising: the abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level. Logic error readjustment is generally the mistake occurred when data parsing; The abnormal readjustment of operation level is generally functional makeing mistakes; The abnormal readjustment of network level is generally that suspension, network are smooth etc. makes mistakes.
In one embodiment; such as login mailbox; first input request order; i.e. login name and password; then by OKHttp, request command is sent to mailbox server; the content of the JSON form that mailbox server returns is processed by GSON; then result is encapsulated in basic data model; when result is for logining mailbox; then enter mailbox, if result is for logging in failure, then call corresponding mistake readjustment according to failure cause; such as because of network outage, then call the business datum etc. of the abnormal readjustment of network level.
The network request method based on OKHttp and GSON of the present invention, by OKHttp request server, the content of the JSON form returned by GSON processing server, and result is encapsulated in basic data model, then corresponding readjustment in multiple default readjustment type is called, making the process of network request have data modeling, network abnormality processing unitizes; In addition the mistakes such as network exception and logic exception are separated, reduce the coupling between each readjustment, make network request process clear and easy-to-use.
With reference to Fig. 3, the embodiment of the present invention also proposes a kind of network request device based on OKHttp and GSON, comprising:
Receive unit 10, for receiving network request order;
Processing unit 20, for passing through OKHttp request server by network request order, the content of the JSON form returned by GSON processing server;
Readjustment unit 30, for according to result, obtaining corresponding business datum in the page is adjusted back.
In the present embodiment, above-mentioned OKHttp is a efficient HTTP client terminal, same socket is shared in the link of the same address of Supporting connectivity, and (two programs on network realize the exchange of data by a two-way communication connection, this one end connected is called a socket), reduce response by connection pool to postpone, request buffer memory etc.; Above-mentioned JSON is JSON (JavaScriptObjectNotation) is the data exchange form of a kind of lightweight. By the combination of OKHttp and GSON, make network request treating processes more simple.
With reference to Fig. 4, in the present embodiment, above-mentioned readjustment unit 30, comprising:
Package module 31, for being encapsulated into default basic data model by result; Wherein, described basic data model is the data model that the data model of other network processes is inherited;
Acquisition module 32, according to the encapsulated content of basic data model, obtains corresponding business datum in the page is adjusted back.
Such as above-mentioned package module 31 and acquisition module 32, the data model of each network processes is all inherited in basic data model, make the result of generation can be encapsulated in this basic data model, make the data structure of result identical, because result is different, so the difference such as data operation and data constraint, it is possible to do not need the problem being concerned about data parsing, directly in the page is adjusted back, obtain corresponding business datum, there is the feature of consistence height, extendability.
With reference to Fig. 5, in the present embodiment, above-mentioned acquisition module 32, comprising:
Call submodule block 321, for presetting the readjustment of multiple mistake type, call corresponding mistake readjustment according to result. In the present embodiment, above-mentioned mistake readjustment generally comprises: the abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level. In network request process, there will be two kinds and ask situation, one is asked successfully, obtaining corresponding business datum, another kind is asked unsuccessfully, and during how the reason of failure have, so a kind of readjustment can be set by unified for the failure cause of different types, make its modularization, so in treating processes, by data modeling, network abnormality processing unitizes, the coupling reduced between each readjustment falls, and is that network request process is more simple, and expansion is stronger. Above-mentioned mistake readjustment, it is possible to comprising: the abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level. Logic error readjustment is generally the mistake occurred when data parsing; The abnormal readjustment of operation level is generally functional makeing mistakes; The abnormal readjustment of network level is generally that suspension, network are smooth etc. makes mistakes.
In one embodiment; such as login mailbox; first input request order; i.e. login name and password; then by OKHttp, request command is sent to mailbox server; the content of the JSON form that mailbox server returns is processed by GSON; then result is encapsulated in basic data model; when result is for logining mailbox; then enter mailbox, if result is for logging in failure, then call corresponding mistake readjustment according to failure cause; such as because of network outage, then call the business datum etc. of the abnormal readjustment of network level.
The network request device based on OKHttp and GSON of the present invention, by OKHttp request server, the content of the JSON form returned by GSON processing server, and result is encapsulated in basic data model, then corresponding readjustment in multiple default readjustment type is called, making the process of network request have data modeling, network abnormality processing unitizes; In addition the mistakes such as network exception and logic exception are separated, reduce the coupling between each readjustment, make network request process clear, easy-to-use.
The foregoing is only the preferred embodiments of the present invention; not thereby the patent scope of the present invention is limited; every utilize specification sheets of the present invention and accompanying drawing content to do equivalent structure or equivalence flow process conversion; or directly or indirectly it is used in other relevant technical fields, all it is included in the scope of patent protection of the present invention with reason.

Claims (8)

1. the network request method based on OKHttp and GSON, it is characterised in that, comprising:
Receive network request order;
OKHttp request server is passed through in network request order, the content of the JSON form returned by GSON processing server;
According to result, in the page is adjusted back, obtain corresponding business datum.
2. the network request method based on OKHttp and GSON according to claim 1, it is characterised in that, described according to result, in the page is adjusted back, obtain the step of corresponding business datum, comprising:
Result is encapsulated into default basic data model; Wherein, described basic data model is the data model that the data model of other network processes is inherited;
Encapsulated content according to basic data model, obtains corresponding business datum in the page is adjusted back.
3. the network request method based on OKHttp and GSON according to claim 2, it is characterised in that, the described step obtaining corresponding business datum in the page is adjusted back, comprising:
Preset the readjustment of multiple mistake type, call corresponding mistake readjustment according to result.
4. the network request method based on OKHttp and GSON according to claim 3, it is characterised in that, described mistake readjustment, comprising:
The abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level.
5. the network request device based on OKHttp and GSON, it is characterised in that, comprising:
Receive unit, for receiving network request order;
Processing unit, for passing through OKHttp request server by network request order, the content of the JSON form returned by GSON processing server;
Readjustment unit, for according to result, obtaining corresponding business datum in the page is adjusted back.
6. the network request device based on OKHttp and GSON according to claim 5, it is characterised in that, described readjustment unit, comprising:
Package module, for being encapsulated into default basic data model by result; Wherein, described basic data model is the data model that the data model of other network processes is inherited;
Acquisition module, according to the encapsulated content of basic data model, obtains corresponding business datum in the page is adjusted back.
7. the network request device based on OKHttp and GSON according to claim 6, it is characterised in that, described acquisition module, comprising:
Call submodule block, for presetting the readjustment of multiple mistake type, call corresponding mistake readjustment according to result.
8. the network request device based on OKHttp and GSON according to claim 7, it is characterised in that, described mistake readjustment, comprising:
The abnormal readjustment of logic error readjustment, operation level and the abnormal readjustment of network level.
CN201511019544.3A 2015-12-29 2015-12-29 Network request method and device based on OKHttp and GSON Withdrawn CN105681394A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201511019544.3A CN105681394A (en) 2015-12-29 2015-12-29 Network request method and device based on OKHttp and GSON

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201511019544.3A CN105681394A (en) 2015-12-29 2015-12-29 Network request method and device based on OKHttp and GSON

Publications (1)

Publication Number Publication Date
CN105681394A true CN105681394A (en) 2016-06-15

Family

ID=56189762

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201511019544.3A Withdrawn CN105681394A (en) 2015-12-29 2015-12-29 Network request method and device based on OKHttp and GSON

Country Status (1)

Country Link
CN (1) CN105681394A (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107748660A (en) * 2017-10-30 2018-03-02 四川长虹技佳精工有限公司 A kind of implementation method of self-defined compatible network request framework
CN108040127A (en) * 2018-01-03 2018-05-15 南京工程学院 A kind of multiterminal data interaction based on location-based service Articles found, owner please contact platform
CN108874833A (en) * 2017-05-15 2018-11-23 武汉斗鱼网络科技有限公司 A kind of method and device for realizing Etcd Java client
CN110677303A (en) * 2019-10-10 2020-01-10 荆楚理工学院 Network management system
WO2024198192A1 (en) * 2023-03-31 2024-10-03 百度国际科技(深圳)有限公司 Client communication network, network request processing method, communication system, electronic device, and medium

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8515999B2 (en) * 2011-09-29 2013-08-20 International Business Machines Corporation Method and system providing document semantic validation and reporting of schema violations
CN103368980A (en) * 2012-03-26 2013-10-23 深圳市财付通科技有限公司 Method and device for JSONP data request
CN104967630A (en) * 2014-04-10 2015-10-07 腾讯科技(深圳)有限公司 Method and apparatus for processing webpage access request

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8515999B2 (en) * 2011-09-29 2013-08-20 International Business Machines Corporation Method and system providing document semantic validation and reporting of schema violations
CN103368980A (en) * 2012-03-26 2013-10-23 深圳市财付通科技有限公司 Method and device for JSONP data request
CN104967630A (en) * 2014-04-10 2015-10-07 腾讯科技(深圳)有限公司 Method and apparatus for processing webpage access request

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
泡在网上的日子: "OkHttp使用教程", 《HTTP://WWW.JCODECRAEER.COM/A/ANZHUOKAIFA/ANDROIDKAIFA/2015/0106/2275.HTML》 *

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108874833A (en) * 2017-05-15 2018-11-23 武汉斗鱼网络科技有限公司 A kind of method and device for realizing Etcd Java client
CN107748660A (en) * 2017-10-30 2018-03-02 四川长虹技佳精工有限公司 A kind of implementation method of self-defined compatible network request framework
CN108040127A (en) * 2018-01-03 2018-05-15 南京工程学院 A kind of multiterminal data interaction based on location-based service Articles found, owner please contact platform
CN110677303A (en) * 2019-10-10 2020-01-10 荆楚理工学院 Network management system
WO2024198192A1 (en) * 2023-03-31 2024-10-03 百度国际科技(深圳)有限公司 Client communication network, network request processing method, communication system, electronic device, and medium

Similar Documents

Publication Publication Date Title
CN105681394A (en) Network request method and device based on OKHttp and GSON
CN104184553B (en) Low-power consumption bluetooth data transmission method, device and equipment
US9331953B2 (en) Device management method, middleware, and machine-to-machine communications platform, device, and system
CN105183452B (en) Spring AOP-based remote protocol service system for monitoring power distribution equipment
CN105763619B (en) The method and device that client and server-side are communicated
CN105897652A (en) Standard protocol based heterogeneous terminal dynamic access method
JP2011222049A5 (en)
US10495336B2 (en) Energy operations across domains
EP3171552B1 (en) Energy operations across domains
US20140164539A1 (en) Application service integration
CN102902537A (en) Embedded WEB model view controller (MVC) development framework based on Fast common gateway interface (CGI) and hypertext markup language (HTML) template
CN104917833A (en) Asynchronous information processing method based on NIO request
CN103873287A (en) Message compensating method based on enterprise service bus (ESB) message monitoring platform
CN105491111B (en) A kind of Internet of Things communication means based on WCF
CN110636127B (en) Communication processing method and system between information data
CN110224972A (en) One kind is towards multi-protocols polymorphic type equipment access system, method and device
CN102868759B (en) The system of server controls browser interface Refresh Data and method thereof
CN104702636B (en) Information interacting method, device and analogue system based on assembly function agent skill group
CN106412123B (en) Method and system for distributed processing of terminal equipment information by cloud access controller
CN105827671A (en) System platform characterized by distributed use and centralized management and portal server
CN105607606A (en) Data acquisition device and data acquisition method based on double-mainboard framework
CN102148702B (en) Method for managing network by utilizing network configuration protocol
CN103997509A (en) Service oriented architecture-based service processing method and apparatus
CN103561113B (en) The generation method and device of Web Service interface
CN104022901B (en) Method of configuring and managing ONU (Optical Network Unit) module of state grid concentrator by utilizing PLC (Programmable Logic Controller)

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right

Effective date of registration: 20190114

Address after: 518000 Building 405A, 01B, Zone E, Zhiheng Industrial Park, Nantou Street, Nanshan District, Shenzhen City, Guangdong Province

Applicant after: SHENZHEN ZHIYING TECHNOLOGY Co.,Ltd.

Address before: 518000 Meilong Road and Minwang Road, Minzhi Street, Longhua New District, Shenzhen City, Guangdong Province

Applicant before: SHENZHEN RUIMING UNLIMITED TECHNOLOGY Co.,Ltd.

TA01 Transfer of patent application right
WW01 Invention patent application withdrawn after publication

Application publication date: 20160615

WW01 Invention patent application withdrawn after publication