HTTP Layer Guide
Overview
The purpose of the document is to guide on how to use HTTP Layer and its classes to help on working with REST API calls. This will also provide a way to get the response back via the callback methods. Using this, you can work with your GET and POST REST API methods and get the response as a callback. Please notice, we are not supporting Image or File downloading approach using this.
Creating a request object
To serve the web service calls, first step is to create a ORARequest object. Below are the different constructors to create ORARequest object
// Construct Request object with URL. Throws ORAInvalidRequestException if url is not a valid
url.public ORARequest(String url);
// Construct request object with url and map payload.
public ORARequest(String url, Map<String, String> payload);
// Construct request object with url and payload.
public ORARequest(String url, String jsonPayload);
In addition to the above constructors, we can use below setter methods to add more value to the ORARequest object.
// To be used if you want to set the URL separately without constructor
public void setUrl(String url);
// Set the url. If the payload is null, it will throw ORAInvalidRequestException
public void setPayload(String jsonPayload);
// Set the request method. Default value is REQUEST_TYPE_POST
public void setRequestType(@RequestType int requestType);
// Set whether payload should be compressed or not. Default value is false.
public void setCompress(boolean compress);
// Sets the retry count. If the request is failed, the same request is retried for retryCount times. The default value is 1. Minimum is 0 and maximum is 15.
public void setRetryCount(int retryCount);
// set the priority of the request. Default value is PRIORITY_NORMAL
public void setPriority(@Priority int priority);
//Set the request TAG. Default value is null.
public void setTAG(String tag)
// Set the callback.
public void setCallback(ORAHTTPResponseCallback callback);
// To add single request header.
public void addRequestHeader(String key, String value);
// To add multiple request headers
public void addRequestHeaders(Map<String, String> headers);
Example
Add the request to request queue
Once the ORARequest object is ready, now we need to use ORAHTTPManager.addRequest() method to trigger the request. Below is the implementation.
When you call addRequest()
method, the request will be added to the end of the queue. Once the requests prior to this requests are done, this request will be processed.
Immediate Request
If you set the priority as PRIORITY_IMMEDIATE
for the ORARequest
, the request will be added to the head of the queue and executed immediately after the current running process.
Get response call back
Since we passed the ORAHTTPResponseCallback
to the request queue above, whenever there is a response to the request, the below method will trigger automatically.
ORAResponse
will contain below methods
Execute Request when app is in background
By default HTTP requests are not processed when the application is in background. The HTTP request queue will be paused when the application goes to background. If any HTTP request is already started before app going to background, it will be completed even after the application went to background. All other requests in the queue will not be scheduled. Once the application comes back to foreground, the queue will be resumed and the requests will be processed. This behavior will same for the PRIORITY_IMMEDIATE
requests also.
This behavior can be overridden by using HTTP_BACKGROUND_TASK_ENABLED configuration. By default HTTP_BACKGROUND_TASK_ENABLED configuration value is false
. If HTTP_BACKGROUND_TASK_ENABLED value set to true, all the HTTP requests are processed even if the application is in background. Below is the code snippet to make HTTP requests execute in background.
Or you can add below line in oracle.json file under ORACORE to enable background execution.