CN108536487A - A kind of Get request method of load server data succinct based on Android platform, efficient - Google Patents
A kind of Get request method of load server data succinct based on Android platform, efficient Download PDFInfo
- Publication number
- CN108536487A CN108536487A CN201810327581.8A CN201810327581A CN108536487A CN 108536487 A CN108536487 A CN 108536487A CN 201810327581 A CN201810327581 A CN 201810327581A CN 108536487 A CN108536487 A CN 108536487A
- Authority
- CN
- China
- Prior art keywords
- data
- request
- url
- callback
- methods
- 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.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/445—Program loading or initiating
- G06F9/44521—Dynamic linking or loading; Link editing at or after load time, e.g. Java class loading
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The present invention relates to Android platform technical field, more particularly to the Get request of a kind of load server data succinct based on Android platform, efficient and can across Activity reception data method.The method of the present invention is one basic Get method of establishment in tool-class, parameter has the url of String types, the params of Map types, the callback of the Callback types and cancel of Boolean type, some fixed required parameter information are added in params parameters first, traversal parameter is spliced in url, url is added by request variables and is built, then getInstance singleton methods are used to obtain OkHttpClient client variables, network request is initiated using client to ask, finally make carbon copies Get methods, the data of return are handled in callback method.The present invention solve the problems, such as based on Android platform request server code complex redundancy, load data slowly and can not across Activity parsing data.
Description
Technical field
It is the present invention relates to Android platform technical field, more particularly to a kind of succinct based on Android platform, efficient
The Get request of load server data and can across Activity reception data method.
Background technology
Request server code complex redundancy, load data are slow in Android platform and can not across Activity solution
The problem of analysing data;In order to solve these problems, it needs to Reseal the method for Get request and returned data processing to realize.
Invention content
Present invention solves the technical problem that be to provide a kind of succinct, efficient load server data Get request and
Can across Activity reception data method;Solve request server code complex redundancy, load data slowly and can not be across
Activity parses the problem of data.
The present invention solve above-mentioned technical problem technical solution be:
In tool-class create a basic Get method, parameter have url, Map type of String types params,
The callback of the Callback types and cancel of Boolean type adds some in params parameters and fixed asks first
It asks parameter information, traversal parameter to be spliced in url, url is added by request variables and is built, is then used
GetInstance singleton methods obtain OkHttpClient client variables, initiate network request using client and ask,
Get methods are finally made carbon copies, the data of return are handled in callback method.It is based on Android platform request to solve
Code server complex redundancy, load data slowly and can not across Activity parsing data the problem of.
The method specifically comprises the following steps:
Step 1: creating tool-class HttpUtils in Android projects, static variable OkHttpClient is defined
MOkHttpClient is initialized by singleton pattern method;
Step 2: create a basic Get method, parameter have url, Map type of String types params,
The callback of the Callback types and cancel of Boolean type;
Step 3: adding some fixed required parameter information in params parameters in Get methods, traversal parameter is spliced to
In url, url is added by request variables and is built, then getInstance singleton methods is used to obtain
OkHttpClient client variables are initiated network request using client and are asked;
Step 4: the BaseEvent classes of general type are created, for parsing data;
Step 5: manifolding Get methods, and newly-increased BaseEvent event parameters, realize onFailure in callback with
OnResponse methods;
Step 6: obtaining the json data returned in onResponse, it is added in event, passes through the post of EventBus
(event) message with data is sent;
Step 7: in any Activity pages, data are received by onEventMainThread methods.
Beneficial effects of the present invention:In tool-class create a basic Get method, parameter have String types url,
The callback of params, Callback type of the Map types and cancel of Boolean type, first in params parameters
Some fixed required parameter information are added, traversal parameter is spliced in url, is added url by request variables and is built,
Then it uses getInstance singleton methods to obtain OkHttpClient client variables, network is initiated using client
Request is asked, and finally makes carbon copies Get methods, the data of return are handled in callback method, succinct, efficient to reach
The purpose of across the Activity parsing data of request server.Effectively solve based on Android platform request server generation
Code complex redundancy, load data slowly and can not across Activity parsing data the problem of.
Description of the drawings
The following further describes the present invention with reference to the drawings:
Fig. 1 is the flow chart of the present invention.
Specific implementation mode
As shown in Figure 1, the present invention uses following steps:
Step 1: creating tool-class HttpUtils in Android projects, static variable OkHttpClient is defined
MOkHttpClient is initialized by singleton pattern method;Such as:
public class HttpUtils{
private static OkHttpClient mOkHttpClient;
private static final int TIMEOUT_CONNECT_SECOND = 10;// connection time-out time, it is single
Position:Second
private static final int TIMEOUT_WRITE_SECOND = 10;// time-out time is read, it is single
Position:Second
private static final int TIMEOUT_READ_SECOND = 30;// write-in time-out time, unit:
Second
public static OkHttpClient getInstance() {
if (mOkHttpClient == null) {
mOkHttpClient=newOkHttpClient();
mOkHttpClient.setConnectTimeout(TIMEOUT_CONNECT_SECOND,
TimeUnit.SECONDS);
mOkHttpClient.setWriteTimeout(TIMEOUT_WRITE_SECOND, TimeUnit.SECONDS);
mOkHttpClient.setReadTimeout(TIMEOUT_READ_SECOND, TimeUnit.SECONDS);
}
return mOkHttpClient;
}
}
Step 2: create a basic Get method, parameter have url, Map type of String types params,
The callback of the Callback types and cancel of Boolean type;Such as:
public static void get(String url, Map<String, String> params, Callback
callback, boolean cancel) {}
Step 3: adding some fixed required parameter information in params parameters in Get methods, traversal parameter is spliced to
In url, url is added by request variables and is built, then getInstance singleton methods is used to obtain
OkHttpClient client variables are initiated network request using client and are asked;Such as:
public static void get(String url, Map<String, String> params, Callback
callback, boolean cancel) {
Set<String> keys = params.keySet();
String str = "token=aaaa&pwd=bbbbbbbb";
for (String key : keys) {
String value = params.get(key);
str+=…..;
}
if (!url.startsWith("http")){
url = …..;
}
Request request = new Request.Builder().url(url).build();
OkHttpClient client = getInstance();
if (cancel) {
client.cancel(Api.host + url);
}
client.newCall(request).enqueue(callback);
}
Step 4: the BaseEvent classes of general type are created, for parsing data;Such as:
public class BaseEvent<T> {
public static final int REQUEST_SUCCESS = 1;
public static final int NETWORK_UNAVAILABLE = 2;
public static final int REQUEST_FAILURE = -1;
protected String resultJson;
protected int resultType;
protected T data;
public boolean isRequestSuccess () {
Code omits ----
}
Get and set methods ...
}
Step 5: manifolding Get methods, and newly-increased BaseEvent event parameters, realize onFailure in callback with
OnResponse methods;Such as:
public static void get(String url, Map<String, String> params, final
BaseEvent event, boolean cancel) {
get(url, params, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
}, cancel);
}
Step 6: obtaining the json data returned in onResponse, it is added in event, passes through the post of EventBus
(event) message with data is sent;Such as:
public static void get(String url, Map<String, String> params, final
BaseEvent event, boolean cancel) {
get(url, params, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
event.setResultType(BaseEvent.REQUEST_FAILURE);
EventBus.getDefault().post(event);
}
@Override
public void onResponse(Response response) throws IOException {
String resultJson = response.body().string();
event.setResultJson(resultJson);
event.setResultType(BaseEvent.REQUEST_SUCCESS);
// event signal with data is sent in activity
EventBus.getDefault().post(event);
}
}, cancel);
}
Step 7: in any Activity pages, data are received by onEventMainThread methods.Such as:
public void onEventMainThread(BaseEvent event) {
if (event.isRequestSuccess()) {
Intent intent = new Intent(getActivity(), OtherActivity.class);
intent.putExtra(“data”, event.getData().toString());
getActivity().startActivity(intent);
} else {
ToastUtil.showLong (getActivity (), " request failures, reason:" +
event.getErrorMessage());
}
}
Claims (3)
1. the Get request of a kind of load server data succinct based on Android platform, efficient and can across Activity reception
The method of data, it is characterised in that:In tool-class create a basic Get method, parameter have String types url,
The callback of params, Callback type of the Map types and cancel of Boolean type, first in params parameters
Some fixed required parameter information are added, traversal parameter is spliced in url, is added url by request variables and is built,
Then it uses getInstance singleton methods to obtain OkHttpClient client variables, network is initiated using client
Request is asked, and finally makes carbon copies Get methods, the data of return are handled in callback method.
2. being based on Android platform request server code complex redundancy, load data slowly and can not be across to solve
Activity receives the problem of data.
3. according to claim 1 solve based on Android platform request server code complex redundancy, load data
Slow method, it is characterised in that:The method specifically comprises the following steps:
Step 1: creating tool-class HttpUtils in Android projects, static variable OkHttpClient is defined
MOkHttpClient is initialized by singleton pattern method;
Step 2: create a basic Get method, parameter have url, Map type of String types params,
The callback of the Callback types and cancel of Boolean type;
Step 3: adding some fixed required parameter information in params parameters in Get methods, traversal parameter is spliced to
In url, url is added by request variables and is built, then getInstance singleton methods is used to obtain
OkHttpClient client variables are initiated network request using client and are asked;
Step 4: the BaseEvent classes of general type are created, for parsing data;
Step 5: manifolding Get methods, and newly-increased BaseEvent event parameters, realize onFailure in callback with
OnResponse methods;
Step 6: obtaining the json data returned in onResponse, it is added in event, passes through the post of EventBus
(event) message with data is sent;
Step 7: in any Activity pages, data are received by onEventMainThread methods.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810327581.8A CN108536487A (en) | 2018-04-12 | 2018-04-12 | A kind of Get request method of load server data succinct based on Android platform, efficient |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810327581.8A CN108536487A (en) | 2018-04-12 | 2018-04-12 | A kind of Get request method of load server data succinct based on Android platform, efficient |
Publications (1)
Publication Number | Publication Date |
---|---|
CN108536487A true CN108536487A (en) | 2018-09-14 |
Family
ID=63481065
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201810327581.8A Pending CN108536487A (en) | 2018-04-12 | 2018-04-12 | A kind of Get request method of load server data succinct based on Android platform, efficient |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108536487A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109739666A (en) * | 2019-01-09 | 2019-05-10 | 广州虎牙信息科技有限公司 | Striding course call method, device, equipment and the storage medium of singleton method |
CN112887390A (en) * | 2021-01-20 | 2021-06-01 | 上海易校信息科技有限公司 | Network request method |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20080077809A1 (en) * | 2006-09-22 | 2008-03-27 | Bea Systems, Inc. | Credential Vault Encryption |
CN106484509A (en) * | 2016-09-27 | 2017-03-08 | 腾讯科技(深圳)有限公司 | A kind of output intent of pop-up, device and terminal |
-
2018
- 2018-04-12 CN CN201810327581.8A patent/CN108536487A/en active Pending
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20080077809A1 (en) * | 2006-09-22 | 2008-03-27 | Bea Systems, Inc. | Credential Vault Encryption |
CN106484509A (en) * | 2016-09-27 | 2017-03-08 | 腾讯科技(深圳)有限公司 | A kind of output intent of pop-up, device and terminal |
Non-Patent Citations (2)
Title |
---|
佚名: ""Android之利用EventBus发送消息传送示例"", 《WWW.ANDROID5.ONLINE/ANDROID/ANDROIDJC/GYANDROID/201603/11227.HTML》 * |
佚名: ""单例安全模式 下载上传文件 get,post请求"", 《HTTPS://BLOG.CSDN.NET/ZXCAAAAAAAA/ARTICLE/DETAILS/78758377》 * |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109739666A (en) * | 2019-01-09 | 2019-05-10 | 广州虎牙信息科技有限公司 | Striding course call method, device, equipment and the storage medium of singleton method |
CN112887390A (en) * | 2021-01-20 | 2021-06-01 | 上海易校信息科技有限公司 | Network request method |
CN112887390B (en) * | 2021-01-20 | 2023-02-17 | 上海易校信息科技有限公司 | Network request method |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
Oaks et al. | JXTA in a Nutshell | |
CN102664909B (en) | Re-establishing push notification channels via user identifiers | |
CN112417049B (en) | Cross-chain interaction method and system based on block chain all-in-one machine | |
EP2215773B1 (en) | Method and system for handling a failover in a distributed environment that uses session affinity | |
WO2005045724A2 (en) | Transaction processing architecture | |
CN112612985B (en) | Multi-user and multi-type message pushing system and method based on WebSocket | |
CN111698205B (en) | Service calling method and related equipment | |
CN104081748A (en) | Document communication runtime interface | |
CN107203541A (en) | Page loading method and its page loading device | |
CN108536487A (en) | A kind of Get request method of load server data succinct based on Android platform, efficient | |
US20080104238A1 (en) | Extending web service description language for sip/call flow interactions | |
CN1540945A (en) | Communication device and communication method | |
CN101155079B (en) | Method, device and system for monitoring instant communication server | |
CN113992755A (en) | Request processing method, system, equipment and storage medium based on micro service gateway | |
US20020188666A1 (en) | Lightweight dynamic service conversation controller | |
CN105897884A (en) | Android mobile phone push platform based on eXtensive messaging presence protocol (XMPP) | |
Anderson et al. | A critique of the open hypermedia protocol | |
US8111814B2 (en) | Extensible alert types | |
CN113791798A (en) | Model updating method and device, computer storage medium and electronic equipment | |
CN112825516A (en) | Token-based multi-system unified login method | |
CN112835728B (en) | Message processing method, device, computer equipment and storage medium | |
CN113542323A (en) | Service processing method, device, equipment and computer readable storage medium | |
CN111464533A (en) | System and method for accessing problem interface | |
CN117978792B (en) | Interaction method, device, equipment and medium of application program and Web terminal | |
CN116800704A (en) | Instant messaging method, device, equipment and readable storage medium based on group chat |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
WD01 | Invention patent application deemed withdrawn after publication | ||
WD01 | Invention patent application deemed withdrawn after publication |
Application publication date: 20180914 |