Network Communication (HTTP Layer) - Android
This section describes the methods required by the mobile app for communicating with server(s), for transmitting or receiving data over REST APIs.
Note: This layer does not support image or file downloads.
HTTP layer works with the app foreground state by default. To enable HTTP layer for transmitting data in the app background state, refer this section.
Creating a HTTP request
To create a connection for transmitting the data, the first step involves creating a ORARequest object. The object could be created in different ways.
Creating a ORARequest object with URL
The URL must be a valid URL or you will encounter the ORAInvalidRequestException
.
public ORARequest(String url);
Creating a ORARequest object with URL and Payload as a map
public ORARequest(String url, Map<String, String> payload);
Creating a ORARequest object with URL and Payload as a String
public ORARequest(String url, String jsonPayload);
Creating a ORARequest - Alternative method
Set the URL
public void setUrl(String url);
Set the payload
If the payload is null, you will encounter the ORAInvalidRequestException
public void setPayload(String jsonPayload);
Set the request method
The default request method is POST. You can override it with the below method.
public void setRequestType(@RequestType int requestType);
Configuring the Request Object
Compressing the payload
This method helps to compress the payload for faster transmission over the network. The default setting is set to false.
public void setCompress(boolean compress);
Retrying failed data transmissions
This method helps to set the number of retries if the event data transmission fails. The default set to 1. The minimum is 0 and maximum is 15.
public void setRetryCount(int retryCount);
Priority for data transmission
This method can be used to set the priority for behavior data transmission to the data collections servers. The default priority is set to normal (PRIORITY_NORMAL
).
public void setPriority(@Priority int priority);
The available priorities are PRIORITY_NORMAL
and PRIORITY_IMMEDIATE
. HTTP requests created with PRIORITY_IMMEDIATE
are transmitted with higher preference over HTTP requests with PRIORITY_NORMAL
.
Set unique ID for HTTP request
You can create a unique ID and pass it along with each HTTP request. This ID is echoed in the response object. You can check whether a particular request has been successfully processed with the help of the ID in the response object. The default value returned is NULL
.
public void setTAG(String tag)
Setting the callback
This method is used to set the call back function for the HTTP request. The status of the HTTP can be known by this method.
public void setCallback(ORAHTTPResponseCallback callback);
Adding header parameters to the request
Adding a single header to the request
You can use this method to add multiple a header statement to the HTTP request.
public void addRequestHeader(String key, String value);
Adding a multiple headers to the request
You can use this method to add multiple header statements to the HTTP request.
public void addRequestHeaders(Map<String, String> headers);
Sample Request Object
ORARequest request = new ORARequest("https://YOUR_END_POINT");
request.setPayload("YOUR_PAYLOAD");
request.addRequestHeader("Content-Type", "application/json");
request.setTAG("some_id");
request.setRequestType(ORARequest.REQUEST_TYPE_POST);
Triggering the HTTP request
Once the ORARequest object is ready, we can trigger the HTTP request using the ORAHTTPManager.addRequest() method. You can use the addResponse() method to add the HTTP request to the queue to be executed after the execution of the current process in the queue.
ORAHTTPManager.getInstance(context).addRequest(request, new ORAHTTPResponseCallback() {
@Override
public void onResponse(ORAResponse oraResponse) {
// code to handle response here
}
});
Response call back
Triggering a response for HTTP request
If the ORAHTTPResponseCallback
is included in the HTTP request, whenever a response is available for the request, the onResponse method is triggered automatically.
void onResponse(ORAResponse oraResponse);
ORAResponse
has the following methods that help to interpret the response.
Status of HTTP request
This method provides the response code for the HTTP request.
public int getResponseCode();
Response message for HTTP request
This message provides the response message for the HTTP request.
public String getMessage();
Response message body for HTTP request
This message provides the response body for the HTTP request.
public String getBody();
Exception encountered when processing HTTP request
This method returns the details of the exception encountered, if any, when processing the HTTP request. If no exception is encountered, it returns a NULL
.
public Exception getException();
Unique ID passed in the HTTP request
This method could be used to retrieve the unique ID that is passed along with the HTTP request. If no ID is passed, this method returns a NULL
value.
public String getRequestTAG();
Execute Request when app is in background
The default behavior for the HTTP request queue processing when an app is backgrounded. When an app is backgrounded, the HTTP requests in the queue are paused. If any HTTP request is under execution when the app is backgrounded, the request is executed and subsequent requests in the queue are paused. Once the app is resumed and is foregrounded, the HTTP requests resume executing. This is the default.
The default behavior of the queue can be overridden by using HTTP_BACKGROUND_TASK_ENABLED
configuration. The HTTP_BACKGROUND_TASK_ENABLED
configuration is set to false by default. If the HTTP_BACKGROUND_TASK_ENABLED
configuration is set to true, then all HTTP requests are processed, even if the application is backgrounded.
ORACoreDataContainer container = new ORACoreDataContainer(context);
container.putValue(ORABaseConfigSettings.HTTP_BACKGROUND_TASK_ENABLED, "true");
An alternative way to enable processing HTTP requests when the app is backgrounded is by adding the following line to the oracle.json file in the Core module.
"ora_dc_http_background_task_enabled" : "true"