Class SimpleHttpRequest
First, HTTP headers are placed in a Map<String, String> structure. For example:
Map<String, String> requestProperties = new HashMap<>();
requestProperties.put("Content-Encoding", "UTF-8");
Next, if required, some data intended for the body of the message is prepared:
byte[] requestData = ....
Only for asynchronous operations, we need to set-up a listener for successful and failed operations:
SimpleHttpRequest.ResponseHandler responseHandler = new SimpleHttpRequest.ResponseHandler() {
@Override public void onSuccess(int statusCode, Map<String, List<String>> headers, byte[] successResponse) {
// handle here a successful HTTP request
}
@Override public void onFailure(int statusCode, Map<String, List<String>> headers, byte[] errorResponse, Throwable e) {
// handle here a failed HTTP request
}
};
Now the class can be instantiated.
SimpleHttpRequest request = new SimpleHttpRequest(theUrl, requestProperties, requestData, responseHandler);
Some request parameters can be configured prior to the request sent:
request.setConnectTimeout(2000);
request.setReadTimeout(2000);
And the actual message is initiated in the makeRequest() call
request.makeRequest(SimpleHttpRequest.HTTP_METHOD.GET);
Once makeRequest() returns, the response details can be extracted:
int responseCode = request.getResponseCode();
Map<String, List<String>> headers = getResponseHeaderFields();
if (200 == responseCode) {
byte[] responseData = request.getResponseData();
String contentType = request.getContentType()
} else {
String errorMessage = request.getErrorMessage();
}
:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic interfaceCallback that notifies on success or failure of an HTTP request. -
Constructor Summary
ConstructorsConstructorDescriptionSimpleHttpRequest(String requestUrl, Map<String, String> requestProperties, byte[] requestData) SimpleHttpRequest(String requestUrl, Map<String, String> requestProperties, byte[] requestData, SimpleHttpRequest.ResponseHandler responseHandler) -
Method Summary
Modifier and TypeMethodDescriptionGet the content type received for the HTTP requestGet the error message of a failed requestintGet the server's response code e.g.byte[]Get the body of the server's responseGet the header fields of the server's responsevoidExecute an HTTP request to the URL supplied in the constructor, with the properties and (optionally) request body provided.voidsetConnectTimeout(int timeout) Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by thisconnection.voidsetHttpConnection(HttpURLConnection connection) voidsetReadTimeout(int timeout) Sets the read timeout to a specified timeout, in milliseconds.
-
Constructor Details
-
SimpleHttpRequest
public SimpleHttpRequest(String requestUrl, @Nullable Map<String, String> requestProperties, @Nullable byte[] requestData, @Nullable SimpleHttpRequest.ResponseHandler responseHandler) -
SimpleHttpRequest
-
-
Method Details
-
setConnectTimeout
public void setConnectTimeout(int timeout) Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by thisconnection. If the timeout expires before the connection can be established, aSocketTimeoutExceptionis raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
timeout- anintthat specifies the connect timeout value in milliseconds
-
setReadTimeout
public void setReadTimeout(int timeout) Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.- Parameters:
timeout- anintthat specifies the timeout value to be used in milliseconds
-
makeRequest
Execute an HTTP request to the URL supplied in the constructor, with the properties and (optionally) request body provided.- Parameters:
method- The HTTP method to execute (GET, PUT, POST or HEADER)
-
getResponseCode
public int getResponseCode()Get the server's response code e.g. 200 (OK)- Returns:
- The response code returned by the server
-
getResponseHeaderFields
Get the header fields of the server's response- Returns:
- The header fields returned by the server
-
getContentType
Get the content type received for the HTTP request- Returns:
- The content-type string
-
getResponseData
public byte[] getResponseData()Get the body of the server's response- Returns:
- The complete response body returned by the server
-
getErrorMessage
Get the error message of a failed request- Returns:
- An error message describing the reason for a failed request (empty string if none)
-
setHttpConnection
-