Web service requests (v5)
The Android Apps Panel SDK provides easy way to communicate with web services.
HTTP Methods
It implements the main http methods so you can take full advantage of the RESTful API.
when (selectedMethod) {
"GET" -> {
method = APWSManager.HttpMethod.GET
}
"POST" -> {
method = APWSManager.HttpMethod.POST
}
"PATCH" -> {
method = APWSManager.HttpMethod.PATCH
}
"DELETE" -> {
method = APWSManager.HttpMethod.DELETE
}
}
switch (selectedMethod) {
case "GET":{
method = APWSManager.HttpMethod.GET;
}
case "POST" : {
method = APWSManager.HttpMethod.POST;
}
case "PATCH" : {
method = APWSManager.HttpMethod.PATCH;
}
case "DELETE" : {
method = APWSManager.HttpMethod.DELETE;
}
}
Callbacks
The following request callbacks are availables.
override fun onNoConnection(request: APWSRequest, cacheContent: String, cacheTimestamp: Long) {
//no connection callback
}
override fun onResponse(apwsRequest: APWSRequest, response: Any)
//sucess callback
//data response only
}
override fun onResponse(request: APWSRequest, response: Any, headers: MutableMap<String, String>) {
//sucess callback
//with data response + headers
}
override fun onError(request: APWSRequest, httpCode: Int, responseContent: String) {
//error callback
}
@Override
public void onNoConnection(APWSRequest request, String cacheContent, long cacheTimestamp) {
//no connection callback
}
@Override
public void onResponse(APWSRequest apwsRequest, Object response) {
//sucess callback
//data response only
}
@Override
public void onResponse(@NotNull APWSRequest request, Object response, @NotNull Map<String, String> headers) {
//sucess callback
//with data response + headers
}
@Override
public void onError(APWSRequest request, int httpCode, String error) {
//error callback
}
Information
onResponse callback with headers parameter is only available from SDK >= 5.0.5
Each implementation is optional so you can use only needed callback.
onResponse has a double implementation to give you access to the headers response, choose one of both according your need.
Parameters
Request parameters depends on method.
You can add GET params for GET method or POST params for POST, PATCH and DELETE methods.
You can also give JSON param as your body POST, PATCH or DELETE request.
if (method == APWSManager.HttpMethod.GET) {
//get param
req.addGetParam("token", userToken)
} else {
//post param
req.addPostParam("token", userToken)
//json param
req.addJsonParam("{ \"token\": $userToken }")
}
if (method == APWSManager.HttpMethod.GET) {
//get param
req.addGetParam("token", userToken);
} else {
//post param
req.addPostParam("token", userToken);
//json param
req.addJsonParam("{ \"token\": \"usertoken\" }");
}
Warning
addJsonParam() method is only operational from SDK >= 5.0.5
Settings
Some settings are availables.
req.isSecure = isCrypterRequest //to use crypted request
req.timeout = customTimeout //to custom time out (in ms)
req.sendUserToken = true //to send user token in request headers
req.setSecure(true); //to use crypted request
req.setTimeout(customTimeout); //to custom time out (in ms)
req.setSendUserToken(true); //to send user token in request headers
The first one allow you to crypt your request.
The second one permit to customize the request timeout in miliseconds.
Important to know : 0 is corresponding to disable timeout
Finally, it is possible to use saved user token to avoid manipulate it for each request.
You need to define it beforehand like below :
APManager.instance.localConfiguration.userToken = token
APManager.Companion.getInstance().localConfiguration.setUserToken(token);
Request call
When your request is correctly setted, you can ask to the SDK to perform it like this :
APWSManager.doRequest(req)
APWSManager.INSTANCE.doRequest(req);
Your request will be added to requests queu and it will be called as soon as possible.
Full sample
Here, the complete source code of the request call :
//request constructor
val req = APWSRequest(method, action, object : OnAPWSCallListener<Any>() {
//request callbacks
override fun onNoConnection(request: APWSRequest, cacheContent: String, cacheTimestamp: Long) {
//no connection callback
}
override fun onResponse(apwsRequest: APWSRequest, response: Any)
//sucess callback
//data response only
}
override fun onResponse(request: APWSRequest, response: Any, headers: MutableMap<String, String>) {
//sucess callback
//with data response + headers
}
override fun onError(request: APWSRequest, httpCode: Int, responseContent: String) {
//error callback
}
})
//params
if (method == APWSManager.HttpMethod.GET) {
//get param
req.addGetParam("token", userToken)
} else {
//post param
req.addPostParam("token", userToken)
//json param
req.addJsonParam("{ \"token\": $userToken }")
}
//request settings
req.isSecure = isCrypterRequest //to use crypted request
req.timeout = customTimeout //to custom time out (in ms)
req.sendUserToken = true //to send user token in request headers
//request call
APWSManager.doRequest(req)
//request constructor
APWSRequest req = new APWSRequest(method, action, new OnAPWSCallListener<Object>() {
//request callbacks
@Override
public void onNoConnection(APWSRequest request, String cacheContent, long cacheTimestamp) {
//no connection callback
}
@Override
public void onResponse(APWSRequest apwsRequest, Object response) {
//sucess callback
//data response only
}
@Override
public void onResponse(@NotNull APWSRequest request, Object response, @NotNull Map<String, String> headers) {
//sucess callback
//with data response + headers
}
@Override
public void onError(APWSRequest request, int httpCode, String error) {
//error callback
}
});
//params
if (method == APWSManager.HttpMethod.GET) {
req.addGetParam("token", userToken);
} else {
req.addPostParam("token", userToken);
req.addJsonParam("{ \"token\": \"usertoken\" }");
}
//settings
req.setSecure(true); //to use crypted request
req.setTimeout(customTimeout); //to custom time out (in ms)
req.setSendUserToken(true); //to send user token in request headers
//call
APWSManager.INSTANCE.doRequest(req);
Custom SDK base URL
According to project specifications you may need in exceptional cases to custom your SDK base url
localConf.sdkBaseURL = "https://www.sdk-domain.com";
mAPLocalConfiguration.setSdkBaseURL("https://www.mydomain.com");
Custom API base URL
You also may need to custom your API base url
localConf.baseURL = "https://www.api-domain.com";
mAPLocalConfiguration.setBaseURL("https://www.mydomain.com");
Updated almost 3 years ago